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

hydraql.shaded.fastjson2.writer.FieldWriterListField Maven / Gradle / Ivy

The newest version!
package hydraql.shaded.fastjson2.writer;

import hydraql.shaded.fastjson2.JSONException;
import hydraql.shaded.fastjson2.JSONWriter;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.List;

import static hydraql.shaded.fastjson2.JSONWriter.Feature.*;

final class FieldWriterListField
        extends FieldWriterList {
    protected FieldWriterListField(
            String fieldName,
            Type itemType,
            int ordinal,
            long features,
            String format,
            String label,
            Type fieldType,
            Class fieldClass,
            Field field
    ) {
        super(fieldName, itemType, ordinal, features, format, label, fieldType, fieldClass, field, null);
    }

    @Override
    public Object getFieldValue(Object object) {
        try {
            return field.get(object);
        } catch (IllegalArgumentException | IllegalAccessException e) {
            throw new JSONException("field.get error, " + fieldName, e);
        }
    }

//    public boolean writeJSONBTable(JSONWriter jsonWriter, List objects) {
//        if (itemObjectWriter == null) {
//            itemObjectWriter = jsonWriter.getObjectWriter(itemType, itemClass);
//        }
//
//        return itemObjectWriter.writeJSONBTable(jsonWriter, objects, getFieldName(), getFieldType(), getItemClass(), getFeatures());
//    }

    @Override
    public boolean write(JSONWriter jsonWriter, T object) {
        List value = (List) getFieldValue(object);

        JSONWriter.Context context = jsonWriter.getContext();

        if (value == null) {
            long features = this.features | context.getFeatures();
            if ((features & (WriteNulls.mask | NullAsDefaultValue.mask | WriteNullListAsEmpty.mask)) != 0) {
                writeFieldName(jsonWriter);
                jsonWriter.writeArrayNull();
                return true;
            } else {
                return false;
            }
        }

        String refPath = jsonWriter.setPath(this, value);
        if (refPath != null) {
            writeFieldName(jsonWriter);
            jsonWriter.writeReference(refPath);
            jsonWriter.popPath(value);
            return true;
        }

        if (itemType == String.class) {
            writeListStr(jsonWriter, true, value);
        } else {
            writeList(jsonWriter, true, value);
        }
        jsonWriter.popPath(value);
        return true;
    }

    @Override
    public void writeValue(JSONWriter jsonWriter, T object) {
        List value = (List) getFieldValue(object);

        if (value == null) {
            jsonWriter.writeNull();
            return;
        }

        boolean refDetect = jsonWriter.isRefDetect();

        if (refDetect) {
            String refPath = jsonWriter.setPath(fieldName, value);
            if (refPath != null) {
                jsonWriter.writeReference(refPath);
                jsonWriter.popPath(value);
                return;
            }
        }

        writeList(jsonWriter, false, value);

        if (refDetect) {
            jsonWriter.popPath(value);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy