All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.fastjson2.reader.ObjectReaderImplValue Maven / Gradle / Ivy

Go to download

Fastjson is a JSON processor (JSON parser + JSON generator) written in Java

There is a newer version: 2.0.53.android8
Show newest version
package com.alibaba.fastjson2.reader;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.schema.JSONSchema;

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.function.Function;

public class ObjectReaderImplValue
        implements ObjectReader {
    final Type valueType;
    final Class valueClass;
    final long features;
    final Constructor constructor;
    final Method factoryMethod;
    final Function function;
    final JSONSchema schema;
    final Object emptyVariantArgs;
    ObjectReader valueReader;

    public ObjectReaderImplValue(
            Class objectClass,
            Type valueType,
            Class valueClass,
            long features,
            String format,
            Object defaultValue,
            JSONSchema schema,
            Constructor constructor,
            Method factoryMethod,
            Function function
    ) {
        this.valueType = valueType;
        this.valueClass = valueClass;
        this.features = features;
        this.schema = schema;
        this.constructor = constructor;
        this.factoryMethod = factoryMethod;
        this.function = function;

        if (factoryMethod != null && factoryMethod.getParameterCount() == 2) {
            Class varArgType = factoryMethod.getParameterTypes()[1].getComponentType();
            emptyVariantArgs = Array.newInstance(varArgType, 0);
        } else {
            emptyVariantArgs = null;
        }
    }

    @Override
    public T readJSONBObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        return readObject(jsonReader, fieldType, fieldName, features);
    }

    @Override
    public T readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
        if (valueReader == null) {
            valueReader = jsonReader.getObjectReader(valueType);
        }

        I value = (I) valueReader.readObject(jsonReader, fieldType, fieldName, features | this.features);

        if (value == null) {
            return null;
        }

        if (schema != null) {
            schema.validate(value);
        }

        T object;

        if (function != null) {
            try {
                object = function.apply(value);
            } catch (Exception ex) {
                throw new JSONException(jsonReader.info("create object error"), ex);
            }
        } else if (constructor != null) {
            try {
                object = constructor.newInstance(value);
            } catch (Exception ex) {
                throw new JSONException(jsonReader.info("create object error"), ex);
            }
        } else if (factoryMethod != null) {
            try {
                if (emptyVariantArgs != null) {
                    object = (T) factoryMethod.invoke(null, value, emptyVariantArgs);
                } else {
                    object = (T) factoryMethod.invoke(null, value);
                }
            } catch (Exception ex) {
                throw new JSONException(jsonReader.info("create object error"), ex);
            }
        } else {
            throw new JSONException(jsonReader.info("create object error"));
        }

        return object;
    }

    public static  ObjectReaderImplValue of(Class objectClass, Class valueClass, Method method) {
        return new ObjectReaderImplValue(objectClass, valueClass, valueClass, 0L, null, null, null, null, method, null);
    }

    public static  ObjectReaderImplValue of(Class objectClass, Class valueClass, Function function) {
        return new ObjectReaderImplValue(objectClass, valueClass, valueClass, 0L, null, null, null, null, null, function);
    }
}