+ @SuppressWarnings("unchecked")
+ private JSONCodec<Object> 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<Object>) jsonStringIdentityrefCodec;
+ }
+ return createFromSimpleType(normalizedType);
+ }
+
+ private JSONCodec<Object> createReferencedTypeCodec(final DataSchemaNode schema,
+ final LeafrefTypeDefinition type) {
+ // FIXME: Verify if this does indeed support leafref of leafref
+ final TypeDefinition<?> referencedType =
+ SchemaContextUtil.getBaseTypeForLeafRef(type, getSchemaContext(), schema);
+ Verify.verifyNotNull(referencedType, "Unable to find base type for leafref node '%s'.", schema.getPath());
+ return createCodec(schema, referencedType);
+ }
+
+ @SuppressWarnings("unchecked")
+ private JSONCodec<Object> createFromSimpleType(final TypeDefinition<?> type) {
+ if (type instanceof InstanceIdentifierTypeDefinition) {
+ return (JSONCodec<Object>) iidCodec;
+ }
+ if (type instanceof EmptyTypeDefinition) {
+ return JSONEmptyCodec.INSTANCE;
+ }
+
+ final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
+ if (codec == null) {
+ LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName()
+ .getLocalName());
+ return NULL_CODEC;
+ }
+ return (JSONCodec<Object>) AbstractJSONCodec.create(codec);
+ }
+