JsonSerializer extends AbstractSerializer<JsonNodeReadOnlyTrx, JsonNodeTrx> { private long maxLevel() { return castVisitor().getMaxLevel(); } private  JsonSerializer(final JsonResourceManager resourceMgr, final @Nonnegative long nodeKey, final Builder builder,
      final boolean initialIndent, final @Nonnegative int revision, final int... revsions); @Override void emitNode(final JsonNodeReadOnlyTrx rtx); static void main(final String... args); static Builder newBuilder(final JsonResourceManager resMgr, final Writer stream, final int... revisions); static Builder newBuilder(final JsonResourceManager resMgr, final @Nonnegative long nodeKey,
      final Writer stream, final JsonSerializerProperties properties, final int... revisions);  }
@Test public void testJsonDocumentWithMetadataAndMaxLevelAndPrettyPrinting() throws IOException { JsonTestHelper.createTestDocument(); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE); final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).withMetaData(true).maxLevel(2).prettyPrint().build(); serializer.call(); final var expected = Files.readString(JSON.resolve("testdoc-withmetadata-withmaxlevel.json"), StandardCharsets.UTF_8); final var actual = writer.toString(); assertEquals(expected, actual); } }
@Test public void testJsonDocumentWithMetadataAndMaxLevelSecond() throws IOException { final var jsonPath = JSON.resolve("simple-testdoc.json"); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE); final var trx = manager.beginNodeTrx(); final Writer writer = new StringWriter()) { final var shredder = new JsonShredder.Builder(trx, JsonShredder.createFileReader(jsonPath), InsertPosition.AS_FIRST_CHILD).commitAfterwards().build(); shredder.call(); final var serializer = new JsonSerializer.Builder(manager, writer).withMetaData(true).maxLevel(2).build(); serializer.call(); final var expected = Files.readString(JSON.resolve("simple-testdoc-withmetadata-withmaxlevel.json"), StandardCharsets.UTF_8); final var actual = writer.toString(); JSONAssert.assertEquals(expected, actual, true); } }
@Test public void testJsonDocumentWithMetadataAndPrettyPrintingAndObjectStartNodeKeyAndMaxLevelTwo() throws IOException { JsonTestHelper.createTestDocument(); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE); final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).withMetaData(true) .startNodeKey(15) .maxLevel(2) .prettyPrint() .build(); serializer.call(); final var expected = Files.readString(JSON.resolve("test-withmetadata-withprettyprinting-withstartnodekey-withmaxlevel2.json"), StandardCharsets.UTF_8); final var actual = writer.toString(); assertEquals(expected, actual); } }
@Test public void testJsonDocumentWithNodeKeyMetadataAndPrettyPrintingAndObjectStartNodeKeyAndMaxLevel() throws IOException { JsonTestHelper.createTestDocument(); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE); final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).withNodeKeyMetaData(true) .startNodeKey(15) .maxLevel(3) .prettyPrint() .build(); serializer.call(); final var expected = Files.readString(JSON.resolve("test-withnodekeymetadata-withprettyprinting-withstartnodekey-withmaxlevel.json"), StandardCharsets.UTF_8); final var actual = writer.toString(); assertEquals(expected, actual); } }
@Test public void testJsonDocumentWithChildCountMetadataAndPrettyPrintingAndObjectStartNodeKeyAndMaxLevel() throws IOException { JsonTestHelper.createTestDocument(); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE); final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).withNodeKeyAndChildCountMetaData(true) .startNodeKey(15) .maxLevel(3) .prettyPrint() .build(); serializer.call(); final var expected = Files.readString(JSON.resolve("test-withnodekeyandchildcountmetadata-withprettyprinting-withstartnodekey-withmaxlevel.json"), StandardCharsets.UTF_8); final var actual = writer.toString(); assertEquals(expected, actual); } }
@Test public void testJsonDocumentWithMetadataAndPrettyPrintingAndObjectStartNodeKeyAndMaxLevelThree() throws IOException { JsonTestHelper.createTestDocument(); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE); final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).withMetaData(true) .startNodeKey(15) .maxLevel(3) .prettyPrint() .build(); serializer.call(); final var expected = Files.readString(JSON.resolve("test-withmetadata-withprettyprinting-withstartnodekey-withmaxlevel3.json"), StandardCharsets.UTF_8); final var actual = writer.toString(); assertEquals(expected, actual); } }
@Test public void testJsonDocumentWithMetadataAndMaxLevelSecondAndPrettyPrinting() throws IOException { final var jsonPath = JSON.resolve("simple-testdoc.json"); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE); final var trx = manager.beginNodeTrx(); final Writer writer = new StringWriter()) { final var shredder = new JsonShredder.Builder(trx, JsonShredder.createFileReader(jsonPath), InsertPosition.AS_FIRST_CHILD).commitAfterwards().build(); shredder.call(); final var serializer = new JsonSerializer.Builder(manager, writer).withMetaData(true).maxLevel(3).prettyPrint().build(); serializer.call(); final var expected = Files.readString(JSON.resolve("simple-testdoc-withmetadata-withmaxlevel-withprettyprint.json"), StandardCharsets.UTF_8); final var actual = writer.toString(); JSONAssert.assertEquals(expected, actual, true); } }
@Test public void testJsonDocumentWithMaxLevel() throws IOException { JsonTestHelper.createTestDocument(); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE)) { try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(1).build(); serializer.call(); final var expected = "{}"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(2).build(); serializer.call(); final var expected = "{\"foo\":[],\"bar\":{},\"baz\":\"hello\",\"tada\":[]}"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(3).build(); serializer.call(); final var expected = "{\"foo\":[\"bar\",null,2.33],\"bar\":{\"hello\":\"world\",\"helloo\":true},\"baz\":\"hello\",\"tada\":[{},{},\"boo\",{},[]]}"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(4).build(); serializer.call(); final var expected = "{\"foo\":[\"bar\",null,2.33],\"bar\":{\"hello\":\"world\",\"helloo\":true},\"baz\":\"hello\",\"tada\":[{\"foo\":\"bar\"},{\"baz\":false},\"boo\",{},[]]}"; assertEquals(expected, writer.toString()); } } }
@Test public void testJsonDocumentWithMaxLevelAndStartNodeKey() throws IOException { JsonTestHelper.createTestDocument(); final var database = JsonTestHelper.getDatabase(PATHS.PATH1.getFile()); try (final var manager = database.openResourceManager(JsonTestHelper.RESOURCE)) { try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(1).startNodeKey(2).build(); serializer.call(); final var expected = "{\"foo\":[]}"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(1).startNodeKey(7).build(); serializer.call(); final var expected = "{\"bar\":{}}"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(2).startNodeKey(7).build(); serializer.call(); final var expected = "{\"bar\":{\"hello\":\"world\",\"helloo\":true}}"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(2).startNodeKey(17).build(); serializer.call(); final var expected = "{\"foo\":\"bar\"}"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(2).startNodeKey(16).build(); serializer.call(); final var expected = "[{},{},\"boo\",{},[]]"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(3).startNodeKey(16).build(); serializer.call(); final var expected = "[{\"foo\":\"bar\"},{\"baz\":false},\"boo\",{},[]]"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(1).startNodeKey(3).build(); serializer.call(); final var expected = "[]"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(1).startNodeKey(4).build(); serializer.call(); final var expected = "\"bar\""; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(1).startNodeKey(5).build(); serializer.call(); final var expected = "null"; assertEquals(expected, writer.toString()); } try (final Writer writer = new StringWriter()) { final var serializer = new JsonSerializer.Builder(manager, writer).maxLevel(1).startNodeKey(6).build(); serializer.call(); final var expected = "2.33"; assertEquals(expected, writer.toString()); } } }