
com.caucho.v5.json.ser.JsonFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of baratine Show documentation
Show all versions of baratine Show documentation
A reactive Java web server.
/*
* Copyright (c) 1998-2015 Caucho Technology -- all rights reserved
*
* This file is part of Baratine(TM)
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Baratine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Baratine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
* of NON-INFRINGEMENT. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Baratine; if not, write to the
*
* Free Software Foundation, Inc.
* 59 Temple Place, Suite 330
* Boston, MA 02111-1307 USA
*
* @author Scott Ferguson
*/
package com.caucho.v5.json.ser;
import java.io.StringReader;
import java.io.Writer;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.Deque;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import com.caucho.v5.inject.type.TypeRef;
import com.caucho.v5.json.io.JsonReader;
import com.caucho.v5.json.io.JsonWriter;
import com.caucho.v5.json.value.JsonValue;
import com.caucho.v5.reflect.ClassImpl;
public class JsonFactory
{
private static final Logger log = Logger.getLogger(JsonFactory.class.getName());
private static final HashMap,SerializerJson>> _staticSerMap
= new HashMap<>();
private static final HashMap,SerializerJson>> _staticSerInterfaceMap
= new HashMap<>();
private static final HashMap,SerializerJson> _staticDeserMap
= new HashMap<>();
private static final HashMap,CollectionFunction> _factoryMap
= new HashMap<>();
private final ClassValue> _serMap
= new SerializerClassValue();
private final ConcurrentHashMap> _serTypeMap
= new ConcurrentHashMap<>();
private final ConcurrentHashMap _deserMap
= new ConcurrentHashMap<>();
private final HashMap,SerializerJson>> _serInterfaceMap
= new HashMap<>();
//private final TypeFactoryReflect _typeFactory = new TypeFactoryReflect();
public JsonFactory()
{
}
/*
public TypeFactoryReflect getTypeFactory()
{
return _typeFactory;
}
*/
//
// serializers
//
/**
* The serializer for the given class.
*/
public final SerializerJson serializer(Class cl)
{
return (SerializerJson) _serMap.get(cl);
}
/**
* The serializer for the given type.
*/
public final SerializerJson serializer(Type type)
{
SerializerJson ser = (SerializerJson) _serTypeMap.get(type);
if (ser == null) {
TypeRef typeRef = TypeRef.of(type);
Class rawClass = (Class) typeRef.rawClass();
ser = serializer(rawClass).withType(typeRef, this);
_serTypeMap.putIfAbsent(type, ser);
ser = (SerializerJson) _serTypeMap.get(type);
}
return ser;
}
public void addInterfaceSerializer(Class cl, SerializerJson ser)
{
_serInterfaceMap.put(cl, ser);
}
/**
* addSerializer adds a custom serializer, overriding the defaults.
*/
public void addSerializer(Class cl,
SerializerJson ser)
{
//_serMap.put(cl, ser);
}
/*
public void addDeserializer(Class> cl,
SerializerJson deser)
{
_deserMap.put(cl, deser);
}
*/
protected SerializerJson> createSerializer(Class> cl)
{
SerializerJson> ser = _staticSerMap.get(cl);
if (ser != null) {
return ser;
}
TypeRef typeRef = TypeRef.of(cl);
if (cl.isArray()) {
Class> eltType = cl.getComponentType();
return new ObjectArraySerializerJson(eltType, serializer(eltType));
}
Method methodReplaceObject = findWriteReplace(cl);
if (methodReplaceObject != null) {
return new JavaSerializerWriteReplace(methodReplaceObject);
}
CollectionFunction collFun = _factoryMap.get(cl);
if (collFun != null) {
return collFun.apply(typeRef, this);
}
if (Map.class.isAssignableFrom(cl)) {
return new MapJavaSerializer(typeRef, this, cl);
}
else if (Collection.class.isAssignableFrom(cl)) {
return new ListJavaSerializer(typeRef, this, cl);
}
/*
if (Collection.class.isAssignableFrom(cl)) {
return new CollectionSerializerJson(typeRef, this);
}
if (Map.class.isAssignableFrom(cl)) {
return new MapSerializerJson(typeRef, this, HashMap::new);
}
*/
ser = findInterfaceSerializer(cl);
if (ser != null) {
return ser;
}
if (AtomicInteger.class.isAssignableFrom(cl)) {
return AtomicIntegerSerializer.SER;
}
if (Enum.class.isAssignableFrom(cl)) {
return EnumSerializer.SER;
}
try {
Method valueOf = cl.getMethod("valueOf", String.class);
if (Modifier.isStatic(valueOf.getModifiers())) {
//&& cl.equals(valueOf.getReturnType())) {
return new StringValueOfSerializer<>(cl, valueOf);
}
} catch (NoSuchMethodException e) {
log.log(Level.ALL, e.toString(), e);
}
return new JavaSerializerJson(typeRef, this);
}
protected SerializerJson> findInterfaceSerializer(Class> cl)
{
if (cl == null) {
return null;
}
SerializerJson> ser;
if (cl.isInterface()) {
ser = _serInterfaceMap.get(cl);
if (ser != null) {
return ser;
}
ser = _staticSerInterfaceMap.get(cl);
if (ser != null) {
return ser;
}
}
for (Class> iface : cl.getInterfaces()) {
ser = findInterfaceSerializer(iface);
if (ser != null) {
return ser;
}
}
if (cl.isInterface()) {
return findInterfaceSerializer(cl.getSuperclass());
}
else {
return null;
}
}
//
// deserializers
//
private SerializerJson deserializerOld(Type type)
{
Objects.requireNonNull(type);
if (type instanceof ClassImpl) {
type = ((ClassImpl) type).getTypeClass();
}
SerializerJson deser = _deserMap.get(type);
if (deser == null) {
deser = createDeserializerOld(type);
_deserMap.putIfAbsent(type, deser);
}
return deser;
}
protected SerializerJson createDeserializerOld(Type type)
{
SerializerJson deser = _staticDeserMap.get(type);
if (deser != null) {
return deser;
}
TypeRef typeImpl = TypeRef.of(type);
Class> cl = typeImpl.rawClass();
if (Enum.class.isAssignableFrom(cl)) {
return new EnumDeserializer(cl);
}
else if (Object.class.equals(cl)) {
return _staticDeserMap.get(Object.class);
}
CollectionFunction collFun = _factoryMap.get(cl);
if (collFun != null) {
return collFun.apply(typeImpl, this);
}
if (Map.class.isAssignableFrom(cl)) {
return new MapJavaSerializer(typeImpl, this, cl);
}
else if (Collection.class.isAssignableFrom(cl)) {
return new ListJavaSerializer(typeImpl, this, cl);
}
/*
if (cl.isInterface() && Map.class.isAssignableFrom(cl)) {
TypeRef mapType = typeImpl.to(Map.class);
TypeRef keyType = mapType.param(0);
TypeRef valueType = mapType.param(1);
Supplier
© 2015 - 2025 Weber Informatics LLC | Privacy Policy