JsonUtils { public static BsonValue parse(String json) throws JsonParseException { if (json == null) { return null; } String trimmed = json.trim(); if (trimmed.isEmpty()) { return null; } else if (trimmed.startsWith("{")) { try { return BsonDocument.parse(json); } catch (BsonInvalidOperationException ex) { return getBsonValue(json); } } else if (trimmed.startsWith("[")) { return BSON_ARRAY_CODEC.decode( new JsonReader(json), DecoderContext.builder().build()); } else { return getBsonValue(json); } }  static BsonValue unescapeKeys(BsonValue json); static BsonValue escapeKeys(BsonValue json, boolean escapeDots); static List<Optional<BsonValue>> getPropsFromPath(
            BsonValue root,
            String path); static boolean isAncestorPath(final String left, final String right); static Integer countPropsFromPath(BsonValue root, String path); static boolean checkType(Optional<BsonValue> o, String type); static String minify(String jsonString); static BsonValue parse(String json); static String toJson(BsonValue bson); static String toJson(BsonValue bson, JsonMode mode); static String getIdAsString(BsonValue id, boolean quote); static BsonDocument toBsonDocument(Map<String, Object> map); static boolean isUpdateOperator(String key); static boolean containsUpdateOperators(BsonValue json); static boolean containsUpdateOperators(BsonValue json,
            boolean ignoreCurrentDate); static BsonValue unflatten(BsonValue json); static BsonDocument flatten(BsonDocument json, boolean ignoreUpdateOperators);  }
@Test public void testJsonArray() throws Exception { String _json1 = "{a: []}}"; String _json2 = "{a: [{}]}}"; String _json3 = "{a: [{f: 1}]}}"; String _json4 = "{a: [{e: 1}, {e: 2}, {e: 3}]}}"; String _json5 = "{a: {'1': {e: 1}, '2': {e: 2}, '3': {e: 3}}}}"; BsonDocument json1 = BsonDocument.parse(_json1); BsonDocument json2 = BsonDocument.parse(_json2); BsonDocument json3 = BsonDocument.parse(_json3); BsonDocument json4 = BsonDocument.parse(_json4); BsonDocument json5 = BsonDocument.parse(_json5); Assert.assertTrue(checkGetPropsFromPath(json1, "$.a", "[]")); Assert.assertTrue(checkGetPropsFromPath(json1, "$.a.[*]")); Assert.assertTrue(checkGetPropsFromPath(json1, "$.a.[*].f")); Assert.assertTrue(checkGetPropsFromPath(json2, "$.a", "[{}]")); Assert.assertTrue(checkGetPropsFromPath(json2, "$.a.[*]", "{}")); Assert.assertTrue(checkGetPropsFromPath(json2, "$.a.[*].f", (String) null)); Assert.assertTrue(checkGetPropsFromPath(json3, "$.a", "[{f: 1}]")); Assert.assertTrue(checkGetPropsFromPath(json3, "$.a.[*]", "{f: 1}")); Assert.assertTrue(checkGetPropsFromPath(json3, "$.a.[*].f", "1")); Assert.assertTrue(checkGetPropsFromPath(json4, "$.a.[*].e", "1", "2", "3")); Assert.assertTrue(checkGetPropsFromPath(json4, "$.a.[*].e", "1", "2", "3")); Assert.assertTrue(checkGetPropsFromPath(json5, "$.a.*", "{e: 1}", "{e: 2}", "{e: 3}")); Assert.assertTrue(checkGetPropsFromPath(json5, "$.a.[*]", "{e: 1}", "{e: 2}", "{e: 3}")); }
@Test public void testJsonObject() throws Exception { String _json1 = "{o: {}}"; String _json2 = "{o: {o: {}}}"; String _json3 = "{o: {o: {f: 1}}}"; BsonDocument json1 = BsonDocument.parse(_json1); BsonDocument json2 = BsonDocument.parse(_json2); BsonDocument json3 = BsonDocument.parse(_json3); Assert.assertTrue(checkGetPropsFromPath(json1, "$.o", "{}")); Assert.assertTrue(checkGetPropsFromPath(json1, "$.*", "{}")); Assert.assertTrue(checkGetPropsFromPath(json1, "$.o.*")); Assert.assertTrue(checkGetPropsFromPath(json1, "$.o.*.f")); Assert.assertTrue(checkGetPropsFromPath(json2, "$.o", "{o: {}}")); Assert.assertTrue(checkGetPropsFromPath(json2, "$.*", "{o: {}}")); Assert.assertTrue(checkGetPropsFromPath(json2, "$.o.o", "{}")); Assert.assertTrue(checkGetPropsFromPath(json2, "$.o.*", "{}")); Assert.assertTrue(checkGetPropsFromPath(json2, "$.o.*.f", (String) null)); Assert.assertTrue(checkGetPropsFromPath(json3, "$.o", "{o: {f: 1}}")); Assert.assertTrue(checkGetPropsFromPath(json3, "$.*", "{o: {f: 1}}")); Assert.assertTrue(checkGetPropsFromPath(json3, "$.o.o", "{f: 1}")); Assert.assertTrue(checkGetPropsFromPath(json3, "$.o.*", "{f: 1}")); Assert.assertTrue(checkGetPropsFromPath(json3, "$.o.*.f", "1")); }
@Test public void testParseObjectId() { ObjectId id = new ObjectId(); BsonValue parsed = JsonUtils.parse( "{'$oid':'" .concat(id.toString()) .concat("'}")); Assert.assertTrue(parsed.isObjectId()); Assert.assertEquals(parsed.asObjectId().getValue(), id); }
@Test public void testParseFloat() { BsonValue parsed = JsonUtils.parse("3.1415"); Assert.assertTrue(parsed.isNumber()); Assert.assertEquals(parsed.asDouble(), new BsonDouble(3.1415)); }
@Test public void testParseString() { BsonValue parsed = JsonUtils.parse("'hello'"); Assert.assertTrue(parsed.isString()); Assert.assertEquals(parsed.asString(), new BsonString("hello")); }
@Test public void testParseEmptyString() { BsonValue parsed = JsonUtils.parse("''"); Assert.assertTrue(parsed.isString()); Assert.assertEquals(parsed.asString(), new BsonString("")); }