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

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

There is a newer version: 1.3.7
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.Type;
import java.util.function.Function;

public class ObjectReaderImplValueString
        implements ObjectReader {
    final long features;
    final Function function;
    final JSONSchema schema;

    public ObjectReaderImplValueString(
            Class objectClass,
            long features,
            JSONSchema schema,
            Function function
    ) {
        this.features = features;
        this.schema = schema;
        this.function = function;
    }

    @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 (jsonReader.nextIfNullOrEmptyString()) {
            return null;
        }

        String value = jsonReader.readString();

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

        T object;
        try {
            object = function.apply(value);
        } catch (Exception ex) {
            throw new JSONException(jsonReader.info("create object error"), ex);
        }

        return object;
    }

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

    public static  ObjectReaderImplValueString of(
            Class objectClass,
            long features,
            JSONSchema schema,
            Function function
    ) {
        return new ObjectReaderImplValueString(
                objectClass,
                features,
                schema,
                function
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy