X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-data-codec-gson%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fdata%2Fcodec%2Fgson%2FJSONCodecFactory.java;h=9df9b347dc16c59a19931c650814420d59a8c221;hb=b18ea8d1646806849f0f1ffbba333a04b9ce93f8;hp=3766169f3c8ad8a57919f0006773ab9bf105460e;hpb=de88c42f48005297d3bd8970a14dee1099dc21bc;p=yangtools.git diff --git a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java index 3766169f3c..9df9b347dc 100644 --- a/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java +++ b/yang/yang-data-codec-gson/src/main/java/org/opendaylight/yangtools/yang/data/codec/gson/JSONCodecFactory.java @@ -20,9 +20,11 @@ import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; -import org.opendaylight.yangtools.yang.model.util.IdentityrefType; -import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType; +import org.opendaylight.yangtools.yang.model.util.DerivedType; import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,7 +53,7 @@ public final class JSONCodecFactory { } @Override - public void serializeToWriter(JsonWriter writer, Object value) throws IOException { + public void serializeToWriter(final JsonWriter writer, final Object value) throws IOException { // NOOP since codec is unkwown. LOG.warn("Call of the serializeToWriter method on JSONCodecFactory.NULL_CODEC object. No operation performed."); } @@ -91,41 +93,35 @@ public final class JSONCodecFactory { return new JSONCodecFactory(context); } - private static TypeDefinition resolveBaseTypeFrom(final TypeDefinition type) { - TypeDefinition superType = type; - while (superType.getBaseType() != null) { - superType = superType.getBaseType(); - } - return superType; - } - @SuppressWarnings("unchecked") - private JSONCodec createCodec(DataSchemaNode key, TypeDefinition type) { - TypeDefinition baseType = resolveBaseTypeFrom(type); - if (baseType instanceof LeafrefTypeDefinition) { - return createReferencedTypeCodec(key, (LeafrefTypeDefinition) baseType); - } else if (baseType instanceof IdentityrefType) { - final JSONCodec jsonStringIdentityrefCodec = new JSONStringIdentityrefCodec(schemaContext, - key.getQName().getModule()); + private JSONCodec createCodec(final DataSchemaNode key, final TypeDefinition type) { + final TypeDefinition normalizedType = DerivedType.from(type); + if (normalizedType instanceof LeafrefTypeDefinition) { + return createReferencedTypeCodec(key, (LeafrefTypeDefinition) normalizedType); + } else if (normalizedType instanceof IdentityrefTypeDefinition) { + final JSONCodec jsonStringIdentityrefCodec = + new JSONStringIdentityrefCodec(schemaContext, key.getQName().getModule()); return (JSONCodec) jsonStringIdentityrefCodec; } - return createFromSimpleType(type); + return createFromSimpleType(normalizedType); } - private JSONCodec createReferencedTypeCodec(DataSchemaNode schema, - LeafrefTypeDefinition type) { + private JSONCodec createReferencedTypeCodec(final DataSchemaNode schema, + final LeafrefTypeDefinition type) { // FIXME: Verify if this does indeed support leafref of leafref - TypeDefinition referencedType = + final TypeDefinition referencedType = SchemaContextUtil.getBaseTypeForLeafRef(type, getSchemaContext(), schema); - return createFromSimpleType(referencedType); + return createCodec(schema, referencedType); } @SuppressWarnings("unchecked") - private JSONCodec createFromSimpleType(TypeDefinition type) { - final TypeDefinition baseType = resolveBaseTypeFrom(type); - if (baseType instanceof InstanceIdentifierType) { + private JSONCodec createFromSimpleType(final TypeDefinition type) { + if (type instanceof InstanceIdentifierTypeDefinition) { return (JSONCodec) iidCodec; } + if (type instanceof EmptyTypeDefinition) { + return (JSONCodec) JSONEmptyCodec.INSTANCE; + } final TypeDefinitionAwareCodec codec = TypeDefinitionAwareCodec.from(type); if (codec == null) { @@ -140,7 +136,7 @@ public final class JSONCodecFactory { return schemaContext; } - JSONCodec codecFor(DataSchemaNode schema) { + JSONCodec codecFor(final DataSchemaNode schema) { return codecs.getUnchecked(schema); }