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;
14 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
20 import org.opendaylight.yangtools.yang.data.codec.gson.JSONNormalizedNodeStreamWriter;
21 import org.opendaylight.yangtools.yang.data.codec.gson.JsonWriterFactory;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25 * JSON stream-writer with disabled leaf-type validation for specified QName.
27 final class JsonStreamWriterWithDisabledValidation extends StreamWriterWithDisabledValidation {
29 private final JsonWriter jsonWriter;
30 private final NormalizedNodeStreamWriter jsonNodeStreamWriter;
33 * Creation of the custom JSON stream-writer.
35 * @param excludedQName QName of the element that is excluded from type-check.
36 * @param outputWriter Output stream that is used for creation of JSON writers.
37 * @param schemaPath Schema-path of the {@link NormalizedNode} to be written.
38 * @param initialNs Initial namespace derived from schema node of the data that are serialized.
39 * @param schemaContextHandler Handler that holds actual schema context.
41 JsonStreamWriterWithDisabledValidation(final QName excludedQName, final OutputStreamWriter outputWriter,
42 final SchemaPath schemaPath, final URI initialNs, final SchemaContextHandler schemaContextHandler) {
44 this.jsonWriter = JsonWriterFactory.createJsonWriter(outputWriter);
45 this.jsonNodeStreamWriter = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
46 JSONCodecFactorySupplier.RFC7951.getShared(schemaContextHandler.get()),
47 schemaPath, initialNs, jsonWriter);
51 protected NormalizedNodeStreamWriter delegate() {
52 return jsonNodeStreamWriter;
56 void startLeafNodeWithDisabledValidation(final NodeIdentifier nodeIdentifier) throws IOException {
57 jsonWriter.name(nodeIdentifier.getNodeType().getLocalName());
61 void scalarValueWithDisabledValidation(final Object value) throws IOException {
62 jsonWriter.value(value.toString());
66 void endNodeWithDisabledValidation() {