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 static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
11 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
13 import com.google.gson.stream.JsonReader;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.io.StringWriter;
18 import java.net.URISyntaxException;
19 import org.junit.BeforeClass;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.stream.LoggingNormalizedNodeStreamWriter;
26 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeWriter;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public class StreamToNormalizedNodeTest {
35 private static final Logger LOG = LoggerFactory.getLogger(StreamToNormalizedNodeTest.class);
36 private static SchemaContext schemaContext;
37 private static String streamAsString;
40 public static void initialization() throws IOException, URISyntaxException {
41 schemaContext = loadModules("/complexjson/yang");
42 streamAsString = loadTextFile(new File(StreamToNormalizedNodeTest.class.getResource(
43 "/complexjson/complex-json.json").toURI()));
47 * Demonstrates how to log events produced by a {@link JsonReader}.
52 public void ownStreamWriterImplementationDemonstration() throws IOException {
53 // GSON's JsonReader reading from the loaded string (our event source)
54 final JsonReader reader = new JsonReader(new StringReader(streamAsString));
56 // StreamWriter which outputs SLF4J events
57 final LoggingNormalizedNodeStreamWriter logWriter = new LoggingNormalizedNodeStreamWriter();
59 // JSON -> StreamWriter parser
60 try (final JsonParserStream jsonHandler = JsonParserStream.create(logWriter, schemaContext)) {
61 // Process multiple readers, flush()/close() as needed
62 jsonHandler.parse(reader);
67 * Demonstrates how to create an immutable NormalizedNode tree from a {@link JsonReader} and
68 * then writes the data back into string representation.
74 public void immutableNormalizedNodeStreamWriterDemonstration() throws IOException {
76 * This is the parsing part
78 // This is where we will output the nodes
79 NormalizedNodeResult result = new NormalizedNodeResult();
81 // StreamWriter which attaches NormalizedNode under parent
82 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
84 // JSON -> StreamWriter parser
85 try (JsonParserStream handler = JsonParserStream.create(streamWriter, schemaContext)) {
86 handler.parse(new JsonReader(new StringReader(streamAsString)));
89 // Finally build the node
90 final NormalizedNode<?, ?> parsedData = result.getResult();
91 LOG.debug("Parsed NormalizedNodes: {}", parsedData);
94 * This is the serialization part.
96 // We want to write the first child out
97 final DataContainerChild<? extends PathArgument, ?> firstChild = (DataContainerChild<? extends PathArgument, ?>) parsedData;
98 LOG.debug("Serializing first child: {}", firstChild);
101 final StringWriter writer = new StringWriter();
103 // StreamWriter which outputs JSON strings
104 final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
106 // NormalizedNode -> StreamWriter
107 final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
109 // Write multiple NormalizedNodes fluently, flush()/close() as needed
110 nodeWriter.write(firstChild).close();
112 // Just to put it somewhere
113 LOG.debug("Serialized JSON: {}", writer.toString());