JsonAnyDecoder
This commit is contained in:
@@ -56,7 +56,7 @@ public class ArrayEncoder<W extends Writer, T> implements Encodeable<W, T[]> {
|
|||||||
throw new ConvertException(
|
throw new ConvertException(
|
||||||
"ArrayEncoder init componentEncoder error, componentType = " + this.componentType);
|
"ArrayEncoder init componentEncoder error, componentType = " + this.componentType);
|
||||||
}
|
}
|
||||||
this.anyEncoder = factory.getAnyEncoder();
|
this.anyEncoder = factory.loadEncoder(Object.class);
|
||||||
this.subTypeFinal = (this.componentType instanceof Class)
|
this.subTypeFinal = (this.componentType instanceof Class)
|
||||||
&& Modifier.isFinal(((Class) this.componentType).getModifiers());
|
&& Modifier.isFinal(((Class) this.componentType).getModifiers());
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -38,12 +38,12 @@ public class CollectionEncoder<W extends Writer, T> implements Encodeable<W, Col
|
|||||||
if (type instanceof ParameterizedType) {
|
if (type instanceof ParameterizedType) {
|
||||||
Type t = ((ParameterizedType) type).getActualTypeArguments()[0];
|
Type t = ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||||
if (t instanceof TypeVariable) {
|
if (t instanceof TypeVariable) {
|
||||||
this.componentEncoder = factory.getAnyEncoder();
|
this.componentEncoder = factory.loadEncoder(Object.class);
|
||||||
} else {
|
} else {
|
||||||
this.componentEncoder = factory.loadEncoder(t);
|
this.componentEncoder = factory.loadEncoder(t);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.componentEncoder = factory.getAnyEncoder();
|
this.componentEncoder = factory.loadEncoder(Object.class);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
inited = true;
|
inited = true;
|
||||||
|
|||||||
@@ -48,8 +48,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
*/
|
*/
|
||||||
protected int features;
|
protected int features;
|
||||||
|
|
||||||
private final Encodeable<W, ?> anyEncoder = new AnyEncoder(this);
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------------
|
||||||
private final ConcurrentHashMap<Class, Creator> creators = new ConcurrentHashMap();
|
private final ConcurrentHashMap<Class, Creator> creators = new ConcurrentHashMap();
|
||||||
|
|
||||||
@@ -1173,9 +1171,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
public final <E> Encodeable<W, E> getAnyEncoder() {
|
|
||||||
return (Encodeable<W, E>) anyEncoder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final <C extends BiFunction> void register(Class<C> clazz, C columnHandler) {
|
public final <C extends BiFunction> void register(Class<C> clazz, C columnHandler) {
|
||||||
fieldFuncs.put(Objects.requireNonNull(clazz), Objects.requireNonNull(columnHandler));
|
fieldFuncs.put(Objects.requireNonNull(clazz), Objects.requireNonNull(columnHandler));
|
||||||
@@ -1477,8 +1472,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
encoder = CompletionHandlerSimpledCoder.instance;
|
encoder = CompletionHandlerSimpledCoder.instance;
|
||||||
} else if (Optional.class == clazz) {
|
} else if (Optional.class == clazz) {
|
||||||
encoder = new OptionalCoder(this, type);
|
encoder = new OptionalCoder(this, type);
|
||||||
} else if (clazz == Object.class) {
|
|
||||||
return (Encodeable<W, E>) this.anyEncoder;
|
|
||||||
} else if (!clazz.getName().startsWith("java.")
|
} else if (!clazz.getName().startsWith("java.")
|
||||||
|| java.net.HttpCookie.class == clazz
|
|| java.net.HttpCookie.class == clazz
|
||||||
|| java.util.Map.Entry.class == clazz
|
|| java.util.Map.Entry.class == clazz
|
||||||
|
|||||||
@@ -46,8 +46,8 @@ public class MapEncoder<W extends Writer, K, V> implements Encodeable<W, Map<K,
|
|||||||
this.keyEncoder = factory.loadEncoder(pt[0]);
|
this.keyEncoder = factory.loadEncoder(pt[0]);
|
||||||
this.valueEncoder = factory.loadEncoder(pt[1]);
|
this.valueEncoder = factory.loadEncoder(pt[1]);
|
||||||
} else {
|
} else {
|
||||||
this.keyEncoder = factory.getAnyEncoder();
|
this.keyEncoder = factory.loadEncoder(Object.class);
|
||||||
this.valueEncoder = factory.getAnyEncoder();
|
this.valueEncoder = factory.loadEncoder(Object.class);
|
||||||
}
|
}
|
||||||
factory.ignoreMapColumnLock.lock();
|
factory.ignoreMapColumnLock.lock();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public class OptionalCoder<R extends Reader, W extends Writer, T> extends Simple
|
|||||||
factory.register(type, this);
|
factory.register(type, this);
|
||||||
this.decoder = factory.loadDecoder(this.componentType);
|
this.decoder = factory.loadDecoder(this.componentType);
|
||||||
if (this.componentType instanceof TypeVariable) {
|
if (this.componentType instanceof TypeVariable) {
|
||||||
this.encoder = factory.getAnyEncoder();
|
this.encoder = factory.loadEncoder(Object.class);
|
||||||
this.componentClass = Object.class;
|
this.componentClass = Object.class;
|
||||||
} else {
|
} else {
|
||||||
if (componentType instanceof ParameterizedType) {
|
if (componentType instanceof ParameterizedType) {
|
||||||
@@ -60,7 +60,7 @@ public class OptionalCoder<R extends Reader, W extends Writer, T> extends Simple
|
|||||||
this.componentType = Object.class;
|
this.componentType = Object.class;
|
||||||
this.componentClass = Object.class;
|
this.componentClass = Object.class;
|
||||||
this.decoder = factory.loadDecoder(this.componentType);
|
this.decoder = factory.loadDecoder(this.componentType);
|
||||||
this.encoder = factory.getAnyEncoder();
|
this.encoder = factory.loadEncoder(this.componentType);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
inited = true;
|
inited = true;
|
||||||
|
|||||||
@@ -16,12 +16,6 @@ import org.redkale.annotation.Nullable;
|
|||||||
*/
|
*/
|
||||||
public abstract class Reader {
|
public abstract class Reader {
|
||||||
|
|
||||||
public enum ValueType {
|
|
||||||
STRING,
|
|
||||||
ARRAY,
|
|
||||||
MAP;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 集合对象为null
|
* 集合对象为null
|
||||||
*
|
*
|
||||||
@@ -67,13 +61,6 @@ public abstract class Reader {
|
|||||||
*/
|
*/
|
||||||
public abstract void readColon();
|
public abstract void readColon();
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取下个值的类型
|
|
||||||
*
|
|
||||||
* @return ValueType
|
|
||||||
*/
|
|
||||||
public abstract ValueType readType();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取对象的类名, 返回 null 表示对象为null, 返回空字符串表示当前class与返回的class一致,返回非空字符串表示class是当前class的子类。
|
* 读取对象的类名, 返回 null 表示对象为null, 返回空字符串表示当前class与返回的class一致,返回非空字符串表示class是当前class的子类。
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -38,12 +38,12 @@ public class StreamEncoder<W extends Writer, T> implements Encodeable<W, Stream<
|
|||||||
if (type instanceof ParameterizedType) {
|
if (type instanceof ParameterizedType) {
|
||||||
Type t = ((ParameterizedType) type).getActualTypeArguments()[0];
|
Type t = ((ParameterizedType) type).getActualTypeArguments()[0];
|
||||||
if (t instanceof TypeVariable) {
|
if (t instanceof TypeVariable) {
|
||||||
this.componentEncoder = factory.getAnyEncoder();
|
this.componentEncoder = factory.loadEncoder(Object.class);
|
||||||
} else {
|
} else {
|
||||||
this.componentEncoder = factory.loadEncoder(t);
|
this.componentEncoder = factory.loadEncoder(t);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
this.componentEncoder = factory.getAnyEncoder();
|
this.componentEncoder = factory.loadEncoder(Object.class);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
inited = true;
|
inited = true;
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
/*
|
/*
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
* Copyright (c) 2016-2116 Redkale
|
||||||
* To change this template file, choose Tools | Templates
|
* All rights reserved.
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
*/
|
||||||
package org.redkale.convert;
|
package org.redkale.convert.json;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
import org.redkale.convert.Reader.ValueType;
|
import java.util.Collection;
|
||||||
import static org.redkale.convert.Reader.ValueType.MAP;
|
import java.util.LinkedHashMap;
|
||||||
import org.redkale.util.*;
|
import java.util.Map;
|
||||||
|
import org.redkale.convert.CollectionDecoder;
|
||||||
|
import org.redkale.convert.ConvertFactory;
|
||||||
|
import org.redkale.convert.Decodeable;
|
||||||
|
import org.redkale.convert.MapDecoder;
|
||||||
|
import org.redkale.util.Creator;
|
||||||
|
import org.redkale.util.TypeToken;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对不明类型的对象进行反序列化。 <br>
|
* 对不明类型的对象进行反序列化。 <br>
|
||||||
@@ -17,9 +22,9 @@ import org.redkale.util.*;
|
|||||||
* 详情见: https://redkale.org
|
* 详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @param <R> Reader
|
* @param <T> 泛型
|
||||||
*/
|
*/
|
||||||
public class AnyDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
public class JsonAnyDecoder<T> implements Decodeable<JsonReader, T> {
|
||||||
|
|
||||||
private static final Type collectionObjectType = new TypeToken<Collection<Object>>() {}.getType();
|
private static final Type collectionObjectType = new TypeToken<Collection<Object>>() {}.getType();
|
||||||
|
|
||||||
@@ -29,7 +34,7 @@ public class AnyDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
|||||||
|
|
||||||
private static final Creator<LinkedHashMap> mapCreator = Creator.create(LinkedHashMap.class);
|
private static final Creator<LinkedHashMap> mapCreator = Creator.create(LinkedHashMap.class);
|
||||||
|
|
||||||
protected final Decodeable<Reader, ? extends CharSequence> stringDecoder;
|
protected final Decodeable<JsonReader, ? extends CharSequence> stringDecoder;
|
||||||
|
|
||||||
protected final CollectionDecoder collectionDecoder;
|
protected final CollectionDecoder collectionDecoder;
|
||||||
|
|
||||||
@@ -40,24 +45,24 @@ public class AnyDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
|||||||
*
|
*
|
||||||
* @param factory ConvertFactory
|
* @param factory ConvertFactory
|
||||||
*/
|
*/
|
||||||
public AnyDecoder(final ConvertFactory factory) {
|
public JsonAnyDecoder(final ConvertFactory factory) {
|
||||||
this(mapCreator, mapObjectType, collectionCreator, collectionObjectType, factory.loadDecoder(String.class));
|
this(mapCreator, mapObjectType, collectionCreator, collectionObjectType, factory.loadDecoder(String.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected AnyDecoder(
|
protected JsonAnyDecoder(
|
||||||
Creator<? extends Map> mapCreator,
|
Creator<? extends Map> mapCreator,
|
||||||
Type mapObjectType,
|
Type mapObjectType,
|
||||||
Creator<? extends Collection> listCreator,
|
Creator<? extends Collection> listCreator,
|
||||||
Type listObjectType,
|
Type listObjectType,
|
||||||
Decodeable<Reader, String> keyDecoder) {
|
Decodeable<JsonReader, String> keyDecoder) {
|
||||||
this.stringDecoder = keyDecoder;
|
this.stringDecoder = keyDecoder;
|
||||||
this.collectionDecoder = new CollectionDecoder(listObjectType, Object.class, listCreator, this);
|
this.collectionDecoder = new CollectionDecoder(listObjectType, Object.class, listCreator, this);
|
||||||
this.mapDecoder = new MapDecoder(mapObjectType, String.class, Object.class, mapCreator, keyDecoder, this);
|
this.mapDecoder = new MapDecoder(mapObjectType, String.class, Object.class, mapCreator, keyDecoder, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public T convertFrom(Reader in) {
|
public T convertFrom(JsonReader in) {
|
||||||
ValueType vt = in.readType();
|
JsonReader.ValueType vt = in.readType();
|
||||||
if (vt == null) {
|
if (vt == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -70,12 +75,12 @@ public class AnyDecoder<R extends Reader, T> implements Decodeable<R, T> {
|
|||||||
return (T) stringFrom(in);
|
return (T) stringFrom(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected T stringFrom(Reader in) {
|
protected T stringFrom(JsonReader in) {
|
||||||
return (T) this.stringDecoder.convertFrom(in);
|
return (T) this.stringDecoder.convertFrom(in);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Type getType() {
|
public Type getType() {
|
||||||
return void.class;
|
return Object.class;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
/*
|
/*
|
||||||
* To change this license header, choose License Headers in Project Properties.
|
* Copyright (c) 2016-2116 Redkale
|
||||||
* To change this template file, choose Tools | Templates
|
* All rights reserved.
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
*/
|
||||||
package org.redkale.convert;
|
package org.redkale.convert.json;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import org.redkale.convert.Encodeable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对不明类型的对象进行序列化; PROTOBUF序列化时将对象的类名写入Writer,JSON则不写入。
|
* 对不明类型的对象进行序列化; PROTOBUF序列化时将对象的类名写入Writer,JSON则不写入。
|
||||||
@@ -13,20 +13,19 @@ import java.lang.reflect.Type;
|
|||||||
* <p>详情见: https://redkale.org
|
* <p>详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @param <W> Writer
|
|
||||||
* @param <T> 序列化的泛型类型
|
* @param <T> 序列化的泛型类型
|
||||||
*/
|
*/
|
||||||
public final class AnyEncoder<W extends Writer, T> implements Encodeable<W, T> {
|
public final class JsonAnyEncoder<T> implements Encodeable<JsonWriter, T> {
|
||||||
|
|
||||||
final ConvertFactory factory;
|
final JsonFactory factory;
|
||||||
|
|
||||||
AnyEncoder(ConvertFactory factory) {
|
JsonAnyEncoder(JsonFactory factory) {
|
||||||
this.factory = factory;
|
this.factory = factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void convertTo(final W out, final T value) {
|
public void convertTo(final JsonWriter out, final T value) {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
out.writeNull();
|
out.writeNull();
|
||||||
} else {
|
} else {
|
||||||
@@ -38,6 +38,8 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
|
|
||||||
private final ThreadLocal<JsonReader> readerPool = Utility.withInitialThreadLocal(JsonReader::new);
|
private final ThreadLocal<JsonReader> readerPool = Utility.withInitialThreadLocal(JsonReader::new);
|
||||||
|
|
||||||
|
private final JsonAnyDecoder anyDecoder;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Encodeable lastEncodeable;
|
private Encodeable lastEncodeable;
|
||||||
|
|
||||||
@@ -46,6 +48,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
|
|
||||||
protected JsonConvert(JsonFactory factory, int features) {
|
protected JsonConvert(JsonFactory factory, int features) {
|
||||||
super(factory, features);
|
super(factory, features);
|
||||||
|
this.anyDecoder = new JsonAnyDecoder(factory);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -269,7 +272,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
}
|
}
|
||||||
// final JsonReader in = readerPool.get();
|
// final JsonReader in = readerPool.get();
|
||||||
// in.setText(text, offset, length);
|
// in.setText(text, offset, length);
|
||||||
Object rs = new AnyDecoder(factory).convertFrom(new JsonReader(text, offset, length));
|
Object rs = anyDecoder.convertFrom(new JsonReader(text, offset, length));
|
||||||
// readerPool.accept(in);
|
// readerPool.accept(in);
|
||||||
return (V) rs;
|
return (V) rs;
|
||||||
}
|
}
|
||||||
@@ -279,7 +282,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
if (in == null) {
|
if (in == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (V) new AnyDecoder(factory).convertFrom(new JsonStreamReader(in));
|
return (V) anyDecoder.convertFrom(new JsonStreamReader(in));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
// 返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
||||||
@@ -287,7 +290,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
if (buffers == null || buffers.length == 0) {
|
if (buffers == null || buffers.length == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (V) new AnyDecoder(factory).convertFrom(new JsonByteBufferReader(buffers));
|
return (V) anyDecoder.convertFrom(new JsonByteBufferReader(buffers));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
// 返回非null的值是由String、ArrayList、HashMap任意组合的对象
|
||||||
@@ -295,7 +298,7 @@ public class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
if (reader == null) {
|
if (reader == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return (V) new AnyDecoder(factory).convertFrom(reader);
|
return (V) anyDecoder.convertFrom(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
// json数据的数组长度必须和types个数相同
|
// json数据的数组长度必须和types个数相同
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package org.redkale.convert.json;
|
|||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.redkale.convert.AnyDecoder;
|
|
||||||
import org.redkale.convert.ext.StringSimpledCoder;
|
import org.redkale.convert.ext.StringSimpledCoder;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
@@ -17,7 +16,7 @@ import org.redkale.util.*;
|
|||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @since 2.8.0
|
* @since 2.8.0
|
||||||
*/
|
*/
|
||||||
class JsonElementDecoder extends AnyDecoder<JsonReader, JsonElement> {
|
class JsonElementDecoder extends JsonAnyDecoder<JsonElement> {
|
||||||
|
|
||||||
private static final Type arrayType = new TypeToken<Collection<JsonElement>>() {}.getType();
|
private static final Type arrayType = new TypeToken<Collection<JsonElement>>() {}.getType();
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
|
|||||||
@Override
|
@Override
|
||||||
protected void initSimpleCoderInRoot() {
|
protected void initSimpleCoderInRoot() {
|
||||||
super.initSimpleCoderInRoot();
|
super.initSimpleCoderInRoot();
|
||||||
|
this.register(Object.class, new JsonAnyDecoder(this));
|
||||||
|
this.register(Object.class, new JsonAnyEncoder(this));
|
||||||
this.register(InetAddress.class, JsonCoders.InetAddressJsonSimpledCoder.instance);
|
this.register(InetAddress.class, JsonCoders.InetAddressJsonSimpledCoder.instance);
|
||||||
this.register(InetSocketAddress.class, JsonCoders.InetSocketAddressJsonSimpledCoder.instance);
|
this.register(InetSocketAddress.class, JsonCoders.InetSocketAddressJsonSimpledCoder.instance);
|
||||||
this.register(Uint128.class, JsonCoders.Uint128JsonSimpledCoder.instance);
|
this.register(Uint128.class, JsonCoders.Uint128JsonSimpledCoder.instance);
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
import static org.redkale.convert.Reader.*;
|
import static org.redkale.convert.Reader.*;
|
||||||
import org.redkale.convert.Reader.ValueType;
|
|
||||||
import org.redkale.util.ByteTreeNode;
|
import org.redkale.util.ByteTreeNode;
|
||||||
import org.redkale.util.Utility;
|
import org.redkale.util.Utility;
|
||||||
|
|
||||||
@@ -22,6 +21,12 @@ import org.redkale.util.Utility;
|
|||||||
*/
|
*/
|
||||||
public class JsonReader extends Reader {
|
public class JsonReader extends Reader {
|
||||||
|
|
||||||
|
public enum ValueType {
|
||||||
|
STRING,
|
||||||
|
ARRAY,
|
||||||
|
MAP;
|
||||||
|
}
|
||||||
|
|
||||||
protected int position = -1;
|
protected int position = -1;
|
||||||
|
|
||||||
private char[] text;
|
private char[] text;
|
||||||
@@ -250,7 +255,6 @@ public class JsonReader extends Reader {
|
|||||||
return ch == '[';
|
return ch == '[';
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public final ValueType readType() {
|
public final ValueType readType() {
|
||||||
char ch = nextGoodChar(true);
|
char ch = nextGoodChar(true);
|
||||||
if (ch == '{') {
|
if (ch == '{') {
|
||||||
|
|||||||
31
src/main/java/org/redkale/convert/pb/ProtobufAnyDecoder.java
Normal file
31
src/main/java/org/redkale/convert/pb/ProtobufAnyDecoder.java
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016-2116 Redkale
|
||||||
|
* All rights reserved.
|
||||||
|
*/
|
||||||
|
package org.redkale.convert.pb;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import org.redkale.convert.Decodeable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
*/
|
||||||
|
public class ProtobufAnyDecoder<T> implements Decodeable<ProtobufReader, T> {
|
||||||
|
|
||||||
|
final ProtobufFactory factory;
|
||||||
|
|
||||||
|
ProtobufAnyDecoder(ProtobufFactory factory) {
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public T convertFrom(ProtobufReader in) {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return Object.class;
|
||||||
|
}
|
||||||
|
}
|
||||||
51
src/main/java/org/redkale/convert/pb/ProtobufAnyEncoder.java
Normal file
51
src/main/java/org/redkale/convert/pb/ProtobufAnyEncoder.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016-2116 Redkale
|
||||||
|
* All rights reserved.
|
||||||
|
*/
|
||||||
|
package org.redkale.convert.pb;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import org.redkale.convert.Encodeable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对不明类型的对象进行序列化; PROTOBUF序列化时将对象的类名写入Writer,JSON则不写入。
|
||||||
|
*
|
||||||
|
* <p>详情见: https://redkale.org
|
||||||
|
*
|
||||||
|
* @author zhangjx
|
||||||
|
* @param <T> 序列化的泛型类型
|
||||||
|
*/
|
||||||
|
public final class ProtobufAnyEncoder<T> implements Encodeable<ProtobufWriter, T> {
|
||||||
|
|
||||||
|
final ProtobufFactory factory;
|
||||||
|
|
||||||
|
ProtobufAnyEncoder(ProtobufFactory factory) {
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public void convertTo(final ProtobufWriter out, final T value) {
|
||||||
|
if (value == null) {
|
||||||
|
out.writeNull();
|
||||||
|
} else {
|
||||||
|
Class clazz = value.getClass();
|
||||||
|
if (clazz == Object.class) {
|
||||||
|
out.writeObjectB(value);
|
||||||
|
out.writeObjectE(value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
factory.loadEncoder(clazz).convertTo(out, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type getType() {
|
||||||
|
return Object.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean specifyable() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -77,6 +77,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
|||||||
@Override
|
@Override
|
||||||
protected void initSimpleCoderInRoot() {
|
protected void initSimpleCoderInRoot() {
|
||||||
super.initSimpleCoderInRoot();
|
super.initSimpleCoderInRoot();
|
||||||
|
this.register(Object.class, new ProtobufAnyDecoder(this));
|
||||||
|
this.register(Object.class, new ProtobufAnyEncoder(this));
|
||||||
this.register(StringWrapper.class, ProtobufCoders.ProtobufStringWrapperSimpledCoder.instance);
|
this.register(StringWrapper.class, ProtobufCoders.ProtobufStringWrapperSimpledCoder.instance);
|
||||||
this.register(CharSequence.class, ProtobufCoders.ProtobufCharSequenceSimpledCoder.instance);
|
this.register(CharSequence.class, ProtobufCoders.ProtobufCharSequenceSimpledCoder.instance);
|
||||||
this.register(StringBuilder.class, ProtobufCoders.ProtobufStringBuilderSimpledCoder.instance);
|
this.register(StringBuilder.class, ProtobufCoders.ProtobufStringBuilderSimpledCoder.instance);
|
||||||
|
|||||||
@@ -438,11 +438,6 @@ public class ProtobufReader extends Reader {
|
|||||||
this.cacheTag = tag;
|
this.cacheTag = tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public final ValueType readType() {
|
|
||||||
throw new UnsupportedOperationException("Not supported yet.");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return (this.position + 1) < this.limit;
|
return (this.position + 1) < this.limit;
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ public class HttpResult<T> {
|
|||||||
if (value instanceof byte[]) {
|
if (value instanceof byte[]) {
|
||||||
out.writeString(new String((byte[]) value, StandardCharsets.UTF_8));
|
out.writeString(new String((byte[]) value, StandardCharsets.UTF_8));
|
||||||
} else {
|
} else {
|
||||||
factory.getAnyEncoder().convertTo(out, value);
|
factory.loadEncoder(Object.class).convertTo(out, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user