package org.opendaylight.yangtools.yang.data.codec.gson;
import com.google.common.base.Preconditions;
-
import org.opendaylight.yangtools.concepts.Codec;
import org.opendaylight.yangtools.yang.data.api.codec.BooleanCodec;
import org.opendaylight.yangtools.yang.data.api.codec.DecimalCodec;
* @param codec underlying codec
* @return A JSONCodec instance
*/
- public static <T> JSONCodec<T> create(final Codec<String, T> codec) {
- if (codec instanceof BooleanCodec || codec instanceof DecimalCodec ||
- codec instanceof Int8Codec || codec instanceof Int16Codec ||
- codec instanceof Int32Codec || codec instanceof Int64Codec ||
- codec instanceof Uint8Codec || codec instanceof Uint16Codec ||
- codec instanceof Uint32Codec || codec instanceof Uint64Codec) {
- return new UnquotedJSONCodec<>(codec);
+ public static JSONCodec<?> create(final Codec<String, ?> codec) {
+ if (codec instanceof BooleanCodec) {
+ return new BooleanJSONCodec((BooleanCodec<String>) codec);
+ } else if (codec instanceof DecimalCodec || codec instanceof Int8Codec
+ || codec instanceof Int16Codec || codec instanceof Int32Codec
+ || codec instanceof Int64Codec || codec instanceof Uint8Codec
+ || codec instanceof Uint16Codec || codec instanceof Uint32Codec
+ || codec instanceof Uint64Codec) {
+ return new NumberJSONCodec(codec);
+ } else {
+ return new QuotedJSONCodec<>(codec);
}
-
- return new QuotedJSONCodec<>(codec);
}
@Override
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.codec.gson;
+
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
+import org.opendaylight.yangtools.concepts.Codec;
+
+/**
+ * A {@link JSONCodec} which does not need double quotes in output representation.
+ *
+ * @param <T> Deserialized value type
+ */
+final class BooleanJSONCodec extends AbstractJSONCodec<Boolean> {
+ BooleanJSONCodec(final Codec<String, Boolean> codec) {
+ super(codec);
+ }
+
+ @Override
+ public boolean needQuotes() {
+ return false;
+ }
+
+ /**
+ * Serialize specified value with specified JsonWriter.
+ *
+ * @param writer JsonWriter
+ * @param value
+ */
+ @Override
+ public void serializeToWriter(JsonWriter writer, Boolean value) throws IOException {
+ writer.value(value);
+ };
+}
\ No newline at end of file
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
import org.opendaylight.yangtools.concepts.Codec;
interface JSONCodec<T> extends Codec<String, T> {
+ // FIXME: Unused, remove once we are sure we do not need this anymore.
boolean needQuotes();
+
+
+ /**
+ * Serialize specified value with specified JsonWriter.
+ *
+ * @param writer JsonWriter
+ * @param value
+ */
+ void serializeToWriter(JsonWriter writer, T value) throws IOException;
}
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
-
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
public boolean needQuotes() {
return false;
}
+
+ @Override
+ public void serializeToWriter(JsonWriter writer, Object value) throws IOException {
+ // NOOP since codec is unkwown.
+ LOG.warn("Call of the serializeToWriter method on JSONCodecFactory.NULL_CODEC object. No operation performed.");
+ }
};
private static TypeDefinition<?> resolveBaseTypeFrom(final TypeDefinition<?> type) {
return NULL_CODEC;
}
- return AbstractJSONCodec.create(codec);
+ return (JSONCodec<Object>) AbstractJSONCodec.create(codec);
}
});
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec;
final class JSONLeafrefCodec implements JSONCodec<Object>, LeafrefCodec<String> {
public boolean needQuotes() {
return true;
}
+
+ /**
+ * Serialize specified value with specified JsonWriter.
+ *
+ * @param writer JsonWriter
+ * @param value
+ */
+ @Override
+ public void serializeToWriter(JsonWriter writer, Object value) throws IOException {
+ writer.value(serialize(value));
+ }
}
\ No newline at end of file
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
-import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.io.Writer;
*
* Values of leaf and leaf-list are NOT translated according to codecs.
*
- * FIXME: rewrite this in terms of {@link JsonWriter}.
*/
public class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
/**
*/
private static final boolean DEFAULT_EMIT_EMPTY_CONTAINERS = true;
- /**
- * Matcher used to check if a string needs to be escaped.
- */
- private static final CharMatcher JSON_ILLEGAL_STRING_CHARACTERS = CharMatcher.anyOf("\\\"\n\r");
-
private final SchemaTracker tracker;
private final JSONCodecFactory codecs;
- private final Writer writer;
- private final String indent;
+ private final JsonWriter writer;
private JSONStreamWriterContext context;
- private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path,
- final Writer writer, final URI initialNs, final int indentSize) {
- this.writer = Preconditions.checkNotNull(writer);
-
- Preconditions.checkArgument(indentSize >= 0, "Indent size must be non-negative");
- if (indentSize != 0) {
- indent = Strings.repeat(" ", indentSize);
- } else {
- indent = null;
- }
+ private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final URI initialNs, JsonWriter JsonWriter) {
+ this.writer = JsonWriter;
this.codecs = Preconditions.checkNotNull(codecFactory);
this.tracker = SchemaTracker.create(codecFactory.getSchemaContext(), path);
this.context = new JSONStreamWriterRootContext(initialNs);
* @return A stream writer instance
*/
public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer) {
- return new JSONNormalizedNodeStreamWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, writer, null, 0);
+ return create(schemaContext, SchemaPath.ROOT, null, writer);
}
/**
* @return A stream writer instance
*/
public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path, final Writer writer) {
- return new JSONNormalizedNodeStreamWriter(JSONCodecFactory.create(schemaContext), path, writer, null, 0);
+ return create(schemaContext, path, null, writer);
}
/**
*/
public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final SchemaPath path,
final URI initialNs, final Writer writer) {
- return new JSONNormalizedNodeStreamWriter(JSONCodecFactory.create(schemaContext), path, writer, initialNs, 0);
+ return create(JSONCodecFactory.create(schemaContext), path, initialNs, JsonWriterFactory.createJsonWriter(writer));
}
/**
* @return A stream writer instance
*/
public static NormalizedNodeStreamWriter create(final SchemaContext schemaContext, final Writer writer, final int indentSize) {
- return new JSONNormalizedNodeStreamWriter(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, writer, null, indentSize);
+ return create(JSONCodecFactory.create(schemaContext), SchemaPath.ROOT, null,JsonWriterFactory.createJsonWriter(writer, indentSize));
}
/**
* @return A stream writer instance
*/
public static NormalizedNodeStreamWriter create(final JSONCodecFactory codecFactory, final Writer writer, final int indentSize) {
- return new JSONNormalizedNodeStreamWriter(codecFactory, SchemaPath.ROOT, writer, null, indentSize);
+ return create(codecFactory, SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer,indentSize));
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream.
+ *
+ * @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 create(JSONCodecFactory.create(schemaContext), path, initialNs, jsonWriter);
+ }
+
+ /**
+ * Create a new stream writer, which writes to the specified output stream. The codec factory
+ * can be reused between multiple writers.
+ *
+ * @param codecFactory JSON codec factory
+ * @param path Schema Path
+ * @param initialNs Initial namespace
+ * @param jsonWriter JsonWriter
+ * @return A stream writer instance
+ */
+ public static NormalizedNodeStreamWriter create(JSONCodecFactory codecFactory, SchemaPath path, URI initialNs, JsonWriter jsonWriter) {
+ return new JSONNormalizedNodeStreamWriter(codecFactory, path, initialNs, jsonWriter);
}
@Override
final LeafSchemaNode schema = tracker.leafNode(name);
final JSONCodec<Object> codec = codecs.codecFor(schema.getType());
- context.emittingChild(codecs.getSchemaContext(), writer, indent);
+ context.emittingChild(codecs.getSchemaContext(), writer);
context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
- writeValue(codec.serialize(value), codec.needQuotes());
+
+ writeValue(value, codec);
}
@Override
final LeafListSchemaNode schema = tracker.leafSetEntryNode();
final JSONCodec<Object> codec = codecs.codecFor(schema.getType());
- context.emittingChild(codecs.getSchemaContext(), writer, indent);
- writeValue(codec.serialize(value), codec.needQuotes());
+ context.emittingChild(codecs.getSchemaContext(), writer);
+
+ writeValue(value, codec);
}
/*
final AnyXmlSchemaNode schema = tracker.anyxmlNode(name);
// FIXME: should have a codec based on this :)
- context.emittingChild(codecs.getSchemaContext(), writer, indent);
+ context.emittingChild(codecs.getSchemaContext(), writer);
context.writeChildJsonIdentifier(codecs.getSchemaContext(), writer, name.getNodeType());
- writeValue(String.valueOf(value), true);
+ writer.value(String.valueOf(value));
}
@Override
public void endNode() throws IOException {
tracker.endNode();
- context = context.endNode(codecs.getSchemaContext(), writer, indent);
+ context = context.endNode(codecs.getSchemaContext(), writer);
+ if(context instanceof JSONStreamWriterRootContext) {
+ context.emitEnd(writer);
+ }
}
- private void writeValue(final String str, final boolean needQuotes) throws IOException {
- if (needQuotes) {
- writer.append('"');
-
- final int needEscape = JSON_ILLEGAL_STRING_CHARACTERS.countIn(str);
- if (needEscape != 0) {
- final char[] escaped = new char[str.length() + needEscape];
- int offset = 0;
-
- for (int i = 0; i < str.length(); i++) {
- final char c = str.charAt(i);
- if (JSON_ILLEGAL_STRING_CHARACTERS.matches(c)) {
- escaped[offset++] = '\\';
- }
- escaped[offset++] = c;
- }
- writer.write(escaped);
- } else {
- writer.append(str);
- }
-
- writer.append('"');
- } else {
- writer.append(str);
- }
+ private void writeValue(Object value, JSONCodec<Object> codec)
+ throws IOException {
+ codec.serializeToWriter(writer,value);
}
@Override
writer.close();
}
+
+
}
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.io.StringWriter;
import java.net.URI;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.common.QName;
private final boolean mandatory;
private final int depth;
private boolean emittedMyself = false;
- private boolean haveChild = false;
/**
* Construct a new context.
* @param qname Namespace/name tuple
* @throws IOException when the writer reports it
*/
- final void writeChildJsonIdentifier(final SchemaContext schema, final Writer writer, final QName qname) throws IOException {
- writer.append('"');
+ final void writeChildJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname) throws IOException {
+ StringWriter strWriter = new StringWriter();
// Prepend module name if namespaces do not match
final URI ns = qname.getNamespace();
if (!ns.equals(getNamespace())) {
final Module module = schema.findModuleByNamespaceAndRevision(ns, null);
Preconditions.checkArgument(module != null, "Could not find module for namespace {}", ns);
- writer.append(module.getName());
- writer.append(':');
+ strWriter.append(module.getName());
+ strWriter.append(':');
}
+ strWriter.append(qname.getLocalName());
- writer.append(qname.getLocalName());
- writer.append("\":");
+ writer.name(strWriter.toString());
}
/**
* @param qname Namespace/name tuple
* @throws IOException when the writer reports it
*/
- protected final void writeMyJsonIdentifier(final SchemaContext schema, final Writer writer, final QName qname) throws IOException {
+ protected final void writeMyJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname) throws IOException {
parent.writeChildJsonIdentifier(schema, writer, qname);
}
* @param writer Output writer
* @throws IOException
*/
- protected abstract void emitStart(final SchemaContext schema, final Writer writer) throws IOException;
+ protected abstract void emitStart(final SchemaContext schema, final JsonWriter writer) throws IOException;
/**
* Emit the end of an element.
* @param writer Output writer
* @throws IOException
*/
- protected abstract void emitEnd(final Writer writer) throws IOException;
+ protected abstract void emitEnd(final JsonWriter writer) throws IOException;
- private final void emitMyself(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
+ private final void emitMyself(final SchemaContext schema, final JsonWriter writer) throws IOException {
if (!emittedMyself) {
if (parent != null) {
- parent.emittingChild(schema, writer, indent);
+ parent.emittingChild(schema, writer);
}
emitStart(schema, writer);
*
* @param schema Schema context
* @param writer Output writer
- * @param indent Indentation string
* @throws IOException when writer reports it
*/
- final void emittingChild(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
- emitMyself(schema, writer, indent);
- if (haveChild) {
- writer.append(',');
- }
-
- if (indent != null) {
- writer.append('\n');
-
- for (int i = 0; i < depth; i++) {
- writer.append(indent);
- }
- }
- haveChild = true;
+ final void emittingChild(final SchemaContext schema, final JsonWriter writer) throws IOException {
+ emitMyself(schema, writer);
}
/**
*
* @param schema Schema context
* @param writer Output writer
- * @param indent Indentation string
* @return Parent node context
* @throws IOException when writer reports it
* @throws IllegalArgumentException if this node cannot be ended (e.g. root)
*/
- final JSONStreamWriterContext endNode(final SchemaContext schema, final Writer writer, final String indent) throws IOException {
+ final JSONStreamWriterContext endNode(final SchemaContext schema, final JsonWriter writer) throws IOException {
if (!emittedMyself && mandatory) {
- emitMyself(schema, writer, indent);
+ emitMyself(schema, writer);
}
if (emittedMyself) {
package org.opendaylight.yangtools.yang.data.codec.gson;
import com.google.common.base.Preconditions;
+import com.google.gson.stream.JsonWriter;
+
-import java.io.Writer;
/**
* A virtual recursion level in {@link JSONNormalizedNodeStreamWriter}, used for nodes
}
@Override
- protected void emitEnd(final Writer writer) {
+ protected void emitEnd(final JsonWriter writer) {
// No-op
}
}
\ No newline at end of file
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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
}
@Override
- protected void emitStart(final SchemaContext schema, final Writer writer) throws IOException {
+ protected void emitStart(final SchemaContext schema, final JsonWriter writer) throws IOException {
writeMyJsonIdentifier(schema, writer, getQName());
- writer.append('[');
+ writer.beginArray();
}
@Override
- protected void emitEnd(final Writer writer) throws IOException {
- writer.append(']');
+ protected void emitEnd(final JsonWriter writer) throws IOException {
+ writer.endArray();
}
}
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import com.google.gson.stream.JsonWriter;
import java.io.IOException;
-import java.io.Writer;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
/**
* A recursion level of {@link JSONNormalizedNodeStreamWriter}, which represents
* a JSON object which has to be prefixed with its identifier -- such as a
}
@Override
- protected void emitStart(final SchemaContext schema, final Writer writer) throws IOException {
+ protected void emitStart(final SchemaContext schema, final JsonWriter writer) throws IOException {
writeMyJsonIdentifier(schema, writer, getQName());
super.emitStart(schema, writer);
}
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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
}
@Override
- protected void emitStart(final SchemaContext schema, final Writer writer) throws IOException {
- writer.append('{');
+ protected void emitStart(final SchemaContext schema, final JsonWriter writer) throws IOException {
+ writer.beginObject();
}
@Override
- protected void emitEnd(final Writer writer) throws IOException {
- writer.append('}');
+ protected void emitEnd(final JsonWriter writer) throws IOException {
+ writer.endObject();
}
}
\ No newline at end of file
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
-import java.io.Writer;
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
import java.net.URI;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
/**
* The root node of a particular {@link JSONNormalizedNodeStreamWriter} instance.
}
@Override
- protected void emitEnd(final Writer writer) {
- throw new IllegalArgumentException("Top-level node reached");
+ protected void emitStart(final SchemaContext schema, final JsonWriter writer) throws IOException {
+ writer.beginObject();
+ }
+
+ @Override
+ protected void emitEnd(final JsonWriter writer) throws IOException {
+ writer.endObject();
}
}
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import com.google.gson.stream.JsonWriter;
import java.io.IOException;
-import java.io.Writer;
import java.net.URI;
-
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
/**
* Abstract class tracking a virtual level of {@link JSONNormalizedNodeStreamWriter}
* recursion. It only tracks the namespace associated with this node.
}
@Override
- protected final void emitStart(final SchemaContext schema, final Writer writer) throws IOException {
+ protected void emitStart(final SchemaContext schema, final JsonWriter writer) throws IOException {
// No-op
}
}
\ No newline at end of file
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.net.URI;
-
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringIdentityrefCodec;
import org.opendaylight.yangtools.yang.model.api.Module;
public boolean needQuotes() {
return true;
}
+
+ /**
+ * Serialize QName with specified JsonWriter.
+ *
+ * @param writer JsonWriter
+ * @param value QName
+ */
+ @Override
+ public void serializeToWriter(JsonWriter writer, QName value) throws IOException {
+ writer.value(serialize(value));
+ }
}
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.net.URI;
-
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
import org.opendaylight.yangtools.yang.model.api.Module;
public boolean needQuotes() {
return true;
}
+
+ /**
+ * Serialize YangInstanceIdentifier with specified JsonWriter.
+ *
+ * @param writer JsonWriter
+ * @param value YangInstanceIdentifier
+ */
+ @Override
+ public void serializeToWriter(JsonWriter writer, YangInstanceIdentifier value) throws IOException {
+ writer.value(serialize(value));
+ }
}
--- /dev/null
+/**
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.codec.gson;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Strings;
+import com.google.gson.stream.JsonWriter;
+import java.io.Writer;
+
+/**
+ * Factory Method class for JsonWriter creation
+ */
+@Beta
+public final class JsonWriterFactory {
+
+ private JsonWriterFactory() {
+ }
+ /**
+ * Create a new JsonWriter, which writes to the specified output writer.
+ *
+ * @param writer Output writer
+ * @return A JsonWriter instance
+ */
+ public static JsonWriter createJsonWriter(Writer writer) {
+ return new JsonWriter(writer);
+ }
+
+ /**
+ * Create a new JsonWriter, which writes to the specified output writer.
+ *
+ * @param writer Output writer
+ * @param indentSize size of the indent
+ * @return A JsonWriter instance
+ */
+ public static JsonWriter createJsonWriter(Writer writer, int indentSize) {
+ JsonWriter jsonWriter = new JsonWriter(writer);
+ final String indent = Strings.repeat(" ", indentSize);
+ jsonWriter.setIndent(indent);
+ return jsonWriter;
+ }
+
+}
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
import org.opendaylight.yangtools.concepts.Codec;
/**
*
* @param <T> Deserialized value type
*/
-final class UnquotedJSONCodec<T> extends AbstractJSONCodec<T> {
- UnquotedJSONCodec(final Codec<String, T> codec) {
+final class NumberJSONCodec<T extends Number> extends AbstractJSONCodec<T > {
+ NumberJSONCodec(final Codec<String, T> codec) {
super(codec);
}
public boolean needQuotes() {
return false;
}
+
+ /**
+ * Serialize specified value with specified JsonWriter.
+ *
+ * @param writer JsonWriter
+ * @param value
+ */
+ @Override
+ public void serializeToWriter(JsonWriter writer, T value) throws IOException {
+ writer.value(value);
+ }
}
\ No newline at end of file
*/
package org.opendaylight.yangtools.yang.data.codec.gson;
+import com.google.gson.stream.JsonWriter;
+import java.io.IOException;
import org.opendaylight.yangtools.concepts.Codec;
/**
public boolean needQuotes() {
return true;
}
+
+ /**
+ * Serialize specified value with specified JsonWriter.
+ *
+ * @param writer JsonWriter
+ * @param value
+ */
+ @Override
+ public void serializeToWriter(JsonWriter writer, T value) throws IOException {
+ writer.value(serialize(value));
+ }
}
\ No newline at end of file
private String normalizedNodeToJsonStreamTransformation(final Writer writer,
final NormalizedNode<?, ?> inputStructure) throws IOException {
- writer.write("{\n");
+
final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.create(schemaContext, writer, 2);
final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
nodeWriter.write(inputStructure);
- writer.write("\n}");
+
nodeWriter.close();
return writer.toString();
}