use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.
the class GridFSFile method toString.
@Override
public String toString() {
JsonWriter writer = new JsonWriter(new StringWriter(), JsonWriterSettings.builder().build());
DEFAULT_REGISTRY.get(GridFSFile.class).encode(writer, this, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
return writer.getWriter().toString();
}
use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.
the class RawBsonDocument method toJson.
@Override
public String toJson(final JsonWriterSettings settings) {
StringWriter writer = new StringWriter();
new RawBsonDocumentCodec().encode(new JsonWriter(writer, settings), this, EncoderContext.builder().build());
return writer.toString();
}
use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.
the class BsonDocument method toJson.
/**
* Gets a JSON representation of this document using the given {@code JsonWriterSettings}.
* @param settings the JSON writer settings
* @return a JSON representation of this document
*/
public String toJson(final JsonWriterSettings settings) {
StringWriter writer = new StringWriter();
new BsonDocumentCodec().encode(new JsonWriter(writer, settings), this, EncoderContext.builder().build());
return writer.toString();
}
use of org.bson.json.JsonWriter in project realm-java by realm.
the class JniBsonProtocol method encode.
public static <T> String encode(T value, Encoder<T> encoder) {
try {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter, writerSettings);
jsonWriter.writeStartDocument();
jsonWriter.writeName(VALUE);
encoder.encode(jsonWriter, value, EncoderContext.builder().build());
jsonWriter.writeEndDocument();
return stringWriter.toString();
} catch (CodecConfigurationException e) {
// might be missing
throw new AppException(ErrorCode.BSON_CODEC_NOT_FOUND, "Could not resolve encoder for end type", e);
} catch (Exception e) {
throw new AppException(ErrorCode.BSON_ENCODING, "Error encoding value", e);
}
}
use of org.bson.json.JsonWriter in project spring-data-mongodb by spring-projects.
the class ExpressionEvaluatingParameterBinder method encode.
private <T> String encode(T value, Supplier<Codec<T>> defaultCodec) {
Codec<T> codec = codecRegistryProvider.getCodecFor((Class<T>) value.getClass()).orElseGet(defaultCodec);
StringWriter writer = new StringWriter();
codec.encode(new JsonWriter(writer), value, null);
writer.flush();
return writer.toString();
}
Aggregations