+ private static TypeDefinition<?> resolveBaseTypeFrom(final TypeDefinition<?> type) {
+ TypeDefinition<?> superType = type;
+ while (superType.getBaseType() != null) {
+ superType = superType.getBaseType();
+ }
+ return superType;
+ }
+
+ private JSONCodec<Object> createCodec(DataSchemaNode key, TypeDefinition<?> type) {
+ TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
+ if (baseType instanceof LeafrefTypeDefinition) {
+ return createReferencedTypeCodec(key, (LeafrefTypeDefinition) baseType);
+ }
+ return createFromSimpleType(type);
+ }
+
+ private JSONCodec<Object> createReferencedTypeCodec(DataSchemaNode schema,
+ LeafrefTypeDefinition type) {
+ // FIXME: Verify if this does indeed support leafref of leafref
+ TypeDefinition<?> referencedType =
+ SchemaContextUtil.getBaseTypeForLeafRef(type, getSchemaContext(), schema);
+ return createFromSimpleType(referencedType);
+ }
+
+ @SuppressWarnings("unchecked")
+ private JSONCodec<Object> createFromSimpleType(TypeDefinition<?> type) {
+ final TypeDefinition<?> baseType = resolveBaseTypeFrom(type);
+ if (baseType instanceof InstanceIdentifierType) {
+ return (JSONCodec<Object>) iidCodec;
+ }
+ if (baseType instanceof IdentityrefType) {
+ return (JSONCodec<Object>) idrefCodec;
+ }
+
+ 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);
+ }
+