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 * otherwise it will produce incorrect JSON.
58 * @deprecated please use
59 * {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
60 * {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
61 * appropriate instead.
63 * @param schemaContext Schema context
64 * @param writer Output writer
65 * @return A stream writer instance
68 public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer) {
69 return create(schemaContext, SchemaPath.ROOT, null, writer);
73 * Create a new stream writer, which writes to the specified {@link Writer}.
75 * This instance of writer can be used only to emit one top level element,
76 * otherwise it will produce incorrect JSON.
78 * @deprecated please use
79 * {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
80 * {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
81 * appropriate instead.
83 * @param schemaContext Schema context
84 * @param path Root schemapath
85 * @param writer Output writer
86 * @return A stream writer instance
89 public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, final Writer writer) {
90 return create(schemaContext, path, null, writer);
94 * Create a new stream writer, which writes to the specified {@link Writer}.
96 * This instance of writer can be used only to emit one top level element,
97 * otherwise it will produce incorrect JSON.
99 * @deprecated please use
100 * {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
101 * {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
102 * appropriate instead.
104 * @param schemaContext Schema context
105 * @param path Root schemapath
106 * @param writer Output writer
107 * @param initialNs Initial namespace
108 * @return A stream writer instance
111 public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path,
112 final URI initialNs, final Writer writer) {
113 return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, JsonWriterFactory.createJsonWriter(writer));
117 * Create a new stream writer, which writes to the specified output stream.
119 * This instance of writer can be used only to emit one top level element,
120 * otherwise it will produce incorrect JSON.
122 * @deprecated please use
123 * {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
124 * {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
125 * appropriate instead.
127 * @param schemaContext Schema context
128 * @param writer Output writer
129 * @param indentSize indentation size
130 * @return A stream writer instance
133 public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer, final int indentSize) {
134 return createExclusiveWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,JsonWriterFactory.createJsonWriter(writer, indentSize));
138 * Create a new stream writer, which writes to the specified output stream. The codec factory
139 * can be reused between multiple writers.
141 * This instance of writer can be used only to emit one top level element,
142 * otherwise it will produce incorrect JSON.
144 * @deprecated please use
145 * {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
146 * {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
147 * appropriate instead.
149 * @param codecFactory JSON codec factory
150 * @param writer Output writer
151 * @param indentSize indentation size
152 * @return A stream writer instance
155 public static NormalizedNodeStreamWriter create(final JSONCodecFactory codecFactory, final Writer writer, final int indentSize) {
156 return createExclusiveWriter(codecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer,indentSize));
160 * Create a new stream writer, which writes to the specified output stream.
162 * This instance of writer can be used only to emit one top level element,
163 * otherwise it will produce incorrect JSON.
165 * @deprecated please use
166 * {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
167 * {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
168 * appropriate instead.
170 * @param schemaContext Schema context
171 * @param path Schema Path
172 * @param initialNs Initial namespace
173 * @param jsonWriter JsonWriter
174 * @return A stream writer instance
177 public static NormalizedNodeStreamWriter create(SchemaContext schemaContext, SchemaPath path, URI initialNs,
178 JsonWriter jsonWriter) {
179 return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, jsonWriter);
183 * Create a new stream writer, which writes to the specified output stream.
185 * The codec factory can be reused between multiple writers.
187 * Returned writer is exclusive user of JsonWriter, which means it will start
188 * top-level JSON element and ends it.
190 * This instance of writer can be used only to emit one top level element,
191 * otherwise it will produce incorrect JSON.
193 * @param codecFactory JSON codec factory
194 * @param path Schema Path
195 * @param initialNs Initial namespace
196 * @param jsonWriter JsonWriter
197 * @return A stream writer instance
199 public static NormalizedNodeStreamWriter createExclusiveWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
200 return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
204 * Create a new stream writer, which writes to the specified output stream.
206 * The codec factory can be reused between multiple writers.
208 * Returned writer can be used emit multiple top level element,
209 * but does not start / close parent JSON object, which must be done
210 * by user providing {@code jsonWriter} instance in order for
213 * @param codecFactory JSON codec factory
214 * @param path Schema Path
215 * @param initialNs Initial namespace
216 * @param jsonWriter JsonWriter
217 * @return A stream writer instance
219 public static NormalizedNodeStreamWriter createNestedWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
220 return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
224 public void leafNode(final NodeIdentifier name, final Object value) throws IOException {
225 final LeafSchemaNode schema = tracker.leafNode(name);
226 final JSONCodec<Object> codec = codecs.codecFor(schema);
227 context.emittingChild(codecs.getSchemaContext(), writer);
228 context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
229 writeValue(value, codec);
233 public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
234 tracker.startLeafSet(name);
235 context = new JSONStreamWriterListContext(context, name);
239 public void leafSetEntryNode(final Object value) throws IOException {
240 final LeafListSchemaNode schema = tracker.leafSetEntryNode();
241 final JSONCodec<Object> codec = codecs.codecFor(schema);
242 context.emittingChild(codecs.getSchemaContext(), writer);
243 writeValue(value, codec);
247 * Warning suppressed due to static final constant which triggers a warning
248 * for the call to schema.isPresenceContainer().
250 @SuppressWarnings("unused")
252 public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
253 final SchemaNode schema = tracker.startContainerNode(name);
255 // FIXME this code ignores presence for containers
256 // but datastore does as well and it needs be fixed first (2399)
257 context = new JSONStreamWriterNamedObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
261 public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
262 tracker.startList(name);
263 context = new JSONStreamWriterListContext(context, name);
267 public void startUnkeyedListItem(final NodeIdentifier name, final int childSizeHint) throws IOException {
268 tracker.startListItem(name);
269 context = new JSONStreamWriterObjectContext(context, name, DEFAULT_EMIT_EMPTY_CONTAINERS);
273 public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
274 tracker.startList(name);
275 context = new JSONStreamWriterListContext(context, name);
279 public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
281 tracker.startListItem(identifier);
282 context = new JSONStreamWriterObjectContext(context, identifier, DEFAULT_EMIT_EMPTY_CONTAINERS);
286 public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
287 tracker.startList(name);
288 context = new JSONStreamWriterListContext(context, name);
292 public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
293 tracker.startChoiceNode(name);
294 context = new JSONStreamWriterInvisibleContext(context);
298 public void startAugmentationNode(final AugmentationIdentifier identifier) {
299 tracker.startAugmentationNode(identifier);
300 context = new JSONStreamWriterInvisibleContext(context);
304 public void anyxmlNode(final NodeIdentifier name, final Object value) throws IOException {
305 @SuppressWarnings("unused")
306 final AnyXmlSchemaNode schema = tracker.anyxmlNode(name);
307 // FIXME: should have a codec based on this :)
309 context.emittingChild(codecs.getSchemaContext(), writer);
310 context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
311 // FIXME this kind of serialization is incorrect since the value for AnyXml is now a DOMSource
312 writer.value(String.valueOf(value));
316 public void endNode() throws IOException {
318 context = context.endNode(codecs.getSchemaContext(), writer);
320 if(context instanceof JSONStreamWriterRootContext) {
321 context.emitEnd(writer);
325 private void writeValue(Object value, JSONCodec<Object> codec)
327 codec.serializeToWriter(writer,value);
331 public void flush() throws IOException {
336 public void close() throws IOException {