Add JSONValue and JSONCodec.unparseValue()
[yangtools.git] / codec / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONValue.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.yangtools.yang.data.codec.gson;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNullByDefault;
13
14 /**
15  * A serialized JSON string, indicating what kind of value it represents.
16  *
17  * @param rawString unescaped string
18  * @param kind string kind
19  */
20 @NonNullByDefault
21 public record JSONValue(String rawString, Kind kind) {
22     /**
23      * The kind of a {@link JSONValue}. Indicates the semantics of {@link JSONValue#rawString()}.
24      */
25     public enum Kind {
26         /**
27          * A {@code boolean} value.
28          */
29         BOOLEAN,
30         /**
31          * An {@code empty} value.
32          */
33         EMPTY,
34         /**
35          * A numeric value, excluding {@code int64} and {@code uint64)}.
36          */
37         NUMBER,
38         /**
39          * A string value.
40          */
41         STRING
42     }
43
44     /**
45      * The equivalent on {@link Boolean#FALSE}.
46      */
47     public static final JSONValue FALSE = new JSONValue("false", Kind.BOOLEAN);
48     /**
49      * The equivalent on {@link Boolean#TRUE}.
50      */
51     public static final JSONValue TRUE = new JSONValue("true", Kind.BOOLEAN);
52     /**
53      * The equivalent on {@link org.opendaylight.yangtools.yang.common.Empty#value()}.
54      */
55     public static final JSONValue EMPTY = new JSONValue("[null]", Kind.EMPTY);
56
57     public JSONValue {
58         requireNonNull(rawString);
59         requireNonNull(kind);
60     }
61 }