2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.codec.gson;
10 import com.google.common.base.Preconditions;
11 import com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
13 import java.io.Writer;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.data.impl.codec.SchemaTracker;
20 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
28 * This implementation will create JSON output as output stream.
30 * Values of leaf and leaf-list are NOT translated according to codecs.
33 public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
35 * RFC6020 deviation: we are not required to emit empty containers unless they
36 * are marked as 'presence'.
38 private static final boolean DEFAULT_EMIT_EMPTY_CONTAINERS = true;
40 private final SchemaTracker tracker;
41 private final JSONCodecFactory codecs;
42 private final JsonWriter writer;
43 private JSONStreamWriterContext context;
45 private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, JsonWriter JsonWriter, JSONStreamWriterRootContext rootContext) {
46 this.writer = Preconditions.checkNotNull(JsonWriter);
47 this.codecs = Preconditions.checkNotNull(codecFactory);
48 this.tracker = SchemaTracker.create(codecFactory.getSchemaContext(), path);
49 this.context = Preconditions.checkNotNull(rootContext);
53 * Create a new stream writer, which writes to the specified {@link Writer}.
55 * This instance of writer can be used only to emit one top level element,
56 * therwise it will produce incorrect JSON.
58 * @param schemaContext Schema context
59 * @param writer Output writer
60 * @return A stream writer instance
62 public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer) {
63 return create(schemaContext, SchemaPath.ROOT, null, writer);
67 * Create a new stream writer, which writes to the specified {@link Writer}.
69 * This instance of writer can be used only to emit one top level element,
70 * therwise it will produce incorrect JSON.
72 * @param schemaContext Schema context
73 * @param path Root schemapath
74 * @param writer Output writer
75 * @return A stream writer instance
77 public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, final Writer writer) {
78 return create(schemaContext, path, null, writer);
82 * Create a new stream writer, which writes to the specified {@link Writer}.
84 * This instance of writer can be used only to emit one top level element,
85 * therwise it will produce incorrect JSON.
87 * @param schemaContext Schema context
88 * @param path Root schemapath
89 * @param writer Output writer
90 * @param initialNs Initial namespace
91 * @return A stream writer instance
93 public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path,
94 final URI initialNs, final Writer writer) {
95 return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, JsonWriterFactory.createJsonWriter(writer));
99 * Create a new stream writer, which writes to the specified output stream.
101 * This instance of writer can be used only to emit one top level element,
102 * therwise it will produce incorrect JSON.
104 * @param schemaContext Schema context
105 * @param writer Output writer
106 * @param indentSize indentation size
107 * @return A stream writer instance
109 public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer, final int indentSize) {
110 return createExclusiveWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,JsonWriterFactory.createJsonWriter(writer, indentSize));
114 * Create a new stream writer, which writes to the specified output stream. The codec factory
115 * can be reused between multiple writers.
117 * This instance of writer can be used only to emit one top level element,
118 * therwise it will produce incorrect JSON.
120 * @param codecFactory JSON codec factory
121 * @param writer Output writer
122 * @param indentSize indentation size
123 * @return A stream writer instance
125 public static NormalizedNodeStreamWriter create(final JSONCodecFactory codecFactory, final Writer writer, final int indentSize) {
126 return createExclusiveWriter(codecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer,indentSize));
130 * Create a new stream writer, which writes to the specified output stream.
132 * This instance of writer can be used only to emit one top level element,
133 * therwise it will produce incorrect JSON.
135 * @param schemaContext Schema context
136 * @param path Schema Path
137 * @param initialNs Initial namespace
138 * @param jsonWriter JsonWriter
139 * @return A stream writer instance
141 public static NormalizedNodeStreamWriter create(SchemaContext schemaContext, SchemaPath path, URI initialNs,
142 JsonWriter jsonWriter) {
143 return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, jsonWriter);
147 * Create a new stream writer, which writes to the specified output stream.
149 * The codec factory can be reused between multiple writers.
151 * Returned writer is exclusive user of JsonWriter, which means it will start
152 * top-level JSON element and ends it.
154 * This instance of writer can be used only to emit one top level element,
155 * therwise it will produce incorrect JSON.
157 * @param codecFactory JSON codec factory
158 * @param path Schema Path
159 * @param initialNs Initial namespace
160 * @param jsonWriter JsonWriter
161 * @return A stream writer instance
163 public static NormalizedNodeStreamWriter createExclusiveWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
164 return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
168 * Create a new stream writer, which writes to the specified output stream.
170 * The codec factory can be reused between multiple writers.
172 * Returned writer can be used emit multiple top level element,
173 * but does not start / close parent JSON object, which must be done
174 * by user providing {@code jsonWriter} instance in order for
177 * @param codecFactory JSON codec factory
178 * @param path Schema Path
179 * @param initialNs Initial namespace
180 * @param jsonWriter JsonWriter
181 * @return A stream writer instance
183 public static NormalizedNodeStreamWriter createNestedWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
184 return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
188 public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
189 final LeafSchemaNode schema = tracker.leafNode(name);
190 final JSONCodec<Object> codec = codecs.codecFor(schema.getType());
192 context.emittingChild(codecs.getSchemaContext(), writer);
193 context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
195 writeValue(value, codec);
199 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
200 tracker.startLeafSet(name);
201 context = new JSONStreamWriterListContext(context, name);
205 public void leafSetEntryNode(final Object value) throws IOException {
206 final LeafListSchemaNode schema = tracker.leafSetEntryNode();
207 final JSONCodec<Object> codec = codecs.codecFor(schema.getType());
209 context.emittingChild(codecs.getSchemaContext(), writer);
211 writeValue(value, codec);
215 * Warning suppressed due to static final constant which triggers a warning
216 * for the call to schema.isPresenceContainer().
218 @SuppressWarnings("unused")
220 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
221 final SchemaNode schema = tracker.startContainerNode(name);
223 // FIXME this code ignores presence for containers
224 // but datastore does as well and it needs be fixed first (2399)
225 context = new JSONStreamWriterNamedObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
229 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
230 tracker.startList(name);
231 context = new JSONStreamWriterListContext(context, name);
235 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
236 tracker.startListItem(name);
237 context = new JSONStreamWriterObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
241 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
242 tracker.startList(name);
243 context = new JSONStreamWriterListContext(context, name);
247 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
249 tracker.startListItem(identifier);
250 context = new JSONStreamWriterObjectContext(context, identifier, DEFAULT_EMIT_EMPTY_CONTAINERS);
254 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
255 tracker.startList(name);
256 context = new JSONStreamWriterListContext(context, name);
260 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
261 tracker.startChoiceNode(name);
262 context = new JSONStreamWriterInvisibleContext(context);
266 public void startAugmentationNode(final AugmentationIdentifier identifier) {
267 tracker.startAugmentationNode(identifier);
268 context = new JSONStreamWriterInvisibleContext(context);
272 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
273 @SuppressWarnings("unused")
274 final AnyXmlSchemaNode schema = tracker.anyxmlNode(name);
275 // FIXME: should have a codec based on this :)
277 context.emittingChild(codecs.getSchemaContext(), writer);
278 context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
279 writer.value(String.valueOf(value));
283 public void endNode() throws IOException {
285 context = context.endNode(codecs.getSchemaContext(), writer);
287 if(context instanceof JSONStreamWriterRootContext) {
288 context.emitEnd(writer);
292 private void writeValue(Object value, JSONCodec<Object> codec)
294 codec.serializeToWriter(writer,value);
298 public void flush() throws IOException {
303 public void close() throws IOException {