import java.util.regex.Pattern;
import javax.xml.transform.dom.DOMSource;
import org.checkerframework.checker.regex.qual.Regex;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
import org.opendaylight.yangtools.rfc8528.data.api.StreamWriterMountPointExtension;
context = requireNonNull(rootContext);
}
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * <p>
+ * The codec factory can be reused between multiple writers.
+ *
+ * <p>
+ * Returned writer is exclusive user of JsonWriter, which means it will start
+ * top-level JSON element and ends it.
+ *
+ * <p>
+ * This instance of writer can be used only to emit one top level element,
+ * otherwise it will produce incorrect JSON. Closing this instance will close
+ * the writer too.
+ *
+ * @param codecFactory JSON codec factory
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+ final JsonWriter jsonWriter) {
+ return createExclusiveWriter(codecFactory, jsonWriter, null);
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * <p>
+ * The codec factory can be reused between multiple writers.
+ *
+ * <p>
+ * Returned writer is exclusive user of JsonWriter, which means it will start
+ * top-level JSON element and ends it.
+ *
+ * <p>
+ * This instance of writer can be used only to emit one top level element,
+ * otherwise it will produce incorrect JSON. Closing this instance will close
+ * the writer too.
+ *
+ * @param codecFactory JSON codec factory
+ * @param jsonWriter JsonWriter
+ * @param initialNs Initial namespace, can be null
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+ final JsonWriter jsonWriter, final @Nullable XMLNamespace initialNs) {
+ return new Exclusive(codecFactory, NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext()),
+ jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
+ }
+
/**
* Create a new stream writer, which writes to the specified output stream.
*
* @param initialNs Initial namespace
* @param jsonWriter JsonWriter
* @return A stream writer instance
+ * @deprecated Use one of the alternative overloads
*/
+ @Deprecated(since = "9.0.1", forRemoval = true)
public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
final SchemaPath path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
return new Exclusive(codecFactory,
new JSONStreamWriterExclusiveRootContext(initialNs));
}
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * <p>
+ * The codec factory can be reused between multiple writers.
+ *
+ * <p>
+ * Returned writer can be used emit multiple top level element,
+ * but does not start / close parent JSON object, which must be done
+ * by user providing {@code jsonWriter} instance in order for
+ * JSON to be valid. Closing this instance <strong>will not</strong>
+ * close the wrapped writer; the caller must take care of that.
+ *
+ * @param codecFactory JSON codec factory
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+ final JsonWriter jsonWriter) {
+ return createNestedWriter(codecFactory, jsonWriter, null);
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * <p>
+ * The codec factory can be reused between multiple writers.
+ *
+ * <p>
+ * Returned writer can be used emit multiple top level element,
+ * but does not start / close parent JSON object, which must be done
+ * by user providing {@code jsonWriter} instance in order for
+ * JSON to be valid. Closing this instance <strong>will not</strong>
+ * close the wrapped writer; the caller must take care of that.
+ *
+ * @param codecFactory JSON codec factory
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+ final JsonWriter jsonWriter, final @Nullable XMLNamespace initialNs) {
+ return new Nested(codecFactory,
+ NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext()), jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
+ }
+
/**
* Create a new stream writer, which writes to the specified output stream.
*
* @param initialNs Initial namespace
* @param jsonWriter JsonWriter
* @return A stream writer instance
+ * @deprecated Use one of the alternative overloads
*/
+ @Deprecated(since = "9.0.1", forRemoval = true)
public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
final SchemaPath path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
return new Nested(codecFactory,
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
private String toJSON(final NormalizedNode input) throws IOException {
final Writer writer = new StringWriter();
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
- codecFactory(), SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
+ codecFactory(), JsonWriterFactory.createJsonWriter(writer, 2));
try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
nodeWriter.write(input);
}
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
final DOMSource Lf14AnyExpectedValue = createAnyXmlSimpleValue("ns:complex:json", "lf14-any", "null");
verifyTransformedAnyXmlNodeValue(Lf14AnyExpectedValue, Lf14AnyActualValue);
- final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext,
- SchemaPath.ROOT);
+ final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext);
assertEquals(JsonParser.parseString(inputJson), JsonParser.parseString(serializationResult));
}
verifyTransformedAnyXmlNodeValue(Lf14AnyExpectedValue, Lf14AnyActualValue);
// serialization
- final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext,
- SchemaPath.ROOT);
+ final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext);
assertEquals(JsonParser.parseString(loadTextFile("/bug8927/json/composite.json")),
JsonParser.parseString(serializationResult));
final DataContainerChild data = ((ContainerNode) result.getResult())
.childByArg(new NodeIdentifier(QName.create("bug8927.test", "2017-01-01", "foo")));
assertNotNull(data);
- final String jsonOutput = normalizedNodesToJsonString(data, schemaContext, SchemaPath.ROOT);
+ final String jsonOutput = normalizedNodesToJsonString(data, schemaContext);
assertEquals(JsonParser.parseReader(new FileReader(
new File(getClass().getResource(expectedJsonFile).toURI()), StandardCharsets.UTF_8)),
JsonParser.parseString(jsonOutput));
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class Bug5446Test {
final NormalizedNode inputStructure) throws IOException {
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
- JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), SchemaPath.ROOT, null,
+ JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext),
JsonWriterFactory.createJsonWriter(writer, 2));
try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
nodeWriter.write(inputStructure);
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class Bug7246Test {
final ContainerNode inputStructure = ImmutableContainerNodeBuilder.create()
.withNodeIdentifier(new NodeIdentifier(qN("my-name")))
.withChild(ImmutableNodes.leafNode(new NodeIdentifier(qN("my-name")), "my-value")).build();
- final SchemaPath rootPath = SchemaPath.create(true, qN("my-name"), qN("input"));
final Writer writer = new StringWriter();
- final String jsonOutput = normalizedNodeToJsonStreamTransformation(schemaContext, rootPath, writer,
- inputStructure);
+ final String jsonOutput = normalizedNodeToJsonStreamTransformation(schemaContext, writer, inputStructure,
+ qN("my-name"), qN("input"));
assertEquals(JsonParser.parseReader(new FileReader(
new File(getClass().getResource("/bug7246/json/expected-output.json").toURI()), StandardCharsets.UTF_8)),
}
private static String normalizedNodeToJsonStreamTransformation(final EffectiveModelContext schemaContext,
- final SchemaPath path, final Writer writer, final NormalizedNode inputStructure)
- throws IOException {
-
+ final Writer writer, final NormalizedNode inputStructure, final QName... path) throws IOException {
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
- JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), path,
+ JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), Absolute.of(path),
XMLNamespace.of(NS), JsonWriterFactory.createJsonWriter(writer, 2));
try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
nodeWriter.write(inputStructure);
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
/**
* Each test tests whether json output obtained after transformation contains is corect. The transformation takes
throws IOException {
final Writer writer = new StringWriter();
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
- lhotkaCodecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
+ lhotkaCodecFactory, JsonWriterFactory.createJsonWriter(writer, 2));
try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
nodeWriter.write(inputStructure);
}
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// StreamWriter which outputs JSON strings
// StreamWriter which outputs JSON strings
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
- lhotkaCodecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
+ lhotkaCodecFactory, JsonWriterFactory.createJsonWriter(writer, 2));
// NormalizedNode -> StreamWriter
final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
public final class TestUtils {
private TestUtils() {
}
static String normalizedNodesToJsonString(final NormalizedNode data,
- final EffectiveModelContext schemaContext, final SchemaPath rootPath) throws IOException {
+ final EffectiveModelContext schemaContext) throws IOException {
final Writer writer = new StringWriter();
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
- JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), rootPath, null,
+ JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext),
JsonWriterFactory.createJsonWriter(writer, 2));
try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
nodeWriter.write(data);
import org.junit.Test;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
public class YT1029Test extends AbstractComplexJsonTest {
@Test
public void testMultipleRootChildren() throws IOException {
final Writer writer = new StringWriter();
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
- lhotkaCodecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
+ lhotkaCodecFactory, JsonWriterFactory.createJsonWriter(writer, 2));
try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
nodeWriter.write(CONT1_WITH_EMPTYLEAF);
nodeWriter.write(CONT1_WITH_EMPTYLEAF);