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

shade.com.alibaba.fastjson2.writer.ObjectWriterMisc Maven / Gradle / Ivy

There is a newer version: 1.3.7
Show newest version
package com.alibaba.fastjson2.writer;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONWriter;

import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;

final class ObjectWriterMisc
        implements ObjectWriter {
    static final ObjectWriterMisc INSTANCE = new ObjectWriterMisc();

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

        Class objectClass = object.getClass();
        String objectClassName = objectClass.getName();

        String str;
        switch (objectClassName) {
            case "net.sf.json.JSONNull":
                jsonWriter.writeNull();
                return;
            case "java.net.Inet4Address":
            case "java.net.Inet6Address":
                str = ((java.net.InetAddress) object).getHostName();
                break;
            case "java.text.SimpleDateFormat":
                str = ((java.text.SimpleDateFormat) object).toPattern();
                break;
            case "java.util.regex.Pattern":
                str = ((java.util.regex.Pattern) object).pattern();
                break;
            case "java.net.InetSocketAddress": {
                java.net.InetSocketAddress address = (java.net.InetSocketAddress) object;

                jsonWriter.startObject();

                jsonWriter.writeName("address");
                jsonWriter.writeColon();
                jsonWriter.writeAny(address.getAddress());

                jsonWriter.writeName("port");
                jsonWriter.writeColon();
                jsonWriter.writeInt32(address.getPort());

                jsonWriter.endObject();
                return;
            }
            case "com.fasterxml.jackson.databind.node.ArrayNode":
                str = object.toString();
                if (jsonWriter.isUTF8()) {
                    jsonWriter.writeRaw(str.getBytes(StandardCharsets.UTF_8));
                } else {
                    jsonWriter.writeRaw(str);
                }
                return;
            default:
                throw new JSONException("not support class : " + objectClassName);
        }

        jsonWriter.writeString(str);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy