Search in sources :

Example 16 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class BasicDBObject method toJson.

/**
 * Gets a JSON representation of this document
 *
 * @param writerSettings the json writer settings to use when encoding
 * @param encoder the BasicDBObject codec instance to encode the document with
 * @return a JSON representation of this document
 * @throws org.bson.codecs.configuration.CodecConfigurationException if the registry does not contain a codec for the document values.
 */
public String toJson(final JsonWriterSettings writerSettings, final Encoder<BasicDBObject> encoder) {
    JsonWriter writer = new JsonWriter(new StringWriter(), writerSettings);
    encoder.encode(writer, this, EncoderContext.builder().build());
    return writer.getWriter().toString();
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter)

Example 17 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class MongoCommandException method getResponseAsJson.

private static String getResponseAsJson(final BsonDocument commandResponse) {
    StringWriter writer = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(writer);
    new BsonDocumentCodec().encode(jsonWriter, commandResponse, EncoderContext.builder().build());
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 18 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class LazyDBObject method toString.

/**
 * Returns a JSON serialization of this object
 *
 * @return JSON serialization
 */
public String toString() {
    JsonWriter writer = new JsonWriter(new StringWriter(), JsonWriterSettings.builder().build());
    DBObjectCodec.getDefaultRegistry().get(LazyDBObject.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 19 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class Document method toJson.

/**
 * Gets a JSON representation of this document
 *
 * @param writerSettings the json writer settings to use when encoding
 * @param encoder the document codec instance to use to encode the document
 * @return a JSON representation of this document
 * @throws org.bson.codecs.configuration.CodecConfigurationException if the registry does not contain a codec for the document values.
 */
public String toJson(final JsonWriterSettings writerSettings, final Encoder<Document> encoder) {
    JsonWriter writer = new JsonWriter(new StringWriter(), writerSettings);
    encoder.encode(writer, this, EncoderContext.builder().build());
    return writer.getWriter().toString();
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter)

Example 20 with JsonWriter

use of org.bson.json.JsonWriter in project mongo-java-driver by mongodb.

the class Geometry method toJson.

/**
 * Converts to GeoJSON representation
 *
 * @return the GeoJSON representation
 */
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
public String toJson() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter, JsonWriterSettings.builder().build());
    Codec codec = getRegistry().get(getClass());
    codec.encode(writer, this, EncoderContext.builder().build());
    return stringWriter.toString();
}
Also used : Codec(org.bson.codecs.Codec) StringWriter(java.io.StringWriter) 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