Search in sources :

Example 1 with JsonWriterSettings

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

the class BsonDocumentTest method toJsonShouldRespectJsonWriterSettings.

@Test
public void toJsonShouldRespectJsonWriterSettings() {
    StringWriter writer = new StringWriter();
    JsonWriterSettings settings = JsonWriterSettings.builder().outputMode(JsonMode.SHELL).build();
    new BsonDocumentCodec().encode(new JsonWriter(writer, settings), document, EncoderContext.builder().build());
    assertEquals(writer.toString(), document.toJson(settings));
}
Also used : StringWriter(java.io.StringWriter) JsonWriterSettings(org.bson.json.JsonWriterSettings) JsonWriter(org.bson.json.JsonWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test)

Example 2 with JsonWriterSettings

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

the class Geometry method toJson.

/**
     * Converts to GeoJSON representation
     *
     * @return the GeoJSON representation
     */
@SuppressWarnings({ "unchecked", "rawtypes" })
public String toJson() {
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter, new JsonWriterSettings());
    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) JsonWriterSettings(org.bson.json.JsonWriterSettings) JsonWriter(org.bson.json.JsonWriter)

Example 3 with JsonWriterSettings

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

the class GenericBsonTest method runValid.

private void runValid() {
    String bsonHex = testCase.getString("bson").getValue().toUpperCase();
    String json = replaceUnicodeEscapes(testCase.getString("extjson", new BsonString("")).getValue());
    String canonicalJson = replaceUnicodeEscapes(testCase.getString("canonical_extjson", new BsonString(json)).getValue());
    String canonicalBsonHex = testCase.getString("canonical_bson", new BsonString(bsonHex)).getValue().toUpperCase();
    String description = testCase.getString("description").getValue();
    boolean lossy = testCase.getBoolean("lossy", new BsonBoolean(false)).getValue();
    BsonDocument decodedDocument = decodeToDocument(bsonHex, description);
    // B -> B
    assertEquals(format("Failed to create expected BSON for document with description '%s'", description), canonicalBsonHex, encodeToHex(decodedDocument));
    JsonWriterSettings jsonWriterSettings = JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build();
    // B -> E
    if (!canonicalJson.isEmpty()) {
        assertEquals(format("Failed to create expected JSON for document with description '%s'", description), stripWhiteSpace(canonicalJson), stripWhiteSpace(decodedDocument.toJson(jsonWriterSettings)));
    }
    if (!canonicalBsonHex.equals(bsonHex)) {
        BsonDocument decodedCanonicalDocument = decodeToDocument(canonicalBsonHex, description);
        // B -> B
        assertEquals(format("Failed to create expected BSON for canonical document with description '%s'", description), canonicalBsonHex, encodeToHex(decodedCanonicalDocument));
        // B -> E
        assertEquals(format("Failed to create expected JSON for canonical document with description '%s'", description), stripWhiteSpace(canonicalJson), stripWhiteSpace(decodedCanonicalDocument.toJson(jsonWriterSettings)));
    }
    if (!json.isEmpty()) {
        BsonDocument parsedDocument = BsonDocument.parse(json);
        // E -> E
        assertEquals(format("Failed to parse expected JSON for document with description '%s'", description), stripWhiteSpace(canonicalJson), stripWhiteSpace(parsedDocument.toJson(jsonWriterSettings)));
        if (!lossy) {
            // E -> B
            assertEquals(format("Failed to create expected BsonDocument for parsed canonical JSON document with description '%s'", description), decodedDocument, parsedDocument);
            assertEquals(format("Failed to create expected BSON for parsed JSON document with description '%s'", description), canonicalBsonHex, encodeToHex(parsedDocument));
        }
        if (!canonicalJson.equals(json)) {
            BsonDocument parsedCanonicalDocument = BsonDocument.parse(canonicalJson);
            // E -> E
            assertEquals(format("Failed to create expected JSON for parsed canonical JSON document with description '%s'", description), stripWhiteSpace(canonicalJson), stripWhiteSpace(parsedCanonicalDocument.toJson(jsonWriterSettings)));
            if (!lossy) {
                // E -> B
                assertEquals(format("Failed to create expected BsonDocument for parsed canonical JSON document " + "with description '%s'", description), decodedDocument, parsedCanonicalDocument);
                assertEquals(format("Failed to create expected BSON for parsed canonical JSON document with description '%s'", description), bsonHex, encodeToHex(parsedDocument));
            }
        }
    }
}
Also used : JsonWriterSettings(org.bson.json.JsonWriterSettings)

Example 4 with JsonWriterSettings

use of org.bson.json.JsonWriterSettings in project graylog2-server by Graylog2.

the class SeedingFongoRule method insertSeed.

public void insertSeed(String seed) throws IOException {
    final byte[] bytes = Resources.toByteArray(Resources.getResource(seed));
    final Map<String, Object> map = objectMapper.readValue(bytes, MAP_TYPE);
    for (String collectionName : map.keySet()) {
        @SuppressWarnings("unchecked") final List<Map<String, Object>> documents = (List<Map<String, Object>>) map.get(collectionName);
        final MongoCollection<Document> indexSets = getDatabase().getCollection(collectionName);
        for (Map<String, Object> document : documents) {
            final Document parsedDocument = Document.parse(objectMapper.writeValueAsString(document));
            LOG.debug("Inserting parsed document: \n{}", parsedDocument.toJson(new JsonWriterSettings(true)));
            indexSets.insertOne(parsedDocument);
        }
    }
}
Also used : JsonWriterSettings(org.bson.json.JsonWriterSettings) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.bson.Document) Map(java.util.Map)

Aggregations

JsonWriterSettings (org.bson.json.JsonWriterSettings)4 StringWriter (java.io.StringWriter)2 JsonWriter (org.bson.json.JsonWriter)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Document (org.bson.Document)1 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)1 Codec (org.bson.codecs.Codec)1 Test (org.junit.Test)1