Skip to content

Commit

Permalink
check if main json structure is null/non-object, pass through as-is; …
Browse files Browse the repository at this point in the history
…check if "records" exist in object, if not, pass through as-is; add tests for these cases. (#49)
  • Loading branch information
eemhu authored Dec 20, 2024
1 parent a69368b commit 8611496
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/com/teragrep/aer_02/json/JsonRecords.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,14 @@ public String[] records() {
return rv;
}

final JsonValue recordsStructure = mainStructure.getValue("/records");
if (mainStructure == null || !mainStructure.getValueType().equals(JsonValue.ValueType.OBJECT)) {
// pass event through as-is if top-level structure is not object or doesn't exist
return rv;
}

final JsonValue recordsStructure = mainStructure.asJsonObject().get("records");

if (recordsStructure.getValueType().equals(JsonValue.ValueType.ARRAY)) {
if (recordsStructure != null && recordsStructure.getValueType().equals(JsonValue.ValueType.ARRAY)) {
final JsonArray recordsArray = recordsStructure.asJsonArray();
String[] records = new String[recordsArray.size()];

Expand All @@ -92,7 +97,7 @@ public String[] records() {
return records;
}

// pass event through as-is if "records" is not an array type
// pass event through as-is if "records" is not an array type or doesn't exist
return rv;
}

Expand Down
27 changes: 27 additions & 0 deletions src/test/java/com/teragrep/aer_02/json/JsonRecordsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,33 @@

public class JsonRecordsTest {

@Test
void testJsonArrayCase() {
final String records = Json.createArrayBuilder().add(0, 1).add(1, 2).build().toString();
JsonRecords jr = new JsonRecords(records);
final String[] result = jr.records();
Assertions.assertEquals(1, result.length);
Assertions.assertEquals("[1,2]", result[0]);
}

@Test
void testEmptyJsonObjectCase() {
final String records = Json.createObjectBuilder().build().toString();
JsonRecords jr = new JsonRecords(records);
final String[] result = jr.records();
Assertions.assertEquals(1, result.length);
Assertions.assertEquals("{}", result[0]);
}

@Test
void testOtherJsonObjectCase() {
final String records = Json.createObjectBuilder().add("k1", "v1").add("k2", "v2").build().toString();
JsonRecords jr = new JsonRecords(records);
final String[] result = jr.records();
Assertions.assertEquals(1, result.length);
Assertions.assertEquals("{\"k1\":\"v1\",\"k2\":\"v2\"}", result[0]);
}

@Test
void testRecordsAsObjectsCase() {
final String records = Json
Expand Down

0 comments on commit 8611496

Please sign in to comment.