Mark JSON writers as @Deprecated
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONNormalizedNodeStreamWriter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.codec.gson;
9
10 import com.google.common.base.Preconditions;
11 import com.google.gson.stream.JsonWriter;
12 import java.io.IOException;
13 import java.io.Writer;
14 import java.net.URI;
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;
26
27 /**
28  * This implementation will create JSON output as output stream.
29  *
30  * Values of leaf and leaf-list are NOT translated according to codecs.
31  *
32  */
33 public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
34     /**
35      * RFC6020 deviation: we are not required to emit empty containers unless they
36      * are marked as 'presence'.
37      */
38     private static final boolean DEFAULT_EMIT_EMPTY_CONTAINERS = true;
39
40     private final SchemaTracker tracker;
41     private final JSONCodecFactory codecs;
42     private final JsonWriter writer;
43     private JSONStreamWriterContext context;
44
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);
50     }
51
52     /**
53      * Create a new stream writer, which writes to the specified {@link Writer}.
54      *
55      * This instance of writer can be used only to emit one top level element,
56      * otherwise it will produce incorrect JSON.
57      *
58      * @deprecated please use
59      *             {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
60      *             {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
61      *             appropriate instead.
62      *
63      * @param schemaContext Schema context
64      * @param writer Output writer
65      * @return A stream writer instance
66      */
67     @Deprecated
68     public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer) {
69         return create(schemaContext, SchemaPath.ROOT, null, writer);
70     }
71
72     /**
73      * Create a new stream writer, which writes to the specified {@link Writer}.
74      *
75      * This instance of writer can be used only to emit one top level element,
76      * otherwise it will produce incorrect JSON.
77      *
78      * @deprecated please use
79      *             {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
80      *             {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
81      *             appropriate instead.
82      *
83      * @param schemaContext Schema context
84      * @param path Root schemapath
85      * @param writer Output writer
86      * @return A stream writer instance
87      */
88     @Deprecated
89     public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, final Writer writer) {
90         return create(schemaContext, path, null, writer);
91     }
92
93     /**
94      * Create a new stream writer, which writes to the specified {@link Writer}.
95      *
96      * This instance of writer can be used only to emit one top level element,
97      * otherwise it will produce incorrect JSON.
98      *
99      * @deprecated please use
100      *             {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
101      *             {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
102      *             appropriate instead.
103      *
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
109      */
110     @Deprecated
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));
114     }
115
116     /**
117      * Create a new stream writer, which writes to the specified output stream.
118      *
119      * This instance of writer can be used only to emit one top level element,
120      * otherwise it will produce incorrect JSON.
121      *
122      * @deprecated please use
123      *             {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
124      *             {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
125      *             appropriate instead.
126      *
127      * @param schemaContext Schema context
128      * @param writer Output writer
129      * @param indentSize indentation size
130      * @return A stream writer instance
131      */
132     @Deprecated
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));
135     }
136
137     /**
138      * Create a new stream writer, which writes to the specified output stream. The codec factory
139      * can be reused between multiple writers.
140      *
141      * This instance of writer can be used only to emit one top level element,
142      * otherwise it will produce incorrect JSON.
143      *
144      * @deprecated please use
145      *             {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
146      *             {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
147      *             appropriate instead.
148      *
149      * @param codecFactory JSON codec factory
150      * @param writer Output writer
151      * @param indentSize indentation size
152      * @return A stream writer instance
153      */
154     @Deprecated
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));
157     }
158
159     /**
160      * Create a new stream writer, which writes to the specified output stream.
161      *
162      * This instance of writer can be used only to emit one top level element,
163      * otherwise it will produce incorrect JSON.
164      *
165      * @deprecated please use
166      *             {@link #createExclusiveWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} or
167      *             {@link #createNestedWriter(JSONCodecFactory, SchemaPath, URI, JsonWriter)} as
168      *             appropriate instead.
169      *
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
175      */
176     @Deprecated
177     public static NormalizedNodeStreamWriter create(SchemaContext schemaContext, SchemaPath path, URI initialNs,
178             JsonWriter jsonWriter) {
179         return createExclusiveWriter(JSONCodecFactory.create(schemaContext), path, initialNs, jsonWriter);
180     }
181
182     /**
183      * Create a new stream writer, which writes to the specified output stream.
184      *
185      * The codec factory can be reused between multiple writers.
186      *
187      * Returned writer is exclusive user of JsonWriter, which means it will start
188      * top-level JSON element and ends it.
189      *
190      * This instance of writer can be used only to emit one top level element,
191      * otherwise it will produce incorrect JSON.
192      *
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
198      */
199     public static NormalizedNodeStreamWriter createExclusiveWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
200         return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
201     }
202
203     /**
204      * Create a new stream writer, which writes to the specified output stream.
205      *
206      * The codec factory can be reused between multiple writers.
207      *
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
211      * JSON to be valid.
212      *
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
218      */
219     public static NormalizedNodeStreamWriter createNestedWriter(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
220         return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
221     }
222
223     @Override
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);
230     }
231
232     @Override
233     public void startLeafSet(final NodeIdentifier name, final int childSizeHint) throws IOException {
234         tracker.startLeafSet(name);
235         context = new JSONStreamWriterListContext(context, name);
236     }
237
238     @Override
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);
244     }
245
246     /*
247      * Warning suppressed due to static final constant which triggers a warning
248      * for the call to schema.isPresenceContainer().
249      */
250     @SuppressWarnings("unused")
251     @Override
252     public void startContainerNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
253         final SchemaNode schema = tracker.startContainerNode(name);
254
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);
258     }
259
260     @Override
261     public void startUnkeyedList(final NodeIdentifier name, final int childSizeHint) throws IOException {
262         tracker.startList(name);
263         context = new JSONStreamWriterListContext(context, name);
264     }
265
266     @Override
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);
270     }
271
272     @Override
273     public void startMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
274         tracker.startList(name);
275         context = new JSONStreamWriterListContext(context, name);
276     }
277
278     @Override
279     public void startMapEntryNode(final NodeIdentifierWithPredicates identifier, final int childSizeHint)
280             throws IOException {
281         tracker.startListItem(identifier);
282         context = new JSONStreamWriterObjectContext(context, identifier, DEFAULT_EMIT_EMPTY_CONTAINERS);
283     }
284
285     @Override
286     public void startOrderedMapNode(final NodeIdentifier name, final int childSizeHint) throws IOException {
287         tracker.startList(name);
288         context = new JSONStreamWriterListContext(context, name);
289     }
290
291     @Override
292     public void startChoiceNode(final NodeIdentifier name, final int childSizeHint) {
293         tracker.startChoiceNode(name);
294         context = new JSONStreamWriterInvisibleContext(context);
295     }
296
297     @Override
298     public void startAugmentationNode(final AugmentationIdentifier identifier) {
299         tracker.startAugmentationNode(identifier);
300         context = new JSONStreamWriterInvisibleContext(context);
301     }
302
303     @Override
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 :)
308
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));
313     }
314
315     @Override
316     public void endNode() throws IOException {
317         tracker.endNode();
318         context = context.endNode(codecs.getSchemaContext(), writer);
319
320         if(context instanceof JSONStreamWriterRootContext) {
321             context.emitEnd(writer);
322         }
323     }
324
325     private void writeValue(Object value, JSONCodec<Object> codec)
326             throws IOException {
327         codec.serializeToWriter(writer,value);
328     }
329
330     @Override
331     public void flush() throws IOException {
332         writer.flush();
333     }
334
335     @Override
336     public void close() throws IOException {
337         writer.flush();
338         writer.close();
339     }
340
341
342
343 }