Add JSONValue and JSONCodec.unparseValue()
[yangtools.git] / codec / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONCodec.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 com.google.gson.stream.JsonWriter;
11 import java.io.IOException;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.data.util.codec.TypeAwareCodec;
14
15 /**
16  * A codec capable of performing normalized value conversion with a {@link JsonWriter}.
17  *
18  * @param <T> Normalized value type
19  */
20 public sealed interface JSONCodec<T> extends TypeAwareCodec<T, Object, JsonWriter>
21         permits AbstractJSONCodec, EmptyJSONCodec, IdentityrefJSONCodec, NullJSONCodec, UnionJSONCodec,
22                 // FIXME: rename this guy
23                 JSONInstanceIdentifierCodec {
24     /**
25      * {@inheritDoc}.
26      *
27      * @throws IOException if the write fails
28      * @deprecated Use {@link #writeValue(JSONValueWriter, Object)} instead.
29      */
30     @Override
31     @Deprecated(since = "13.0.3", forRemoval = true)
32     default void writeValue(final JsonWriter writer, final T value) throws IOException {
33         writeValue(new DefaultJSONValueWriter(writer), value);
34     }
35
36     /**
37      * Serialize specified value with specified {@link JSONValueWriter}.
38      *
39      * @param ctx Write context
40      * @param value Value in native format
41      * @throws IOException if the write fails
42      */
43     void writeValue(JSONValueWriter ctx, T value) throws IOException;
44
45     /**
46      * {@inheritDoc}.
47      *
48      * @deprecated Use {@link #parseValue(String)} instead.
49      */
50     @Override
51     @Deprecated
52     default T parseValue(final Object ctx, final String str) {
53         return parseValue(str);
54     }
55
56     /**
57      * Parse a String representation into its native format.
58      *
59      * @param str String representation
60      * @return Value in native format
61      * @throws IllegalArgumentException if the value does not parse or pass type validation
62      */
63     T parseValue(String str);
64
65     /**
66      * Return the {@link JSONValue} representation of a native value.
67      *
68      * @param value Value in native format
69      * @return A {@link JSONValue}
70      * @throws IllegalArgumentException if the value does not parse or pass type validation
71      */
72     @NonNull JSONValue unparseValue(T value);
73 }