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));
}
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();
}
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));
}
}
}
}
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);
}
}
}
Aggregations