X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-codec-gson%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fcodec%2Fgson%2FJSONNormalizedNodeStreamWriter.java;h=e5dc228b334a3dd88802f2a74d56cbb5c916a652;hb=f7fe89b85f540dbe7d1fc051f2082f2ce571321a;hp=57204c838aba86eee96f3f675090c8b9bb03d5bd;hpb=34463893cd60ddb660a7276acb98207583c563af;p=yangtools.git diff --git a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java index 57204c838a..e5dc228b33 100644 --- a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java +++ b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONNormalizedNodeStreamWriter.java @@ -10,8 +10,8 @@ package org.opendaylight.yangtools.yang.data.codec.gson; import com.google.common.base.Preconditions; import com.google.gson.stream.JsonWriter; import java.io.IOException; -import java.io.Writer; import java.net.URI; +import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; @@ -20,7 +20,6 @@ import org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker; import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; @@ -30,7 +29,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath; * Values of leaf and leaf-list are NOT translated according to codecs. * */ -public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter { +public final class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter { /** * RFC6020 deviation: we are not required to emit empty containers unless they * are marked as 'presence'. @@ -42,107 +41,13 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite private final JsonWriter writer; private JSONStreamWriterContext context; - private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, JsonWriter JsonWriter, JSONStreamWriterRootContext rootContext) { + 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); } - /** - * Create a new stream writer, which writes to the specified {@link Writer}. - * - * This instance of writer can be used only to emit one top level element, - * therwise it will produce incorrect JSON. - * - * @param schemaContext Schema context - * @param writer Output writer - * @return A stream writer instance - */ - public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer) { - return create(schemaContext, SchemaPath.ROOT, null, writer); - } - - /** - * Create a new stream writer, which writes to the specified {@link Writer}. - * - * This instance of writer can be used only to emit one top level element, - * therwise it will produce incorrect JSON. - * - * @param schemaContext Schema context - * @param path Root schemapath - * @param writer Output writer - * @return A stream writer instance - */ - public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, final Writer writer) { - return create(schemaContext, path, null, writer); - } - - /** - * Create a new stream writer, which writes to the specified {@link Writer}. - * - * This instance of writer can be used only to emit one top level element, - * therwise it will produce incorrect JSON. - * - * @param schemaContext Schema context - * @param path Root schemapath - * @param writer Output writer - * @param initialNs Initial namespace - * @return A stream writer instance - */ - public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, - final URI initialNs, final Writer writer) { - return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, JsonWriterFactory.createJsonWriter(writer)); - } - - /** - * Create a new stream writer, which writes to the specified output stream. - * - * This instance of writer can be used only to emit one top level element, - * therwise it will produce incorrect JSON. - * - * @param schemaContext Schema context - * @param writer Output writer - * @param indentSize indentation size - * @return A stream writer instance - */ - public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer, final int indentSize) { - return createExclusiveWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,JsonWriterFactory.createJsonWriter(writer, indentSize)); - } - - /** - * Create a new stream writer, which writes to the specified output stream. The codec factory - * can be reused between multiple writers. - * - * This instance of writer can be used only to emit one top level element, - * therwise it will produce incorrect JSON. - * - * @param codecFactory JSON codec factory - * @param writer Output writer - * @param indentSize indentation size - * @return A stream writer instance - */ - public static NormalizedNodeStreamWriter create(final JSONCodecFactory codecFactory, final Writer writer, final int indentSize) { - return createExclusiveWriter(codecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer,indentSize)); - } - - /** - * Create a new stream writer, which writes to the specified output stream. - * - * This instance of writer can be used only to emit one top level element, - * therwise it will produce incorrect JSON. - * - * @param schemaContext Schema context - * @param path Schema Path - * @param initialNs Initial namespace - * @param jsonWriter JsonWriter - * @return A stream writer instance - */ - public static NormalizedNodeStreamWriter create(SchemaContext schemaContext, SchemaPath path, URI initialNs, - JsonWriter jsonWriter) { - return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, jsonWriter); - } - /** * Create a new stream writer, which writes to the specified output stream. * @@ -152,7 +57,7 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite * top-level JSON element and ends it. * * This instance of writer can be used only to emit one top level element, - * therwise it will produce incorrect JSON. + * otherwise it will produce incorrect JSON. * * @param codecFactory JSON codec factory * @param path Schema Path @@ -160,7 +65,7 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite * @param jsonWriter JsonWriter * @return A stream writer instance */ - public static NormalizedNodeStreamWriter createExclusiveWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) { + 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)); } @@ -180,18 +85,16 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite * @param jsonWriter JsonWriter * @return A stream writer instance */ - public static NormalizedNodeStreamWriter createNestedWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) { + 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)); } @Override public void leafNode(final NodeIdentifier name, final Object value) throws IOException { final LeafSchemaNode schema = tracker.leafNode(name); - final JSONCodec codec = codecs.codecFor(schema.getType()); - + final JSONCodec codec = codecs.codecFor(schema); context.emittingChild(codecs.getSchemaContext(), writer); context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType()); - writeValue(value, codec); } @@ -202,15 +105,19 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite } @Override - public void leafSetEntryNode(final Object value) throws IOException { + public void leafSetEntryNode(final QName name, final Object value) throws IOException { final LeafListSchemaNode schema = tracker.leafSetEntryNode(); - final JSONCodec codec = codecs.codecFor(schema.getType()); - + final JSONCodec codec = codecs.codecFor(schema); context.emittingChild(codecs.getSchemaContext(), writer); - writeValue(value, codec); } + @Override + public void startOrderedLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException { + tracker.startLeafSet(name); + context = new JSONStreamWriterListContext(context, name); + } + /* * Warning suppressed due to static final constant which triggers a warning * for the call to schema.isPresenceContainer(). @@ -280,6 +187,12 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite writer.value(String.valueOf(value)); } + @Override + public void startYangModeledAnyXmlNode(final NodeIdentifier name, final int childSizeHint) throws IOException { + tracker.startYangModeledAnyXmlNode(name); + context = new JSONStreamWriterNamedObjectContext(context, name, true); + } + @Override public void endNode() throws IOException { tracker.endNode(); @@ -290,7 +203,7 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite } } - private void writeValue(Object value, JSONCodec codec) + private void writeValue(final Object value, final JSONCodec codec) throws IOException { codec.serializeToWriter(writer,value); } @@ -302,10 +215,7 @@ public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWrite @Override public void close() throws IOException { - writer.flush(); + flush(); writer.close(); } - - - }