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

Support list of objects for JSON #693

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add support list of objects in json transformer
  • Loading branch information
RVRhub committed Feb 11, 2023
commit e38f556f88a0836113dc12e15b6340ff24d85e16
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String apply(IN input, Schema<IN, ?> schema) {
if (fields[i] instanceof CompositeField) {
sb.append(apply(input, (CompositeField) fields[i], i));
} else {
applyValue(input, sb, fields[i]);
applyValue(input, sb, ((SimpleField) fields[i]).transform(input));
}
if (i < fields.length - 1) {
sb.append(", ");
Expand Down Expand Up @@ -77,8 +77,7 @@ public String generate(Schema<IN, ?> schema, int limit) {
return limit > 1 ? wrappers[0] + LINE_SEPARATOR + sb + LINE_SEPARATOR + wrappers[1] : sb.toString();
}

private void applyValue(IN input, StringBuilder sb, Field<?, ?> fields) {
Object value = ((SimpleField) fields).transform(input);
private void applyValue(IN input, StringBuilder sb, Object value) {
if (value instanceof Collection<?>) {
sb.append(generate(input,(Collection) value));
} else if (value != null && value.getClass().isArray()) {
Expand All @@ -100,7 +99,7 @@ private String generate(IN input, Collection<Object> collection) {
if (value instanceof CompositeField<?,?>) {
sb.append(apply(input,((CompositeField) value)));
} else {
value2String(value, sb);
applyValue(input, sb, value);
}
}
sb.append("]");
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/net/datafaker/formats/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,35 @@ void jsonObjectCollectionTest() {
"\"objectCollection\": [{\"country\": \"Denmark\", \"city\": \"Port Angel\"}, {\"two\": \"Denmark\", \"one\": \"Port Angel\"}]}");
}

@Test
void jsonCollectionOfCollectionsTest() {
JsonTransformer<Name> transformer = JsonTransformer.<Name>builder().build();

String json = transformer.generate(
Schema.of(
field("text", () -> "Mrs. Brian Braun"),
field("objectCollection", () -> List.of(
List.of(
List.of(
compositeField(null, new Field[]{
field("country", () -> "Denmark"),
field("city", () -> "Port Angel")
}
),
compositeField(null, new Field[]{
field("two", () -> "Denmark"),
field("one", () -> "Port Angel")
}
)
)
)
)
)
), 1);
assertThat(json).isEqualTo("{\"text\": \"Mrs. Brian Braun\", " +
"\"objectCollection\": [[[{\"country\": \"Denmark\", \"city\": \"Port Angel\"}, {\"two\": \"Denmark\", \"one\": \"Port Angel\"}]]]}");
}

private static Map.Entry<Supplier<String>, Supplier<Object>> entry(
Supplier<String> key, Supplier<Object> value) {
return new AbstractMap.SimpleEntry<>(key, value);
Expand Down