Skip to content

Commit

Permalink
Added mongodb flatten format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederik Reifschneider committed Mar 23, 2017
1 parent aed0ef5 commit 6a4313a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public enum FlattenMode {
/**
* Flattens every objects except arrays.
*/
KEEP_ARRAYS;
KEEP_ARRAYS,

/**
* Conforms to MongoDB dot.notation to update also nested documents.
*/
MONGODB;

}
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public JsonFlattener(Reader jsonReader) throws IOException {
*/
public JsonFlattener withFlattenMode(FlattenMode flattenMode) {
this.flattenMode = notNull(flattenMode);
if(flattenMode.equals(FlattenMode.MONGODB)) {
this.separator = '.';
this.leftBracket = '.';
}
flattenedMap = null;
return this;
}
Expand Down Expand Up @@ -181,8 +185,12 @@ public JsonFlattener withSeparator(char separator) {
}

private String illegalBracketsRegex() {
return "[\"\\s" + "]";
//return "[\"\\s" + Pattern.quote(this.separator.toString()) + "]";
if(this.flattenMode.equals(FlattenMode.MONGODB)) {
//allow . as separator, needed for arrays
return "[\"\\s" + "]";
} else {
return "[\"\\s" + Pattern.quote(this.separator.toString()) + "]";
}
}

/**
Expand All @@ -198,7 +206,7 @@ private String illegalBracketsRegex() {
*/
public JsonFlattener withLeftAndRightBrackets(char leftBracket,
char rightBracket) {
//isTrue(leftBracket != rightBracket, "Both brackets cannot be the same");
isTrue(!this.flattenMode.equals(FlattenMode.MONGODB) && (leftBracket != rightBracket), "Both brackets cannot be the same");
isTrue(!Character.toString(leftBracket).matches(illegalBracketsRegex()),
"Left bracket contains illegal chracter(%s)",
Character.toString(leftBracket));
Expand Down Expand Up @@ -358,15 +366,19 @@ private String computeKey() {
sb.append(policy.getCharSequenceTranslator().translate(key));
sb.append('\\');
sb.append('"');
//sb.append(rightBracket);
if(!this.flattenMode.equals(FlattenMode.MONGODB)) {
sb.append(rightBracket);
}
} else {
if (sb.length() != 0) sb.append(separator);
sb.append(policy.getCharSequenceTranslator().translate(key));
}
} else { // JsonValue
sb.append(leftBracket);
sb.append(iter.getIndex());
// sb.append(rightBracket);
if(!this.flattenMode.equals(FlattenMode.MONGODB)) {
sb.append(rightBracket);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,8 @@ public void testWithLeftAndRightBracket() {
}

@Test
@Ignore
public void testWithLeftAndRightBracketsExceptions() {
String json = "{\"abc\":{\"def\":123}}";

try {
new JsonFlattener(json).withLeftAndRightBrackets('#', '#');
fail();
Expand Down Expand Up @@ -333,6 +331,7 @@ public void testRootInMap() {
root.get(0).get(0));
}


@Test
public void testPrintMode() throws IOException {
URL url = Resources.getResource("test.json");
Expand Down Expand Up @@ -439,4 +438,18 @@ public void testInitByReader() throws IOException {
assertEquals(jf, new JsonFlattener(json));
}

@Test
public void testMongoFlattening() throws IOException {
URL url = Resources.getResource("test_mongo.json");
String src = Resources.toString(url, Charsets.UTF_8);

URL urlMongo = Resources.getResource("test_mongo_flattened.json");
String expectedJson = Resources.toString(urlMongo, Charsets.UTF_8);

String flattened = new JsonFlattener(src)
.withFlattenMode(FlattenMode.MONGODB).withPrintMode(PrintMode.PRETTY).flatten();

assertEquals(expectedJson, flattened);
}

}
30 changes: 30 additions & 0 deletions src/test/resources/test_mongo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup",
"GlossArray" : [
{
"a" : 1
},
{
"a" : 2
}
]
}
}
}
}
}
15 changes: 15 additions & 0 deletions src/test/resources/test_mongo_flattened.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"glossary.title": "example glossary",
"glossary.GlossDiv.title": "S",
"glossary.GlossDiv.GlossList.GlossEntry.ID": "SGML",
"glossary.GlossDiv.GlossList.GlossEntry.SortAs": "SGML",
"glossary.GlossDiv.GlossList.GlossEntry.GlossTerm": "Standard Generalized Markup Language",
"glossary.GlossDiv.GlossList.GlossEntry.Acronym": "SGML",
"glossary.GlossDiv.GlossList.GlossEntry.Abbrev": "ISO 8879:1986",
"glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para": "A meta-markup language, used to create markup languages such as DocBook.",
"glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso.0": "GML",
"glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso.1": "XML",
"glossary.GlossDiv.GlossList.GlossEntry.GlossSee": "markup",
"glossary.GlossDiv.GlossList.GlossEntry.GlossArray.0.a": 1,
"glossary.GlossDiv.GlossList.GlossEntry.GlossArray.1.a": 2
}

0 comments on commit 6a4313a

Please sign in to comment.