2 * Copyright © 2019 FRINX s.r.o. 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.restconf.nb.rfc8040.jersey.providers.errors;
10 import com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import java.io.OutputStreamWriter;
13 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Errors;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
22 * JSON stream-writer with disabled leaf-type validation for specified QName.
24 final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
25 private static final int DEFAULT_INDENT_SPACES_NUM = 2;
26 private static final XMLNamespace IETF_RESTCONF_URI = Errors.QNAME.getModule().getNamespace();
28 private final JsonWriter jsonWriter;
29 private final NormalizedNodeStreamWriter jsonNodeStreamWriter;
32 * Creation of the custom JSON stream-writer.
34 * @param databindContext {@link DatabindContext} to use
35 * @param outputWriter Output stream that is used for creation of JSON writers.
37 JsonStreamWriterWithDisabledValidation(final DatabindContext databindContext,
38 final OutputStreamWriter outputWriter) {
39 jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter, DEFAULT_INDENT_SPACES_NUM);
40 final var inference = errorsContainerInference(databindContext);
41 jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(databindContext.jsonCodecs(),
42 inference, IETF_RESTCONF_URI, jsonWriter);
46 protected NormalizedNodeStreamWriter delegate() {
47 return jsonNodeStreamWriter;
51 void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
52 jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
56 void scalarValueWithDisabledValidation(final Object value) throws IOException {
57 jsonWriter.value(value.toString());
61 void endNodeWithDisabledValidation() {