import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadXmlToNormalizedNodes;
import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.normalizedNodesToJsonString;
-import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import java.io.File;
final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext,
SchemaPath.ROOT);
- final JsonParser parser = new JsonParser();
- final JsonElement expected = parser.parse(inputJson);
- final JsonElement actual = parser.parse(serializationResult);
- assertTrue(expected.equals(actual));
+ assertEquals(JsonParser.parseString(inputJson), JsonParser.parseString(serializationResult));
}
@Test
final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext,
SchemaPath.ROOT);
- final JsonParser parser = new JsonParser();
- final String expectedJson = loadTextFile("/bug8927/json/composite.json");
- final JsonElement expected = parser.parse(expectedJson);
- final JsonElement actual = parser.parse(serializationResult);
- assertTrue(expected.equals(actual));
+ assertEquals(JsonParser.parseString(loadTextFile("/bug8927/json/composite.json")),
+ JsonParser.parseString(serializationResult));
}
@Test
.childByArg(new NodeIdentifier(QName.create("bug8927.test", "2017-01-01", "foo")));
assertNotNull(data);
final String jsonOutput = normalizedNodesToJsonString(data, schemaContext, SchemaPath.ROOT);
- final JsonParser parser = new JsonParser();
- final JsonElement expextedJson = parser.parse(new FileReader(
- new File(getClass().getResource(expectedJsonFile).toURI()), StandardCharsets.UTF_8));
- final JsonElement serializedJson = parser.parse(jsonOutput);
- assertEquals(expextedJson, serializedJson);
+ assertEquals(JsonParser.parseReader(new FileReader(
+ new File(getClass().getResource(expectedJsonFile).toURI()), StandardCharsets.UTF_8)),
+ JsonParser.parseString(jsonOutput));
}
private static DOMSource getParsedAnyXmlValue(final NormalizedNode transformedInput, final QName anyxmlName) {
final Writer writer = new StringWriter();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, rootNode);
- final JsonParser parser = new JsonParser();
- final JsonElement serializedJson = parser.parse(jsonOutput);
- final JsonElement expextedJson = parser.parse(new FileReader(new File(getClass().getResource(
+ final JsonElement serializedJson = JsonParser.parseString(jsonOutput);
+ final JsonElement expextedJson = JsonParser.parseReader(new FileReader(new File(getClass().getResource(
"/bug5446/json/foo.json").toURI()), StandardCharsets.UTF_8));
assertEquals(expextedJson, serializedJson);
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), SchemaPath.ROOT, null,
JsonWriterFactory.createJsonWriter(writer, 2));
- final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
- nodeWriter.write(inputStructure);
+ try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
+ nodeWriter.write(inputStructure);
+ }
- nodeWriter.close();
return writer.toString();
}
import static org.junit.Assert.assertEquals;
-import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.FileReader;
public void test() throws Exception {
final EffectiveModelContext schemaContext =
YangParserTestUtils.parseYangResource("/bug7246/yang/rpc-test.yang");
- final JsonParser parser = new JsonParser();
- final JsonElement expextedJson = parser
- .parse(new FileReader(new File(getClass().getResource("/bug7246/json/expected-output.json").toURI()),
- StandardCharsets.UTF_8));
-
final ContainerNode inputStructure = ImmutableContainerNodeBuilder.create()
.withNodeIdentifier(new NodeIdentifier(qN("my-name")))
.withChild(ImmutableNodes.leafNode(new NodeIdentifier(qN("my-name")), "my-value")).build();
final Writer writer = new StringWriter();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(schemaContext, rootPath, writer,
inputStructure);
- final JsonElement serializedJson = parser.parse(jsonOutput);
- assertEquals(expextedJson, serializedJson);
+ assertEquals(JsonParser.parseReader(new FileReader(
+ new File(getClass().getResource("/bug7246/json/expected-output.json").toURI()), StandardCharsets.UTF_8)),
+ JsonParser.parseString(jsonOutput));
}
private static QName qN(final String localName) {
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), path,
XMLNamespace.of(NS), JsonWriterFactory.createJsonWriter(writer, 2));
- final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
- nodeWriter.write(inputStructure);
-
- nodeWriter.close();
+ try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
+ nodeWriter.write(inputStructure);
+ }
return writer.toString();
}
}
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), rootPath, null,
JsonWriterFactory.createJsonWriter(writer, 2));
- final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
- nodeWriter.write(data);
- nodeWriter.close();
- final String serializationResult = writer.toString();
- return serializationResult;
+ try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
+ nodeWriter.write(data);
+ }
+ return writer.toString();
}
static JsonObject childObject(final JsonObject jsonObject, final String... names) {
}
static JsonObject resolveCont1(final String jsonOutput) {
- final JsonElement rootElement = new JsonParser().parse(jsonOutput);
+ final JsonElement rootElement = JsonParser.parseString(jsonOutput);
assertTrue(rootElement.isJsonObject());
final JsonObject rootObject = rootElement.getAsJsonObject();
final JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");
}
static JsonObject resolveCont2(final String jsonOutput) {
- final JsonElement rootElement = new JsonParser().parse(jsonOutput);
+ final JsonElement rootElement = JsonParser.parseString(jsonOutput);
assertTrue(rootElement.isJsonObject());
final JsonObject rootObject = rootElement.getAsJsonObject();
final JsonObject cont2 = childObject(rootObject, "complexjson:cont2", "cont2");