Search in sources :

Example 6 with JsonParser

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();
}
Also used : JsonbException(jakarta.json.bind.JsonbException) JsonParser(jakarta.json.stream.JsonParser)

Example 7 with JsonParser

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;
}
Also used : DeserializationContextImpl(org.eclipse.yasson.internal.DeserializationContextImpl) JsonbException(jakarta.json.bind.JsonbException) Collection(java.util.Collection) JsonParser(jakarta.json.stream.JsonParser)

Example 8 with JsonParser

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);
}
Also used : ClassModel(org.eclipse.yasson.internal.model.ClassModel) JsonParser(jakarta.json.stream.JsonParser)

Example 9 with JsonParser

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;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonParser(jakarta.json.stream.JsonParser)

Example 10 with JsonParser

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LinkedHashMap(java.util.LinkedHashMap) DeserializerBinding(org.eclipse.yasson.internal.components.DeserializerBinding) ClassCustomization(org.eclipse.yasson.internal.model.customization.ClassCustomization) JsonParser(jakarta.json.stream.JsonParser) JsonbCreator(org.eclipse.yasson.internal.model.JsonbCreator) PropertyModel(org.eclipse.yasson.internal.model.PropertyModel) CreatorModel(org.eclipse.yasson.internal.model.CreatorModel) TypeInheritanceConfiguration(org.eclipse.yasson.internal.model.customization.TypeInheritanceConfiguration)

Aggregations

JsonParser (jakarta.json.stream.JsonParser)73 StringReader (java.io.StringReader)23 Test (org.junit.Test)17 JsonbException (jakarta.json.bind.JsonbException)12 InputStream (java.io.InputStream)11 Event (jakarta.json.stream.JsonParser.Event)9 JsonObject (jakarta.json.JsonObject)8 JsonProvider (jakarta.json.spi.JsonProvider)8 GenericArrayType (java.lang.reflect.GenericArrayType)8 JsonString (jakarta.json.JsonString)7 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ParameterizedType (java.lang.reflect.ParameterizedType)7 Type (java.lang.reflect.Type)7 Collection (java.util.Collection)6 JsonbJsonpMapper (co.elastic.clients.json.jsonb.JsonbJsonpMapper)5 JsonGenerator (jakarta.json.stream.JsonGenerator)5 HashMap (java.util.HashMap)5 JsonException (jakarta.json.JsonException)4 JsonParsingException (jakarta.json.stream.JsonParsingException)4 JsonbJsonpMapper (org.opensearch.client.json.jsonb.JsonbJsonpMapper)4