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

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

import java.lang.reflect.Type;

import static com.alibaba.fastjson2.JSONWriter.Feature.*;

final class ObjectWriterImplInt64
        extends ObjectWriterPrimitiveImpl {
    static final ObjectWriterImplInt64 INSTANCE = new ObjectWriterImplInt64(null);

    final Class defineClass;

    public ObjectWriterImplInt64(Class defineClass) {
        this.defineClass = defineClass;
    }

    @Override
    public void writeJSONB(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        if (object == null) {
            jsonWriter.writeNumberNull();
            return;
        }

        long longValue = (Long) object;

        if ((features & WriteNonStringValueAsString.mask) != 0) {
            jsonWriter.writeString(longValue);
            return;
        }
        jsonWriter.writeInt64(longValue);
    }

    @Override
    public void write(JSONWriter jsonWriter, Object object, Object fieldName, Type fieldType, long features) {
        if (object == null) {
            jsonWriter.writeNumberNull();
            return;
        }

        long value = ((Number) object).longValue();

        if ((features & WriteNonStringValueAsString.mask) != 0) {
            jsonWriter.writeString(value);
            return;
        }

        jsonWriter.writeInt64(value);

        if (value >= Integer.MIN_VALUE
                && value <= Integer.MAX_VALUE
                && (features & WriteClassName.mask) != 0
        ) {
            long contextFeatures = jsonWriter.getFeatures();
            if ((contextFeatures & WriteClassName.mask) == 0) {
                boolean writeAsString = (contextFeatures & (WriteNonStringValueAsString.mask | WriteLongAsString.mask)) != 0;
                if (!writeAsString) {
                    jsonWriter.writeRaw('L');
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy