Search in sources :

Example 6 with JsonWriter

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();
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter)

Example 7 with JsonWriter

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();
}
Also used : StringWriter(java.io.StringWriter) RawBsonDocumentCodec(org.bson.codecs.RawBsonDocumentCodec) JsonWriter(org.bson.json.JsonWriter)

Example 8 with JsonWriter

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();
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 9 with JsonWriter

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);
    }
}
Also used : AppException(io.realm.mongodb.AppException) StringWriter(java.io.StringWriter) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) JsonWriter(org.bson.json.JsonWriter) AppException(io.realm.mongodb.AppException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException)

Example 10 with JsonWriter

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();
}
Also used : StringWriter(java.io.StringWriter) UtilityClass(lombok.experimental.UtilityClass) JsonWriter(org.bson.json.JsonWriter)

Aggregations

JsonWriter (org.bson.json.JsonWriter)20 StringWriter (java.io.StringWriter)19 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)4 Test (org.junit.Test)4 BsonBinaryReader (org.bson.BsonBinaryReader)2 BsonWriter (org.bson.BsonWriter)2 ByteBuf (org.bson.ByteBuf)2 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)2 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 TextBasedBenchmarkResultWriter (com.mongodb.benchmark.framework.TextBasedBenchmarkResultWriter)1 AppException (io.realm.mongodb.AppException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 UtilityClass (lombok.experimental.UtilityClass)1 BsonReader (org.bson.BsonReader)1