import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
-import com.google.gson.stream.JsonWriter;
import java.io.IOException;
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.common.XMLNamespace;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringInstanceIdentifierCodec;
+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;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.util.LeafrefResolver;
-abstract sealed class JSONInstanceIdentifierCodec extends AbstractModuleStringInstanceIdentifierCodec
+abstract sealed class JSONInstanceIdentifierCodec extends AbstractStringInstanceIdentifierCodec
implements JSONCodec<YangInstanceIdentifier> {
static final class Lhotka02 extends JSONInstanceIdentifierCodec {
Lhotka02(final EffectiveModelContext context, final JSONCodecFactory jsonCodecFactory) {
}
@Override
- protected final Module moduleForPrefix(final String prefix) {
- final var modules = context.findModules(prefix).iterator();
- return modules.hasNext() ? modules.next() : null;
+ protected final QNameModule moduleForPrefix(final String prefix) {
+ final var modules = context.findModuleStatements(prefix).iterator();
+ return modules.hasNext() ? modules.next().localQNameModule() : null;
}
@Override
protected final String prefixForNamespace(final XMLNamespace namespace) {
- final var modules = context.findModules(namespace).iterator();
- return modules.hasNext() ? modules.next().getName() : null;
+ final var modules = context.findModuleStatements(namespace).iterator();
+ return modules.hasNext() ? modules.next().argument().getLocalName() : null;
}
@Override
protected final Object deserializeKeyValue(final DataSchemaNode schemaNode, final LeafrefResolver resolver,
final String value) {
requireNonNull(schemaNode, "schemaNode cannot be null");
- checkArgument(schemaNode instanceof LeafSchemaNode, "schemaNode must be of type LeafSchemaNode");
- return codecFactory.codecFor((LeafSchemaNode) schemaNode, resolver).parseValue(null, value);
+ if (schemaNode instanceof LeafSchemaNode leafSchemaNode) {
+ return codecFactory.codecFor(leafSchemaNode, resolver).parseValue(value);
+ } else if (schemaNode instanceof LeafListSchemaNode leafListSchemaNode) {
+ return codecFactory.codecFor(leafListSchemaNode, resolver).parseValue(value);
+ }
+ throw new IllegalArgumentException("schemaNode " + schemaNode
+ + " must be of type LeafSchemaNode or LeafListSchemaNode");
}
@Override
}
@Override
- public final YangInstanceIdentifier parseValue(final Object ctx, final String str) {
+ public final YangInstanceIdentifier parseValue(final String str) {
return deserialize(str);
}
@Override
- public final void writeValue(final JsonWriter ctx, final YangInstanceIdentifier value) throws IOException {
- ctx.value(serialize(value));
+ 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;
+ try {
+ str = serialize(value);
+ } catch (IllegalArgumentException e) {
+ throw new IOException("Failed to encode instance-identifier", e);
+ }
+ ctx.writeString(str);
}
}