use of jakarta.json.stream.JsonParser in project yasson by eclipse-ee4j.
the class ObjectDeserializer method deserialize.
@Override
public Object deserialize(JsonParser parser, DeserializationContextImpl context) {
String key = null;
while (parser.hasNext()) {
final JsonParser.Event next = parser.next();
context.setLastValueEvent(next);
switch(next) {
case KEY_NAME:
key = renamer.apply(parser.getString());
break;
case VALUE_NULL:
case START_OBJECT:
case START_ARRAY:
case VALUE_STRING:
case VALUE_NUMBER:
case VALUE_FALSE:
case VALUE_TRUE:
if (propertyDeserializerChains.containsKey(key)) {
try {
propertyDeserializerChains.get(key).deserialize(parser, context);
} catch (JsonbException e) {
throw new JsonbException("Unable to deserialize property '" + key + "' because of: " + e.getMessage(), e);
}
} else if (failOnUnknownProperty && !ignoredProperties.contains(key)) {
throw new JsonbException(Messages.getMessage(MessageKeys.UNKNOWN_JSON_PROPERTY, key, rawClass));
}
break;
case END_ARRAY:
break;
case END_OBJECT:
return context.getInstance();
default:
throw new JsonbException("Unexpected state: " + next);
}
}
return context.getInstance();
}
use of jakarta.json.stream.JsonParser in project yasson by eclipse-ee4j.
the class CollectionDeserializer method deserialize.
@SuppressWarnings("unchecked")
@Override
public Object deserialize(JsonParser parser, DeserializationContextImpl context) {
Collection<Object> collection = (Collection<Object>) context.getInstance();
while (parser.hasNext()) {
final JsonParser.Event next = parser.next();
context.setLastValueEvent(next);
switch(next) {
case VALUE_NULL:
case START_OBJECT:
case START_ARRAY:
case VALUE_STRING:
case VALUE_TRUE:
case VALUE_FALSE:
case VALUE_NUMBER:
DeserializationContextImpl newContext = new DeserializationContextImpl(context);
collection.add(delegate.deserialize(parser, newContext));
break;
case END_ARRAY:
return collection;
default:
throw new JsonbException("Unexpected state: " + next);
}
}
return collection;
}
use of jakarta.json.stream.JsonParser in project yasson by eclipse-ee4j.
the class DeserializationModelCreator method createNewChain.
private ModelDeserializer<JsonParser> createNewChain(LinkedList<Type> chain, ModelDeserializer<Object> memberDeserializer, Class<?> rawType, Type type, Customization propertyCustomization) {
ClassModel classModel = jsonbContext.getMappingContext().getOrCreateClassModel(rawType);
ModelDeserializer<JsonParser> modelDeserializer = deserializerChain(chain, type, propertyCustomization, classModel);
return new ContextSwitcher(memberDeserializer, modelDeserializer);
}
use of jakarta.json.stream.JsonParser in project yasson by eclipse-ee4j.
the class DeserializationModelCreator method createOptionalDeserializer.
private OptionalDeserializer createOptionalDeserializer(LinkedList<Type> chain, Type type, Customization propertyCustomization, CachedItem cachedItem) {
Type colType = type instanceof ParameterizedType ? ((ParameterizedType) type).getActualTypeArguments()[0] : Object.class;
ModelDeserializer<JsonParser> typeProcessor = typeProcessor(chain, colType, propertyCustomization, JustReturn.instance());
OptionalDeserializer optionalDeserializer = new OptionalDeserializer(typeProcessor, JustReturn.instance());
models.put(cachedItem, optionalDeserializer);
return optionalDeserializer;
}
use of jakarta.json.stream.JsonParser in project yasson by eclipse-ee4j.
the class DeserializationModelCreator method createObjectDeserializer.
private ModelDeserializer<JsonParser> createObjectDeserializer(LinkedList<Type> chain, Type type, Customization propertyCustomization, ClassModel classModel, Class<?> rawType, CachedItem cachedItem) {
ClassCustomization classCustomization = classModel.getClassCustomization();
Optional<DeserializerBinding<?>> deserializerBinding = userDeserializer(type, (ComponentBoundCustomization) propertyCustomization);
if (deserializerBinding.isPresent()) {
UserDefinedDeserializer user = new UserDefinedDeserializer(deserializerBinding.get().getJsonbDeserializer(), JustReturn.instance(), type, classCustomization);
models.put(cachedItem, user);
return user;
}
JsonbCreator creator = classCustomization.getCreator();
boolean hasCreator = creator != null;
List<String> params = hasCreator ? creatorParamsList(creator) : Collections.emptyList();
Function<String, String> renamer = propertyRenamer();
Map<String, ModelDeserializer<JsonParser>> processors = new LinkedHashMap<>();
Map<String, ModelDeserializer<Object>> defaultCreatorValues = new HashMap<>();
for (PropertyModel propertyModel : classModel.getSortedProperties()) {
if (!propertyModel.isWritable() || params.contains(propertyModel.getReadName())) {
continue;
}
ModelDeserializer<JsonParser> modelDeserializer = memberTypeProcessor(chain, propertyModel, hasCreator);
processors.put(renamer.apply(propertyModel.getReadName()), modelDeserializer);
}
for (String s : params) {
CreatorModel creatorModel = creator.findByName(s);
ModelDeserializer<JsonParser> modelDeserializer = typeProcessor(chain, creatorModel.getType(), creatorModel.getCustomization(), JustReturn.instance());
String parameterName = renamer.apply(creatorModel.getName());
processors.put(parameterName, modelDeserializer);
if (creatorModel.getCustomization().isRequired()) {
defaultCreatorValues.put(parameterName, new RequiredCreatorParameter(parameterName));
} else {
Class<?> rawParamType = ReflectionUtils.getRawType(creatorModel.getType());
defaultCreatorValues.put(parameterName, DEFAULT_CREATOR_VALUES.getOrDefault(rawParamType, NULL_PROVIDER));
}
}
ModelDeserializer<JsonParser> instanceCreator;
TypeInheritanceConfiguration typeInheritanceConfiguration = classCustomization.getPolymorphismConfig();
Set<String> ignoredProperties = collectIgnoredProperties(typeInheritanceConfiguration);
boolean failOnUnknownProperties = jsonbContext.getConfigProperties().getConfigFailOnUnknownProperties();
if (hasCreator) {
instanceCreator = new JsonbCreatorDeserializer(processors, defaultCreatorValues, creator, rawType, renamer, failOnUnknownProperties, ignoredProperties);
} else {
ModelDeserializer<JsonParser> typeWrapper = new ObjectDeserializer(processors, renamer, rawType, failOnUnknownProperties, ignoredProperties);
instanceCreator = new DefaultObjectInstanceCreator(typeWrapper, rawType, classModel.getDefaultConstructor());
}
PositionChecker positionChecker = new PositionChecker(instanceCreator, rawType, Event.START_OBJECT);
if (typeInheritanceConfiguration != null && !typeInheritanceConfiguration.isInherited()) {
instanceCreator = new InheritanceInstanceCreator(rawType, this, typeInheritanceConfiguration, positionChecker);
positionChecker = new PositionChecker(instanceCreator, rawType, Event.START_OBJECT);
}
ModelDeserializer<JsonParser> nullChecker = new NullCheckDeserializer(positionChecker, JustReturn.instance());
models.put(cachedItem, nullChecker);
return nullChecker;
}
Aggregations