public void writeValue(final JSONValueWriter ctx, final Boolean value) throws IOException {
ctx.writeBoolean(value);
}
+
+ @Override
+ public JSONValue unparseValue(final Boolean value) {
+ return value ? JSONValue.TRUE : JSONValue.FALSE;
+ }
}
return Empty.value();
}
+ @Override
+ public JSONValue unparseValue(final Empty value) {
+ return JSONValue.EMPTY;
+ }
+
@Override
public void writeValue(final JSONValueWriter ctx, final Empty value) throws IOException {
ctx.writeEmpty();
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.data.codec.gson.JSONValue.Kind;
import org.opendaylight.yangtools.yang.data.util.codec.IdentityCodecUtil;
import org.opendaylight.yangtools.yang.data.util.codec.QNameCodecUtil;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
}).getQName();
}
+ @Override
+ public JSONValue unparseValue(final QName value) {
+ return new JSONValue(encode(value), Kind.STRING);
+ }
+
@Override
public void writeValue(final JSONValueWriter ctx, final QName value) throws IOException {
- ctx.writeString(QNameCodecUtil.encodeQName(value, uri -> context.findModuleStatement(uri)
+ ctx.writeString(encode(value));
+ }
+
+ private @NonNull String encode(final QName value) {
+ return QNameCodecUtil.encodeQName(value, uri -> context.findModuleStatement(uri)
.map(module -> module.argument().getLocalName())
- .orElseThrow(() -> new IllegalArgumentException("Cannot find module for " + uri))));
+ .orElseThrow(() -> new IllegalArgumentException("Cannot find module for " + uri)));
}
}
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.data.util.codec.TypeAwareCodec;
/**
* @throws IllegalArgumentException if the value does not parse or pass type validation
*/
T parseValue(String str);
+
+ /**
+ * Return the {@link JSONValue} representation of a native value.
+ *
+ * @param value Value in native format
+ * @return A {@link JSONValue}
+ * @throws IllegalArgumentException if the value does not parse or pass type validation
+ */
+ @NonNull JSONValue unparseValue(T value);
}
\ No newline at end of file
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.common.XMLNamespace;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.codec.gson.JSONValue.Kind;
import org.opendaylight.yangtools.yang.data.util.AbstractStringInstanceIdentifierCodec;
import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
return deserialize(str);
}
+ @Override
+ public JSONValue unparseValue(final YangInstanceIdentifier value) {
+ return new JSONValue(serialize(value), Kind.STRING);
+ }
+
@Override
public final void writeValue(final JSONValueWriter ctx, final YangInstanceIdentifier value) throws IOException {
final String str;
--- /dev/null
+/*
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.data.codec.gson;
+
+import static java.util.Objects.requireNonNull;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+
+/**
+ * A serialized JSON string, indicating what kind of value it represents.
+ *
+ * @param rawString unescaped string
+ * @param kind string kind
+ */
+@NonNullByDefault
+public record JSONValue(String rawString, Kind kind) {
+ /**
+ * The kind of a {@link JSONValue}. Indicates the semantics of {@link JSONValue#rawString()}.
+ */
+ public enum Kind {
+ /**
+ * A {@code boolean} value.
+ */
+ BOOLEAN,
+ /**
+ * An {@code empty} value.
+ */
+ EMPTY,
+ /**
+ * A numeric value, excluding {@code int64} and {@code uint64)}.
+ */
+ NUMBER,
+ /**
+ * A string value.
+ */
+ STRING
+ }
+
+ /**
+ * The equivalent on {@link Boolean#FALSE}.
+ */
+ public static final JSONValue FALSE = new JSONValue("false", Kind.BOOLEAN);
+ /**
+ * The equivalent on {@link Boolean#TRUE}.
+ */
+ public static final JSONValue TRUE = new JSONValue("true", Kind.BOOLEAN);
+ /**
+ * The equivalent on {@link org.opendaylight.yangtools.yang.common.Empty#value()}.
+ */
+ public static final JSONValue EMPTY = new JSONValue("[null]", Kind.EMPTY);
+
+ public JSONValue {
+ requireNonNull(rawString);
+ requireNonNull(kind);
+ }
+}
// NOOP since codec is unknown.
LOG.warn("Call of the serializeToWriter method on null codec. No operation performed.");
}
+
+ @Override
+ public JSONValue unparseValue(final Object value) {
+ throw new UnsupportedOperationException();
+ }
}
package org.opendaylight.yangtools.yang.data.codec.gson;
import java.io.IOException;
+import org.opendaylight.yangtools.yang.data.codec.gson.JSONValue.Kind;
import org.opendaylight.yangtools.yang.data.impl.codec.DataStringCodec;
/**
super(codec);
}
+ @Override
+ public JSONValue unparseValue(final T value) {
+ return new JSONValue(value.toString(), Kind.NUMBER);
+ }
+
@Override
public void writeValue(final JSONValueWriter ctx, final T value) throws IOException {
ctx.writeNumber(value);
package org.opendaylight.yangtools.yang.data.codec.gson;
import java.io.IOException;
+import org.opendaylight.yangtools.yang.data.codec.gson.JSONValue.Kind;
import org.opendaylight.yangtools.yang.data.impl.codec.DataStringCodec;
/**
super(codec);
}
+ @Override
+ public JSONValue unparseValue(final T value) {
+ return new JSONValue(serialize(value), Kind.STRING);
+ }
+
@Override
public void writeValue(final JSONValueWriter ctx, final T value) throws IOException {
ctx.writeString(serialize(value));
@SuppressWarnings("checkstyle:illegalCatch")
public final void writeValue(final JSONValueWriter ctx, final T value) throws IOException {
for (var codec : codecs) {
- if (!codec.getDataType().isInstance(value)) {
+ if (codec.getDataType().isInstance(value)) {
+ @SuppressWarnings("unchecked")
+ final var objCodec = (JSONCodec<Object>) codec;
+ try {
+ objCodec.writeValue(ctx, value);
+ return;
+ } catch (RuntimeException e) {
+ LOG.debug("Codec {} failed to serialize {}", codec, value, e);
+ }
+ } else {
LOG.debug("Codec {} cannot accept input {}, skipping it", codec, value);
- continue;
}
+ }
- @SuppressWarnings("unchecked")
- final var objCodec = (JSONCodec<Object>) codec;
- try {
- objCodec.writeValue(ctx, value);
- return;
- } catch (RuntimeException e) {
- LOG.debug("Codec {} failed to serialize {}", codec, value, e);
+ throw new IllegalArgumentException("No codecs could serialize" + value);
+ }
+
+
+ @Override
+ @SuppressWarnings("checkstyle:illegalCatch")
+ public JSONValue unparseValue(final Object value) {
+ for (var codec : codecs) {
+ if (codec.getDataType().isInstance(value)) {
+ @SuppressWarnings("unchecked")
+ final var objCodec = (JSONCodec<Object>) codec;
+ try {
+ return objCodec.unparseValue(value);
+ } catch (RuntimeException e) {
+ LOG.debug("Codec {} failed to unparse {}", codec, value, e);
+ }
+ } else {
+ LOG.debug("Codec {} cannot accept input {}, skipping it", codec, value);
}
}
- throw new IllegalArgumentException("No codecs could serialize" + value);
+ throw new IllegalArgumentException("No codecs could unparse" + value);
}
}