/** * Instance is not created in case of array items, because, we don't know how long it should be * till parser ends parsing. */ private JsonbDeserializer<?> createArrayItem(Class<?> componentType) { if (componentType == byte.class) { return new ByteArrayDeserializer(this); } else if (componentType == short.class) { return new ShortArrayDeserializer(this); } else if (componentType == int.class) { return new IntArrayDeserializer(this); } else if (componentType == long.class) { return new LongArrayDeserializer(this); } else if (componentType == float.class) { return new FloatArrayDeserializer(this); } else if (componentType == double.class) { return new DoubleArrayDeserializer(this); } else { return new ObjectArrayDeserializer(this); } }
/** * Called during context creation, introspecting user components provided with JsonbConfig. */ void init() { final JsonbSerializer<?>[] serializers = (JsonbSerializer<?>[])jsonbContext.getConfig().getProperty(JsonbConfig.SERIALIZERS).orElseGet(()->new JsonbSerializer<?>[]{}); for (JsonbSerializer serializer : serializers) { SerializerBinding serializerBinding = introspectSerializerBinding(serializer.getClass(), serializer); addSerializer(serializerBinding.getBindingType(), serializerBinding); } final JsonbDeserializer<?>[] deserializers = (JsonbDeserializer<?>[])jsonbContext.getConfig().getProperty(JsonbConfig.DESERIALIZERS).orElseGet(()->new JsonbDeserializer<?>[]{}); for (JsonbDeserializer deserializer : deserializers) { DeserializerBinding deserializerBinding = introspectDeserializerBinding(deserializer.getClass(), deserializer); addDeserializer(deserializerBinding.getBindingType(), deserializerBinding); } final JsonbAdapter<?, ?>[] adapters = (JsonbAdapter<?, ?>[]) jsonbContext.getConfig().getProperty(JsonbConfig.ADAPTERS).orElseGet(()->new JsonbAdapter<?, ?>[]{}); for (JsonbAdapter<?, ?> adapter : adapters) { AdapterBinding adapterBinding = introspectAdapterBinding(adapter.getClass(), adapter); addAdapter(adapterBinding.getBindingType(), adapterBinding); } }
@SuppressWarnings("unchecked") private JsonbDeserializer<?> wrapAdapted(Optional<AdapterBinding> adapterInfoOptional, JsonbDeserializer<?> item) { final Optional<JsonbDeserializer<?>> adaptedDeserializerOptional = adapterInfoOptional.map(adapterInfo -> { setAdaptedItemCaptor((AdaptedObjectDeserializer)wrapper, item); return (JsonbDeserializer<?>)wrapper; }); return adaptedDeserializerOptional.orElse(item); }
@Override public Optional<?> deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { JsonbContext jsonbContext = ((ProcessingContext) ctx).getJsonbContext(); final JsonParser.Event lastEvent = ((JsonbParser) parser).getCurrentLevel().getLastEvent(); JsonbDeserializer deserializer = new DeserializerBuilder(jsonbContext).withType(optionalValueType) .withWrapper(wrapper).withJsonValueType(lastEvent).build(); return Optional.of(deserializer.deserialize(parser, ctx, optionalValueType)); }
/** * Checks for {@link JsonbDeserializer} on a property. * @param property property not null * @return components info */ public DeserializerBinding getDeserializerBinding(Property property) { Objects.requireNonNull(property); JsonbTypeDeserializer deserializerAnnotation = getAnnotationFromProperty(JsonbTypeDeserializer.class, property) .orElseGet(()-> getAnnotationFromPropertyType(property, JsonbTypeDeserializer.class)); if (deserializerAnnotation == null) { return null; } final Class<? extends JsonbDeserializer> deserializerClass = deserializerAnnotation.value(); return jsonbContext.getComponentMatcher().introspectDeserializerBinding(deserializerClass, null); }
/** * If an instance of deserializerClass is present in context and is bound for same type, return that instance. * Otherwise create new instance and set it to context. * * @param deserializerClass class of deserializer * @param instance instance to use if not cached already * @return wrapper used in property models */ @SuppressWarnings("unchecked") DeserializerBinding introspectDeserializerBinding(Class<? extends JsonbDeserializer> deserializerClass, JsonbDeserializer instance) { final ParameterizedType deserializerRuntimeType = ReflectionUtils.findParameterizedType(deserializerClass, JsonbDeserializer.class); Type deserializerBindingType = resolveTypeArg(deserializerRuntimeType.getActualTypeArguments()[0], deserializerClass.getClass()); final ComponentBindings componentBindings = getBindingInfo(deserializerBindingType); if (componentBindings.getDeserializer() != null && componentBindings.getDeserializer().getClass().equals(deserializerClass)) { return componentBindings.getDeserializer(); } else { JsonbDeserializer deserializer = instance != null ? instance : jsonbContext.getComponentInstanceCreator() .getOrCreateComponent(deserializerClass); return new DeserializerBinding(deserializerBindingType, deserializer); } }
private <T,A> void setAdaptedItemCaptor(AdaptedObjectDeserializer<T,A> decoratorItem, JsonbDeserializer<T> adaptedItem) { decoratorItem.setAdaptedTypeDeserializer(adaptedItem); }
@Override protected void deserializeNext(JsonParser parser, Unmarshaller context) { final JsonbDeserializer<?> deserializer = newCollectionOrMapItem(mapValueRuntimeType, context.getJsonbContext()); appendResult(deserializer.deserialize(parser, context, mapValueRuntimeType)); }
@Override protected void deserializeNext(JsonParser parser, Unmarshaller context) { final JsonbDeserializer<?> deserializer = newCollectionOrMapItem(collectionValueType, context.getJsonbContext()); appendResult(deserializer.deserialize(parser, context, collectionValueType)); }
protected JsonbDeserializer<?> newCollectionOrMapItem(Type valueType, JsonbContext ctx) { Type actualValueType = ReflectionUtils.resolveType(this, valueType); return newUnmarshallerItemBuilder(ctx).withType(actualValueType).withModel(getModel()).build(); }
@Override protected void deserializeNext(JsonParser parser, Unmarshaller context) { final JsonbDeserializer<?> deserializer = newUnmarshallerItemBuilder(context.getJsonbContext()).withType(componentClass) .withModel(model).build(); appendResult(deserializer.deserialize(parser, context, componentClass)); }
@SuppressWarnings("unchecked") private <T> T deserializeItem(Type type, JsonParser parser) { final JsonbDeserializer<?> item = new DeserializerBuilder(jsonbContext).withWrapper(current) .withType(type).withJsonValueType(getRootEvent(parser)).withModel(new ContainerModel(type, null)).build(); return (T) item.deserialize(parser, this, type); }
/** * Sets adapted item. * * @param adaptedTypeDeserializer Adapted item to set. */ public void setAdaptedTypeDeserializer(JsonbDeserializer<A> adaptedTypeDeserializer) { this.adaptedTypeDeserializer = adaptedTypeDeserializer; }
/** *Creates a new instance. * * @param bindingType Binding type. * @param jsonbDeserializer Deserializer. */ public DeserializerBinding(Type bindingType, JsonbDeserializer<T> jsonbDeserializer) { super(bindingType); this.jsonbDeserializer = jsonbDeserializer; }
/** * Gets deserializer if any. * * @return Deserializer. */ public JsonbDeserializer<T> getJsonbDeserializer() { return jsonbDeserializer; }