2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.databind;
10 import com.google.gson.stream.JsonReader;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.nio.charset.StandardCharsets;
15 import org.opendaylight.restconf.server.api.DataPutPath;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
17 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
18 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
21 * A JSON-encoded {@link ResourceBody}.
23 public final class JsonResourceBody extends ResourceBody {
24 public JsonResourceBody(final InputStream inputStream) {
29 void streamTo(final DataPutPath path, final PathArgument name, final InputStream inputStream,
30 final NormalizedNodeStreamWriter writer) throws IOException {
31 try (var jsonParser = newParser(path, writer)) {
32 try (var reader = new JsonReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
33 jsonParser.parse(reader);
38 private static JsonParserStream newParser(final DataPutPath path, final NormalizedNodeStreamWriter writer) {
39 final var codecs = path.databind().jsonCodecs();
40 final var inference = path.inference();
41 if (inference.isEmpty()) {
42 return JsonParserStream.create(writer, codecs);
45 final var stack = inference.toSchemaInferenceStack();
47 return JsonParserStream.create(writer, codecs, stack.toInference());