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

com.alibaba.fastjson2.writer.ObjectWriters 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.writer;

import com.alibaba.fastjson2.codec.FieldInfo;
import com.alibaba.fastjson2.function.*;
import com.alibaba.fastjson2.util.ParameterizedTypeImpl;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.List;

public class ObjectWriters {
    static ObjectWriterCreator INSTANCE = ObjectWriterCreator.INSTANCE;

    public static ObjectWriter ofReflect(Class objectType) {
        return ObjectWriterCreator.INSTANCE.createObjectWriter(objectType);
    }

    public static ObjectWriter objectWriter(Class objectType) {
        return INSTANCE.createObjectWriter(objectType);
    }

    public static ObjectWriter objectWriter(Class objectType, FieldWriter... fieldWriters) {
        return INSTANCE.createObjectWriter(objectType, fieldWriters);
    }

    public static  ObjectWriter of(Class objectType, FieldWriter... fieldWriters) {
        return INSTANCE.createObjectWriter(objectType, fieldWriters);
    }

    public static ObjectWriter objectWriter(Class objectType, long features, FieldWriter... fieldWriters) {
        return INSTANCE.createObjectWriter(objectType, features, fieldWriters);
    }

    public static ObjectWriter objectWriter(FieldWriter... fieldWriters) {
        return INSTANCE.createObjectWriter(fieldWriters);
    }

    public static  ObjectWriter ofToString(Function function) {
        return INSTANCE.createObjectWriter(
                INSTANCE.createFieldWriter(
                        null,
                        null,
                        "toString",
                        0,
                        FieldInfo.VALUE_MASK,
                        null,
                        null,
                        String.class,
                        String.class,
                        null,
                        function
                )
        );
    }

    public static  ObjectWriter ofToInt(ToIntFunction function) {
        return INSTANCE.createObjectWriter(
                new FieldWriterInt32ValFunc(
                        "toInt",
                        0,
                        FieldInfo.VALUE_MASK,
                        null,
                        null,
                        null,
                        function
                )
        );
    }

    public static  ObjectWriter ofToLong(ToLongFunction function) {
        return INSTANCE.createObjectWriter(
                new FieldWriterInt64ValFunc(
                        "toLong",
                        0,
                        FieldInfo.VALUE_MASK,
                        null,
                        null,
                        null,
                        function
                )
        );
    }

    public static  ObjectWriter ofToByteArray(Function function) {
        return new ObjectWriterImplInt8ValueArray(function);
    }

    public static  ObjectWriter ofToShortArray(Function function) {
        return new ObjectWriterImplInt16ValueArray(function);
    }

    public static  ObjectWriter ofToIntArray(Function function) {
        return new ObjectWriterImplInt32ValueArray(function);
    }

    public static  ObjectWriter ofToLongArray(Function function) {
        return new ObjectWriterImplInt64ValueArray(function);
    }

    public static  ObjectWriter ofToCharArray(Function function) {
        return new ObjectWriterImplCharValueArray(function);
    }

    public static  ObjectWriter ofToFloatArray(Function function) {
        return new ObjectWriterImplFloatValueArray(function, null);
    }

    public static  ObjectWriter ofToDoubleArray(Function function) {
        return new ObjectWriterImplDoubleValueArray(function, null);
    }

    public static  ObjectWriter ofToBooleanArray(Function function) {
        return new ObjectWriterImplBoolValueArray(function);
    }

    public static  ObjectWriter ofToBooleanArray(
            ToIntFunction functionSize,
            BiFunction functionGet
    ) {
        return new ObjectWriterImplBoolValueArrayLambda(functionSize, functionGet);
    }

    public static  ObjectWriter ofToBigDecimal(Function function) {
        return new ObjectWriterImplBigDecimal(null, function);
    }

    public static  ObjectWriter ofToBooleanArray(
            ToLongFunction functionSize,
            BiFunction functionGet
    ) {
        ToIntFunction functionSizeInt = o -> (int) functionSize.applyAsLong(o);
        return new ObjectWriterImplBoolValueArrayLambda(functionSizeInt, functionGet);
    }

    public static  FieldWriter fieldWriter(String fieldName, ToLongFunction function) {
        return INSTANCE.createFieldWriter(fieldName, function);
    }

    public static  FieldWriter fieldWriter(String fieldName, ToIntFunction function) {
        return INSTANCE.createFieldWriter(fieldName, function);
    }

    public static  FieldWriter fieldWriter(String fieldName, ToShortFunction function) {
        return INSTANCE.createFieldWriter(fieldName, function);
    }

    public static  FieldWriter fieldWriter(String fieldName, ToByteFunction function) {
        return INSTANCE.createFieldWriter(fieldName, function);
    }

    public static  FieldWriter fieldWriter(String fieldName, ToFloatFunction function) {
        return INSTANCE.createFieldWriter(fieldName, function);
    }

    public static  FieldWriter fieldWriter(String fieldName, ToDoubleFunction function) {
        return INSTANCE.createFieldWriter(fieldName, function);
    }

    public static  FieldWriter fieldWriter(String fieldName, Predicate function) {
        return INSTANCE.createFieldWriter(fieldName, function);
    }

    public static  FieldWriter fieldWriter(String fieldName, Function function) {
        return INSTANCE.createFieldWriter(fieldName, String.class, function);
    }

    public static  FieldWriter fieldWriter(String fieldName, Class fieldClass, Function function) {
        return INSTANCE.createFieldWriter(fieldName, fieldClass, function);
    }

    public static  FieldWriter fieldWriter(String fieldName,
                                                 Type fieldType,
                                                 Class fieldClass,
                                                 Function function) {
        return INSTANCE.createFieldWriter(fieldName, fieldType, fieldClass, function);
    }

    public static  FieldWriter fieldWriterList(String fieldName,
                                                     Class itemType,
                                                     Function> function) {
        ParameterizedType listType;
        if (itemType == String.class) {
            listType = TypeUtils.PARAM_TYPE_LIST_STR;
        } else {
            listType = new ParameterizedTypeImpl(List.class, itemType);
        }
        return INSTANCE.createFieldWriter(fieldName, listType, List.class, function);
    }

    public static  FieldWriter fieldWriterListString(String fieldName, Function> function) {
        return INSTANCE.createFieldWriter(fieldName, TypeUtils.PARAM_TYPE_LIST_STR, List.class, function);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy