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

shade.com.alibaba.fastjson2.reader.ObjectReaders Maven / Gradle / Ivy

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

import com.alibaba.fastjson2.TypeReference;
import com.alibaba.fastjson2.function.*;
import com.alibaba.fastjson2.util.BeanUtils;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.*;

public class ObjectReaders {
    public static  ObjectReader of(
            Supplier defaultCreator,
            FieldReader... fieldReaders
    ) {
        return ObjectReaderCreator.INSTANCE.createObjectReader(null, defaultCreator, fieldReaders);
    }

    public static  ObjectReader of(
            Class objectClass,
            Supplier defaultCreator,
            FieldReader... fieldReaders
    ) {
        return ObjectReaderCreator.INSTANCE.createObjectReader(objectClass, defaultCreator, fieldReaders);
    }

    public static  ObjectReader ofString(Function function) {
        return new ObjectReaderImplFromString<>(null, function);
    }

    public static  ObjectReader ofInt(IntFunction function) {
        return new ObjectReaderImplFromInt<>(null, function);
    }

    public static  ObjectReader ofLong(LongFunction function) {
        return new ObjectReaderImplFromLong<>(null, function);
    }

    public static  ObjectReader fromCharArray(Function function) {
        return (ObjectReader) new ObjectReaderImplCharValueArray(function);
    }

    public static  ObjectReader fromByteArray(Function function) {
        return (ObjectReader) new ObjectReaderImplInt8ValueArray(function, "base64");
    }

    public static  ObjectReader fromShortArray(Function function) {
        return (ObjectReader) new ObjectReaderImplInt16ValueArray(function);
    }

    public static  ObjectReader fromIntArray(Function function) {
        return (ObjectReader) new ObjectReaderImplInt32ValueArray(null, function);
    }

    public static  ObjectReader fromLongArray(Function function) {
        return (ObjectReader) new ObjectReaderImplInt64ValueArray(null, function);
    }

    public static  ObjectReader fromFloatArray(Function function) {
        return (ObjectReader) new ObjectReaderImplFloatValueArray(function);
    }

    public static  ObjectReader fromDoubleArray(Function function) {
        return (ObjectReader) new ObjectReaderImplDoubleValueArray(function);
    }

    public static  ObjectReader fromBigDecimal(Function function) {
        return (ObjectReader) new ObjectReaderImplBigDecimal(function);
    }

    public static  ObjectReader objectReader(
            Class objectClass,
            Supplier defaultCreator,
            FieldReader... fieldReaders
    ) {
        return ObjectReaderCreator.INSTANCE.createObjectReader(objectClass, defaultCreator, fieldReaders);
    }

    public static  ObjectReader ofReflect(Class objectType) {
        return ObjectReaderCreator.INSTANCE.createObjectReader(objectType);
    }

    public static  ObjectReader of(Class objectType) {
        return ObjectReaderCreator.INSTANCE.createObjectReader(objectType);
    }

    public static  ObjectReader objectReader(
            Function, T> creator,
            FieldReader... fieldReaders) {
        return ObjectReaderCreator.INSTANCE.createObjectReaderNoneDefaultConstructor(
                null,
                creator,
                fieldReaders
        );
    }

    public static FieldReader fieldReader(String fieldName, Class fieldClass) {
        return ObjectReaderCreator.INSTANCE.createFieldReader(null, fieldName, fieldClass, fieldClass, null);
    }

    public static FieldReader fieldReader(String fieldName, Type fieldType, Class fieldClass) {
        return ObjectReaderCreator.INSTANCE.createFieldReader(null, fieldName, fieldType, fieldClass, null);
    }

    public static  FieldReader fieldReaderBool(String fieldName, ObjBoolConsumer function) {
        return new FieldReaderBoolValFunc<>(fieldName, 0, null, null, function);
    }

    public static  FieldReader fieldReaderByte(String fieldName, ObjByteConsumer function) {
        return new FieldReaderInt8ValueFunc<>(fieldName, 0, null, null, function);
    }

    public static  FieldReader fieldReaderShort(String fieldName, ObjShortConsumer function) {
        return new FieldReaderInt16ValueFunc<>(fieldName, 0, 0L, null, null, null, null, null, function);
    }

    public static  FieldReader fieldReaderInt(String fieldName, ObjIntConsumer function) {
        return new FieldReaderInt32ValueFunc<>(fieldName, 0, null, null, null, function);
    }

    public static  FieldReader fieldReaderLong(String fieldName, ObjLongConsumer function) {
        return new FieldReaderInt64ValueFunc<>(fieldName, 0, null, null, null, function);
    }

    public static  FieldReader fieldReaderChar(String fieldName, ObjCharConsumer function) {
        return new FieldReaderCharValueFunc<>(fieldName, 0, null, null, null, null, function);
    }

    public static  FieldReader fieldReaderFloat(String fieldName, ObjFloatConsumer function) {
        return new FieldReaderFloatValueFunc<>(fieldName, 0, null, null, null, function);
    }

    public static  FieldReader fieldReaderDouble(String fieldName, ObjDoubleConsumer function) {
        return new FieldReaderDoubleValueFunc<>(fieldName, 0, null, null, null, function);
    }

    public static  FieldReader fieldReaderString(
            String fieldName,
            BiConsumer function
    ) {
        return ObjectReaderCreator.INSTANCE.createFieldReader(fieldName, String.class, String.class, null, function);
    }

    public static  FieldReader fieldReader(
            String fieldName,
            Class fieldClass,
            BiConsumer function
    ) {
        return ObjectReaderCreator.INSTANCE.createFieldReader(fieldName, fieldClass, fieldClass, null, function);
    }

    public static  FieldReader fieldReader(
            String fieldName,
            Type fieldType,
            Class fieldClass,
            BiConsumer function
    ) {
        return ObjectReaderCreator.INSTANCE.createFieldReader(fieldName, fieldType, fieldClass, null, function);
    }

    public static  FieldReader fieldReader(
            String fieldName,
            Type fieldType,
            BiConsumer consumer,
            ObjectReader fieldObjectReader
    ) {
//        return new FieldReaderObjectFunc2<>(fieldObjectReader, consumer, fieldType, fieldName);
        return new FieldReaderObjectFunc<>(
                fieldName,
                fieldType,
                (Class) TypeUtils.getClass(fieldType),
                0,
                0,
                null,
                null,
                null,
                null,
                null,
                consumer,
                fieldObjectReader
        );
    }

    public static  FieldReader fieldReaderList(
            String fieldName,
            Type itemType,
            Supplier> listCreator,
            BiConsumer> function,
            ObjectReader itemObjectReader
    ) {
        return new FieldReaderListFuncImpl<>(listCreator, itemObjectReader, function, itemType, fieldName);
    }

    public static  FieldReader fieldReaderList(
            String fieldName,
            Type itemType,
            Supplier> listCreator,
            BiConsumer> function
    ) {
        return new FieldReaderListFuncImpl<>(listCreator, null, function, itemType, fieldName);
    }

    public static  FieldReader fieldReaderListStr(
            String fieldName,
            BiConsumer> function
    ) {
        return new FieldReaderListFuncImpl<>(ArrayList::new, null, function, String.class, fieldName);
    }

    public static  FieldReader fieldReaderList(
            String fieldName,
            Type itemType,
            BiConsumer> function
    ) {
        return fieldReaderList(fieldName, itemType, ArrayList::new, function);
    }

    /**
     * @since 2.0.34
     */
    public static  FieldReader fieldReaderMap(
            String fieldName,
            Class mapClass,
            Type keyType,
            Type valueType,
            BiConsumer function
    ) {
        return new FieldReaderObject(
                fieldName,
                TypeReference.parametricType(mapClass, keyType, valueType),
                mapClass,
                0,
                0,
                null,
                null,
                null,
                null,
                null,
                null,
                function
        );
    }

    public static FieldReader fieldReaderWithField(String fieldName, Class objectClass) {
        Field field = BeanUtils.getDeclaredField(objectClass, fieldName);
        return ObjectReaderCreator.INSTANCE.createFieldReader(
                fieldName,
                field
        );
    }

    public static FieldReader fieldReaderWithField(String name, Class objectClass, String fieldName) {
        Field field = BeanUtils.getDeclaredField(objectClass, fieldName);
        return ObjectReaderCreator.INSTANCE.createFieldReader(
                name,
                field
        );
    }

    public static FieldReader fieldReaderWithMethod(String name, Class objectClass, String methodName) {
        Method method = BeanUtils.getSetter(objectClass, methodName);
        return ObjectReaderCreator.INSTANCE.createFieldReader(
                name,
                method
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy