* Values of leaf and leaf-list are NOT translated according to codecs.
- *
*/
-public final class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
+public abstract class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter,
+ StreamWriterMountPointExtension {
+ private static final class Exclusive extends JSONNormalizedNodeStreamWriter {
+ Exclusive(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
+ final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
+ super(codecFactory, tracker, writer, rootContext);
+ }
+
+ @Override
+ public void close() throws IOException {
+ flush();
+ closeWriter();
+ }
+ }
+
+ private static final class Nested extends JSONNormalizedNodeStreamWriter {
+ Nested(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
+ final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
+ super(codecFactory, tracker, writer, rootContext);
+ }
+
+ @Override
+ public void close() throws IOException {
+ flush();
+ // The caller "owns" the writer, let them close it
+ }
+ }
+
/**
* RFC6020 deviation: we are not required to emit empty containers unless they
* are marked as 'presence'.
*/
private static final boolean DEFAULT_EMIT_EMPTY_CONTAINERS = true;
- @RegEx
+ @Regex
private static final String NUMBER_STRING = "-?\\d+(\\.\\d+)?";
private static final Pattern NUMBER_PATTERN = Pattern.compile(NUMBER_STRING);
- @RegEx
+ @Regex
private static final String NOT_DECIMAL_NUMBER_STRING = "-?\\d+";
private static final Pattern NOT_DECIMAL_NUMBER_PATTERN = Pattern.compile(NOT_DECIMAL_NUMBER_STRING);
- private final SchemaTracker tracker;
+ private final NormalizedNodeStreamWriterStack tracker;
private final JSONCodecFactory codecs;
private final JsonWriter writer;
private JSONStreamWriterContext context;
- private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final JsonWriter JsonWriter, final JSONStreamWriterRootContext rootContext) {
- this.writer = Preconditions.checkNotNull(JsonWriter);
- this.codecs = Preconditions.checkNotNull(codecFactory);
- this.tracker = SchemaTracker.create(codecFactory.getSchemaContext(), path);
- this.context = Preconditions.checkNotNull(rootContext);
+ JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final NormalizedNodeStreamWriterStack tracker,
+ final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
+ this.writer = requireNonNull(writer);
+ this.codecs = requireNonNull(codecFactory);
+ this.tracker = requireNonNull(tracker);
+ this.context = requireNonNull(rootContext);
}
/**
* Create a new stream writer, which writes to the specified output stream.
*
+ *
* The codec factory can be reused between multiple writers.
*
+ *
* Returned writer is exclusive user of JsonWriter, which means it will start
* top-level JSON element and ends it.
*
+ *
* This instance of writer can be used only to emit one top level element,
- * otherwise it will produce incorrect JSON.
+ * otherwise it will produce incorrect JSON. Closing this instance will close
+ * the writer too.
*
* @param codecFactory JSON codec factory
* @param path Schema Path
@@ -83,19 +127,106 @@ public final class JSONNormalizedNodeStreamWriter implements NormalizedNodeStrea
* @param jsonWriter JsonWriter
* @return A stream writer instance
*/
- public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
- return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
+ public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+ final SchemaPath path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Exclusive(codecFactory,
+ NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext(), path), jsonWriter,
+ new JSONStreamWriterExclusiveRootContext(initialNs));
}
/**
* Create a new stream writer, which writes to the specified output stream.
*
+ *
* The codec factory can be reused between multiple writers.
*
+ *
+ * Returned writer is exclusive user of JsonWriter, which means it will start
+ * top-level JSON element and ends it.
+ *
+ *
+ * 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 rootNode Root node inference
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+ final EffectiveStatementInference rootNode, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Exclusive(codecFactory, NormalizedNodeStreamWriterStack.of(rootNode), jsonWriter,
+ new JSONStreamWriterExclusiveRootContext(initialNs));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ *
+ * The codec factory can be reused between multiple writers.
+ *
+ *
+ * Returned writer is exclusive user of JsonWriter, which means it will start
+ * top-level JSON element and ends it.
+ *
+ *
+ * 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 path Schema Path
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+ final Absolute path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Exclusive(codecFactory,
+ NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext(), path), jsonWriter,
+ new JSONStreamWriterExclusiveRootContext(initialNs));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ *
+ * The codec factory can be reused between multiple writers.
+ *
+ *
+ * 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 will not
+ * close the wrapped writer; the caller must take care of that.
+ *
+ * @param codecFactory JSON codec factory
+ * @param path Schema Path
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+ final SchemaPath path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Nested(codecFactory,
+ NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext(), path), jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ *
+ * The codec factory can be reused between multiple writers.
+ *
+ *
* 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.
+ * JSON to be valid. Closing this instance will not
+ * close the wrapped writer; the caller must take care of that.
*
* @param codecFactory JSON codec factory
* @param path Schema Path
@@ -103,35 +234,64 @@ public final class JSONNormalizedNodeStreamWriter implements NormalizedNodeStrea
* @param jsonWriter JsonWriter
* @return A stream writer instance
*/
- public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
- return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
+ public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+ final Absolute path, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Nested(codecFactory,
+ NormalizedNodeStreamWriterStack.of(codecFactory.getEffectiveModelContext(), path), jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ *
+ * The codec factory can be reused between multiple writers.
+ *
+ *
+ * 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 will not
+ * close the wrapped writer; the caller must take care of that.
+ *
+ * @param codecFactory JSON codec factory
+ * @param rootNode Root node inference
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+ final EffectiveStatementInference rootNode, final XMLNamespace initialNs, final JsonWriter jsonWriter) {
+ return new Nested(codecFactory, NormalizedNodeStreamWriterStack.of(rootNode), jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
+ }
+
+ @Override
+ public ClassToInstanceMap getExtensions() {
+ return ImmutableClassToInstanceMap.of(StreamWriterMountPointExtension.class, this);
}
@Override
- public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
- final LeafSchemaNode schema = tracker.leafNode(name);
- final JSONCodec> codec = codecs.codecFor(schema);
- context.emittingChild(codecs.getSchemaContext(), writer);
- context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
- writeValue(value, codec);
+ public void startLeafNode(final NodeIdentifier name) throws IOException {
+ tracker.startLeafNode(name);
+ context.emittingChild(codecs.getEffectiveModelContext(), writer);
+ context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
}
@Override
- public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
+ public final void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
tracker.startLeafSet(name);
context = new JSONStreamWriterListContext(context, name);
}
@Override
- public void leafSetEntryNode(final QName name, final Object value) throws IOException {
- final LeafListSchemaNode schema = tracker.leafSetEntryNode(name);
- final JSONCodec> codec = codecs.codecFor(schema);
- context.emittingChild(codecs.getSchemaContext(), writer);
- writeValue(value, codec);
+ public void startLeafSetEntryNode(final NodeWithValue> name) throws IOException {
+ tracker.startLeafSetEntryNode(name);
+ context.emittingChild(codecs.getEffectiveModelContext(), writer);
}
@Override
- public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
+ public final void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
tracker.startLeafSet(name);
context = new JSONStreamWriterListContext(context, name);
}
@@ -140,195 +300,281 @@ public final class JSONNormalizedNodeStreamWriter implements NormalizedNodeStrea
* Warning suppressed due to static final constant which triggers a warning
* for the call to schema.isPresenceContainer().
*/
- @SuppressWarnings("unused")
@Override
- public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
+ public final void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
final SchemaNode schema = tracker.startContainerNode(name);
-
- // FIXME this code ignores presence for containers
- // but datastore does as well and it needs be fixed first (2399)
- context = new JSONStreamWriterNamedObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
+ final boolean isPresence = schema instanceof ContainerSchemaNode
+ ? ((ContainerSchemaNode) schema).isPresenceContainer() : DEFAULT_EMIT_EMPTY_CONTAINERS;
+ context = new JSONStreamWriterNamedObjectContext(context, name, isPresence);
}
@Override
- public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
+ public final void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
tracker.startList(name);
context = new JSONStreamWriterListContext(context, name);
}
@Override
- public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
+ public final void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
tracker.startListItem(name);
context = new JSONStreamWriterObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
}
@Override
- public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
+ public final void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
tracker.startList(name);
context = new JSONStreamWriterListContext(context, name);
}
@Override
- public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
+ public final void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
throws IOException {
tracker.startListItem(identifier);
context = new JSONStreamWriterObjectContext(context, identifier, DEFAULT_EMIT_EMPTY_CONTAINERS);
}
@Override
- public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
+ public final void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
tracker.startList(name);
context = new JSONStreamWriterListContext(context, name);
}
@Override
- public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
+ public final void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
tracker.startChoiceNode(name);
context = new JSONStreamWriterInvisibleContext(context);
}
@Override
- public void startAugmentationNode(final AugmentationIdentifier identifier) {
+ public final void startAugmentationNode(final AugmentationIdentifier identifier) {
tracker.startAugmentationNode(identifier);
context = new JSONStreamWriterInvisibleContext(context);
}
@Override
- public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
- @SuppressWarnings("unused")
- final AnyXmlSchemaNode schema = tracker.anyxmlNode(name);
- // FIXME: should have a codec based on this :)
+ public final boolean startAnydataNode(final NodeIdentifier name, final Class> objectModel) throws IOException {
+ if (NormalizedAnydata.class.isAssignableFrom(objectModel)) {
+ tracker.startAnydataNode(name);
+ context.emittingChild(codecs.getEffectiveModelContext(), writer);
+ context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
+ return true;
+ }
- context.emittingChild(codecs.getSchemaContext(), writer);
- context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
+ return false;
+ }
- writeAnyXmlValue((DOMSource) value);
+ @Override
+ public final NormalizedNodeStreamWriter startMountPoint(final MountPointIdentifier mountId,
+ final MountPointContext mountCtx) throws IOException {
+ final EffectiveModelContext ctx = mountCtx.getEffectiveModelContext();
+ return new Nested(codecs.rebaseTo(ctx), NormalizedNodeStreamWriterStack.of(ctx), writer,
+ new JSONStreamWriterSharedRootContext(context.getNamespace()));
}
@Override
- public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
- tracker.startYangModeledAnyXmlNode(name);
- context = new JSONStreamWriterNamedObjectContext(context, name, true);
+ public final boolean startAnyxmlNode(final NodeIdentifier name, final Class> objectModel) throws IOException {
+ if (DOMSource.class.isAssignableFrom(objectModel)) {
+ tracker.startAnyxmlNode(name);
+ context.emittingChild(codecs.getEffectiveModelContext(), writer);
+ context.writeChildJsonIdentifier(codecs.getEffectiveModelContext(), writer, name.getNodeType());
+ return true;
+ }
+ return false;
}
@Override
- public void endNode() throws IOException {
+ public final void endNode() throws IOException {
tracker.endNode();
- context = context.endNode(codecs.getSchemaContext(), writer);
+ context = context.endNode(codecs.getEffectiveModelContext(), writer);
+ }
+
+ @Override
+ public final void flush() throws IOException {
+ writer.flush();
+ }
- if (context instanceof JSONStreamWriterRootContext) {
- context.emitEnd(writer);
+ final void closeWriter() throws IOException {
+ if (!(context instanceof JSONStreamWriterRootContext)) {
+ throw new IOException("Unexpected root context " + context);
}
+
+ context.endNode(codecs.getEffectiveModelContext(), writer);
+ writer.close();
}
- private void writeValue(final Object value, final JSONCodec> codec)
- throws IOException {
- ((JSONCodec