Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@
abstract class AbstractJsonEntityProducer implements AsyncEntityProducer {

private final InternalBuffer buffer;
private final ContentType contentType;
private volatile State state;

AbstractJsonEntityProducer(final int initSize) {
this(initSize, null);
}

AbstractJsonEntityProducer(final int initSize, final ContentType contentType) {
this.buffer = new InternalBuffer(initSize);
this.contentType = contentType != null ? contentType : ContentType.APPLICATION_JSON;
this.state = State.ACTIVE;
}

Expand All @@ -58,7 +64,7 @@ public final Set<String> getTrailerNames() {

@Override
public final String getContentType() {
return ContentType.APPLICATION_JSON.toString();
return contentType.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.util.Args;

/**
Expand All @@ -45,12 +46,25 @@ public class JsonObjectEntityProducer<T> extends AbstractJsonEntityProducer {
private final T jsonObject;
private final ObjectMapper objectMapper;

public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper) {
super(4096);
/**
* Creates a new instance that serializes the given object using the specified content type.
* If {@code contentType} is {@code null}, defaults to {@code application/json}.
*
* @param jsonObject the object to serialize.
* @param objectMapper the object mapper.
* @param contentType the content type, or {@code null} for {@code application/json}.
* @since 5.5
*/
public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper, final ContentType contentType) {
super(4096, contentType);
this.jsonObject = Args.notNull(jsonObject, "Json object");
this.objectMapper = Args.notNull(objectMapper, "Object mapper");
}

public JsonObjectEntityProducer(final T jsonObject, final ObjectMapper objectMapper) {
this(jsonObject, objectMapper, null);
}

@Override
final void generateJson(final OutputStream outputStream) throws IOException {
objectMapper.writeValue(outputStream, jsonObject);
Expand Down
Loading