Eliminate NormalizedNodePayload
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / HackJsonWriter.java
1 /*
2  * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.server.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.gson.stream.JsonWriter;
13 import java.io.IOException;
14 import java.io.StringWriter;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodec;
18
19 /**
20  * A hack to intercept {@link JSONCodec#writeValue(JsonWriter, Object)} output. This class is closely tailored to
21  * respond implementation behaviour of JSONCodecs.
22  */
23 // FIXME: remove this class once we have YANGTOOLS-1569
24 final class HackJsonWriter extends JsonWriter {
25     @NonNullByDefault
26     record Value(String rawString, Kind kind) {
27         enum Kind {
28             BOOLEAN,
29             NULL,
30             NUMBER,
31             STRING
32         }
33
34         Value {
35             requireNonNull(rawString);
36             requireNonNull(kind);
37         }
38     }
39
40     private static final Value FALSE = new Value("false", Value.Kind.BOOLEAN);
41     private static final Value TRUE = new Value("true", Value.Kind.BOOLEAN);
42     private static final Value NULL = new Value("[null]", Value.Kind.NULL);
43
44     private Value captured = null;
45
46     HackJsonWriter() {
47         super(new StringWriter());
48     }
49
50     @Override
51     public JsonWriter nullValue() throws IOException {
52         capture(NULL);
53         return super.nullValue();
54     }
55
56     @Override
57     public JsonWriter value(final boolean value) throws IOException {
58         capture(value ? TRUE : FALSE);
59         return super.value(value);
60     }
61
62     @Override
63     public JsonWriter value(final Boolean value) throws IOException {
64         // We assume non-null values
65         return value(value.booleanValue());
66     }
67
68     @Override
69     public JsonWriter value(final Number value) throws IOException {
70         capture(new Value(value.toString(), Value.Kind.NUMBER));
71         return super.value(value);
72     }
73
74     @Override
75     public JsonWriter value(final String value) throws IOException {
76         if (value == null) {
77             return nullValue();
78         }
79         capture(new Value(value, Value.Kind.STRING));
80         return super.value(value);
81     }
82
83     @NonNull Value acquireCaptured() throws IOException {
84         final var local = captured;
85         if (local == null) {
86             throw new IOException("No value set");
87         }
88         return local;
89     }
90
91     private void capture(final Value newValue) throws IOException {
92         if (captured != null) {
93             throw new IOException("Value already set to " + captured);
94         }
95         captured = newValue;
96     }
97 }