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

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

Go to download

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

The newest version!
package com.alibaba.fastjson2.reader;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.util.TypeUtils;

import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.atomic.AtomicIntegerArray;

final class FieldReaderAtomicIntegerArrayReadOnly
        extends FieldReader {
    FieldReaderAtomicIntegerArrayReadOnly(
            String fieldName,
            Class fieldType,
            int ordinal,
            Method method
    ) {
        super(fieldName, fieldType, fieldType, ordinal, 0, null, null, null, method, null);
    }

    @Override
    public boolean isReadOnly() {
        return true;
    }

    @Override
    public void accept(T object, Object value) {
        if (value == null) {
            return;
        }

        try {
            AtomicIntegerArray atomic = (AtomicIntegerArray) method.invoke(object);
            if (value instanceof AtomicIntegerArray) {
                AtomicIntegerArray array = (AtomicIntegerArray) value;
                for (int i = 0; i < array.length(); i++) {
                    atomic.set(i, array.get(i));
                }
            } else {
                List values = (List) value;
                for (int i = 0; i < values.size(); i++) {
                    int itemValue = TypeUtils.toIntValue(values.get(i));
                    atomic.set(i, itemValue);
                }
            }
        } catch (Exception e) {
            throw new JSONException("set " + fieldName + " error", e);
        }
    }

    @Override
    public void readFieldValue(JSONReader jsonReader, T object) {
        if (jsonReader.readIfNull()) {
            return;
        }

        AtomicIntegerArray atomic;
        try {
            atomic = (AtomicIntegerArray) method.invoke(object);
        } catch (Exception e) {
            throw new JSONException(jsonReader.info("set " + fieldName + " error"), e);
        }

        if (jsonReader.nextIfArrayStart()) {
            for (int i = 0; ; ++i) {
                if (jsonReader.nextIfArrayEnd()) {
                    break;
                }

                int value = jsonReader.readInt32Value();
                if (atomic != null && i < atomic.length()) {
                    atomic.set(i, value);
                }
            }
        }
    }

    @Override
    public Object readFieldValue(JSONReader jsonReader) {
        if (jsonReader.nextIfNull()) {
            return null;
        }
        return jsonReader.readArray(Integer.class);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy