Skip to content

Commit

Permalink
Fix parseToken array popping
Browse files Browse the repository at this point in the history
Signed-off-by: naomichi-y <[email protected]>
  • Loading branch information
naomichi-y committed May 8, 2024
1 parent 1c208d5 commit 51d6d10
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public JsonToStringXContentParser(
public XContentParser parseObject() throws IOException {
builder.startObject();
StringBuilder path = new StringBuilder(fieldTypeName);
parseToken(path, null);
parseToken(path, null, true);
builder.field(this.fieldTypeName, keyList);
builder.field(this.fieldTypeName + VALUE_SUFFIX, valueList);
builder.field(this.fieldTypeName + VALUE_AND_PATH_SUFFIX, valueAndPathList);
Expand All @@ -74,8 +74,7 @@ public XContentParser parseObject() throws IOException {
return JsonXContent.jsonXContent.createParser(this.xContentRegistry, this.deprecationHandler, String.valueOf(jString));
}

private void parseToken(StringBuilder path, String currentFieldName) throws IOException {

private void parseToken(StringBuilder path, String currentFieldName, boolean popName) throws IOException {
while (this.parser.nextToken() != Token.END_OBJECT) {
if (this.parser.currentName() != null) {
currentFieldName = this.parser.currentName();
Expand All @@ -100,15 +99,18 @@ private void parseToken(StringBuilder path, String currentFieldName) throws IOEx
this.keyList.add(fieldNameSuffix);
}
} else if (this.parser.currentToken() == Token.START_ARRAY) {
parseToken(path, currentFieldName);
parseToken(path, currentFieldName, false);
break;
} else if (this.parser.currentToken() == Token.END_ARRAY) {
// skip
} else if (this.parser.currentToken() == Token.START_OBJECT) {
parseToken(path, currentFieldName);
int dotIndex = path.lastIndexOf(DOT_SYMBOL);
if (dotIndex != -1) {
path.setLength(path.length() - currentFieldName.length() - 1);
parseToken(path, currentFieldName, true);

if (popName) {
int dotIndex = path.lastIndexOf(DOT_SYMBOL);
if (dotIndex != -1) {
path.setLength(path.length() - currentFieldName.length() - 1);
}
}
} else {
if (!path.toString().contains(currentFieldName)) {
Expand All @@ -117,12 +119,15 @@ private void parseToken(StringBuilder path, String currentFieldName) throws IOEx
parseValue(parsedFields);
this.valueList.add(parsedFields.toString());
this.valueAndPathList.add(path + EQUAL_SYMBOL + parsedFields);
int dotIndex = path.lastIndexOf(DOT_SYMBOL);
if (dotIndex != -1) {
path.setLength(path.length() - currentFieldName.length() - 1);

if (popName) {
int dotIndex = path.lastIndexOf(DOT_SYMBOL);

if (dotIndex != -1) {
path.setLength(path.length() - currentFieldName.length() - 1);
}
}
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,25 @@ public void testNestChildObjectWithDotsAndFieldWithDots() throws IOException {
);
}

public void testArrayOfObjects() throws IOException {
String jsonExample ="{" +
"\"field\": {" +
" \"detail\": {" +
" \"foooooooooooo\": [" +
" {\"name\":\"baz\"}," +
" {\"name\":\"baz\"}" +
" ]" +
" }" +
"}}";

assertEquals("{" +
"\"flat\":[\"field\",\"detail\",\"foooooooooooo\",\"name\",\"name\"]," +
"\"flat._value\":[\"baz\",\"baz\"]," +
"\"flat._valueAndPath\":[" +
"\"flat.field.detail.foooooooooooo.name=baz\"," +
"\"flat.field.detail.foooooooooooo.name=baz\"" +
"]}",
flattenJsonString("flat", jsonExample)
);
}
}

0 comments on commit 51d6d10

Please sign in to comment.