Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicate path when writing nested JSON array #32

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,10 @@ private static void writeArray(JsonWriter writer,
}
if (element instanceof StructuredRecord) {
StructuredRecord record = (StructuredRecord) element;
path.add(name);
processRecord(writer, record, Objects.requireNonNull(record.getSchema().getFields()),
path, jsonStringFieldsPaths);
path.remove(path.size() - 1);
} else {
path.add(name);
write(writer, name, true, element, componentSchema, path, jsonStringFieldsPaths);
path.remove(path.size() - 1);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,58 @@ public void testJsonStringWithEmptyArray() throws IOException {
}
}

@Test
public void testJsonStringWithStringArray() throws IOException {
Schema recordSchema = Schema.recordOf("record",
Schema.Field.of("arrayOfString", Schema.arrayOf(Schema.of(Schema.Type.STRING))));
List<String> jsonStringList = ImmutableList.of("{\"arrayKey1\": \"arrayValue1\"}",
"{\"arrayKey2\": \"arrayValue2\"}");
StructuredRecord record = StructuredRecord.builder(recordSchema).set("arrayOfString", jsonStringList).build();
Set<String> jsonStringFieldsPaths = ImmutableSet.of("arrayOfString");
try (JsonTreeWriter writer = new JsonTreeWriter()) {
writer.beginObject();
for (Schema.Field recordField : Objects.requireNonNull(record.getSchema().getFields())) {
if (recordSchema.getField(recordField.getName()) != null) {
BigQueryRecordToJson.write(writer, recordField.getName(), record.get(recordField.getName()),
recordField.getSchema(), jsonStringFieldsPaths);
}
}
writer.endObject();
JsonObject actual = writer.get().getAsJsonObject();
String actualJsonString = actual.get("arrayOfString").getAsJsonArray().toString();
String expectedJsonString = "[{\"arrayKey1\":\"arrayValue1\"},{\"arrayKey2\":\"arrayValue2\"}]";
Assert.assertEquals(expectedJsonString, actualJsonString);
}
}

@Test
public void testJsonStringWithArrayAndNestedRecord() throws IOException {
Schema nestedRecordSchema = Schema.recordOf("nestedRecord",
Schema.Field.of("nestedJsonString", Schema.of(Schema.Type.STRING)));
StructuredRecord nestedRecord = StructuredRecord.builder(nestedRecordSchema)
.set("nestedJsonString", "{\"nestedKey1\":\"nestedValue1\"}").build();
Schema recordSchema = Schema.recordOf("record",
Schema.Field.of("arrayOfNestedRecord", Schema.arrayOf(nestedRecordSchema)));
List<StructuredRecord> nestedRecordList = ImmutableList.of(nestedRecord);
StructuredRecord record = StructuredRecord.builder(recordSchema).set("arrayOfNestedRecord", nestedRecordList)
.build();

Set<String> jsonStringFieldsPaths = ImmutableSet.of("arrayOfNestedRecord.nestedJsonString");
try (JsonTreeWriter writer = new JsonTreeWriter()) {
writer.beginObject();
for (Schema.Field recordField : Objects.requireNonNull(record.getSchema().getFields())) {
if (recordSchema.getField(recordField.getName()) != null) {
BigQueryRecordToJson.write(writer, recordField.getName(), record.get(recordField.getName()),
recordField.getSchema(), jsonStringFieldsPaths);
}
}
writer.endObject();
JsonObject actual = writer.get().getAsJsonObject();
String actualJsonString = actual.get("arrayOfNestedRecord").toString();
String expectedJsonString = "[{\"nestedJsonString\":{\"nestedKey1\":\"nestedValue1\"}}]";
Assert.assertEquals(expectedJsonString, actualJsonString);
}
}

/**
* Empty JSON string is not a valid JSON string and should throw an exception.
Expand Down