com.alibaba.fastjson2.support.AwtRederModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastjson2-extension Show documentation
Show all versions of fastjson2-extension Show documentation
Fastjson is a JSON processor (JSON parser + JSON generator) written in Java
package com.alibaba.fastjson2.support;
import com.alibaba.fastjson2.modules.ObjectReaderModule;
import com.alibaba.fastjson2.reader.ObjectReader;
import com.alibaba.fastjson2.reader.ObjectReaderProvider;
import com.alibaba.fastjson2.reader.ObjectReaders;
import com.alibaba.fastjson2.util.Fnv;
import java.awt.*;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Function;
import static com.alibaba.fastjson2.reader.ObjectReaders.fieldReader;
public class AwtRederModule
implements ObjectReaderModule {
// Android not support
static final long HASH_X = Fnv.hashCode64("x");
static final long HASH_Y = Fnv.hashCode64("y");
static final long HASH_NAME = Fnv.hashCode64("name");
static final long HASH_SIZE = Fnv.hashCode64("size");
static final long HASH_STYLE = Fnv.hashCode64("style");
public static AwtRederModule INSTANCE = new AwtRederModule();
@Override
public ObjectReader getObjectReader(ObjectReaderProvider provider, Type type) {
if (type == Color.class) {
return ObjectReaders.objectReader(
new ColorCreator(),
fieldReader("rgb", int.class),
fieldReader("r", int.class),
fieldReader("g", int.class),
fieldReader("b", int.class)
);
}
if (type == Point.class) {
return ObjectReaders.objectReader(
(values) -> new Point(
(Integer) values.get(HASH_X),
(Integer) values.get(HASH_Y)
),
fieldReader("x", int.class),
fieldReader("y", int.class)
);
}
if (type == Font.class) {
return ObjectReaders.objectReader(
(values) -> new Font(
(String) values.get(HASH_NAME),
(Integer) values.get(HASH_STYLE),
(Integer) values.get(HASH_SIZE)
),
fieldReader("name", String.class),
fieldReader("style", int.class),
fieldReader("size", int.class)
);
}
return null;
}
static class ColorCreator
implements Function