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

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

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

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

public final class ObjectWriterRootName
        extends ObjectWriterAdapter {
    final String rootName;
    public ObjectWriterRootName(
            Class objectClass,
            String typeKey,
            String typeName,
            String rootName,
            long features,
            List fieldWriters
    ) {
        super(objectClass, typeKey, typeName, features, fieldWriters);
        this.rootName = rootName;
    }

    public void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        jsonWriter.startObject();
        jsonWriter.writeName(rootName);
        super.writeJSONB(jsonWriter, object, fieldName, fieldType, features);
        jsonWriter.endObject();
    }

    public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        jsonWriter.startObject();
        jsonWriter.writeName(rootName);
        jsonWriter.writeColon();
        super.write(jsonWriter, object, fieldName, fieldType, features);
        jsonWriter.endObject();
    }

    public JSONObject toJSONObject(T object, long features) {
        return JSONObject.of(
                rootName,
                super.toJSONObject(object, features));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy