This commit is contained in:
@@ -3,23 +3,19 @@
|
|||||||
* To change this template file, choose Tools | Templates
|
* To change this template file, choose Tools | Templates
|
||||||
* and open the template in the editor.
|
* and open the template in the editor.
|
||||||
*/
|
*/
|
||||||
package org.redkale.convert.json;
|
package org.redkale.convert;
|
||||||
|
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.Type;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.redkale.convert.*;
|
|
||||||
import org.redkale.convert.json.JsonReader.ValueType;
|
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
import org.redkale.convert.Reader.ValueType;
|
||||||
|
import static org.redkale.convert.Reader.ValueType.MAP;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对不明类型的对象进行JSON反序列化。
|
|
||||||
* <p>
|
|
||||||
* 详情见: https://redkale.org
|
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
public class AnyDecoder implements Decodeable<Reader, Object> {
|
||||||
public final class JsonAnyDecoder implements Decodeable<JsonReader, Object> {
|
|
||||||
|
|
||||||
private static final Type collectionObjectType = new TypeToken<Collection<Object>>() {
|
private static final Type collectionObjectType = new TypeToken<Collection<Object>>() {
|
||||||
}.getType();
|
}.getType();
|
||||||
@@ -31,26 +27,26 @@ public final class JsonAnyDecoder implements Decodeable<JsonReader, Object> {
|
|||||||
|
|
||||||
private static final Creator<HashMap> mapCreator = Creator.create(HashMap.class);
|
private static final Creator<HashMap> mapCreator = Creator.create(HashMap.class);
|
||||||
|
|
||||||
protected final Decodeable<JsonReader, String> stringDecoder;
|
protected final Decodeable<Reader, String> stringDecoder;
|
||||||
|
|
||||||
protected final CollectionDecoder collectionDecoder;
|
protected final CollectionDecoder collectionDecoder;
|
||||||
|
|
||||||
protected final MapDecoder mapDecoder;
|
protected final MapDecoder mapDecoder;
|
||||||
|
|
||||||
public JsonAnyDecoder(final ConvertFactory factory) {
|
public AnyDecoder(final ConvertFactory factory) {
|
||||||
this.stringDecoder = factory.loadDecoder(String.class);
|
this.stringDecoder = factory.loadDecoder(String.class);
|
||||||
this.collectionDecoder = new CollectionDecoder(factory, collectionObjectType, Object.class, collectionCreator, this);
|
this.collectionDecoder = new CollectionDecoder(factory, collectionObjectType, Object.class, collectionCreator, this);
|
||||||
this.mapDecoder = new MapDecoder(factory, mapObjectType, String.class, Object.class, mapCreator, stringDecoder, this);
|
this.mapDecoder = new MapDecoder(factory, mapObjectType, String.class, Object.class, mapCreator, stringDecoder, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object convertFrom(JsonReader in) {
|
public Object convertFrom(Reader in) {
|
||||||
ValueType vt = in.readType();
|
ValueType vt = in.readType();
|
||||||
if (vt == null) return null;
|
if (vt == null) return null;
|
||||||
switch (vt) {
|
switch (vt) {
|
||||||
case COLLECTION:
|
case ARRAY:
|
||||||
return this.collectionDecoder.convertFrom(in);
|
return this.collectionDecoder.convertFrom(in);
|
||||||
case JSONOBJECT:
|
case MAP:
|
||||||
return this.mapDecoder.convertFrom(in);
|
return this.mapDecoder.convertFrom(in);
|
||||||
}
|
}
|
||||||
return this.stringDecoder.convertFrom(in);
|
return this.stringDecoder.convertFrom(in);
|
||||||
@@ -15,6 +15,10 @@ package org.redkale.convert;
|
|||||||
*/
|
*/
|
||||||
public abstract class Reader {
|
public abstract class Reader {
|
||||||
|
|
||||||
|
public static enum ValueType {
|
||||||
|
STRING, ARRAY, MAP;
|
||||||
|
}
|
||||||
|
|
||||||
//当前对象字段名的游标
|
//当前对象字段名的游标
|
||||||
protected int fieldIndex;
|
protected int fieldIndex;
|
||||||
|
|
||||||
@@ -73,6 +77,13 @@ public abstract class Reader {
|
|||||||
*/
|
*/
|
||||||
public abstract void readBlank();
|
public abstract void readBlank();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 读取下个值的类型
|
||||||
|
*
|
||||||
|
* @return ValueType
|
||||||
|
*/
|
||||||
|
public abstract ValueType readType();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 读取对象的类名, 返回 null 表示对象为null, 返回空字符串表示当前class与返回的class一致,返回非空字符串表示class是当前class的子类。
|
* 读取对象的类名, 返回 null 表示对象为null, 返回空字符串表示当前class与返回的class一致,返回非空字符串表示class是当前class的子类。
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -346,4 +346,9 @@ public class BsonReader extends Reader {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ValueType readType() {
|
||||||
|
throw new UnsupportedOperationException("Not supported yet.");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -128,43 +128,43 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
return rs;
|
return rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertFrom(final String text) {
|
public <V> V convertFrom(final String text) {
|
||||||
if (text == null) return null;
|
if (text == null) return null;
|
||||||
return convertFrom(Utility.charArray(text));
|
return (V) convertFrom(Utility.charArray(text));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertFrom(final char[] text) {
|
public <V> V convertFrom(final char[] text) {
|
||||||
if (text == null) return null;
|
if (text == null) return null;
|
||||||
return convertFrom(text, 0, text.length);
|
return (V) convertFrom(text, 0, text.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertFrom(final char[] text, final int start, final int len) {
|
public <V> V convertFrom(final char[] text, final int start, final int len) {
|
||||||
if (text == null) return null;
|
if (text == null) return null;
|
||||||
final JsonReader in = readerPool.get();
|
final JsonReader in = readerPool.get();
|
||||||
in.setText(text, start, len);
|
in.setText(text, start, len);
|
||||||
Object rs = new JsonAnyDecoder(factory).convertFrom(in);
|
Object rs = new AnyDecoder(factory).convertFrom(in);
|
||||||
readerPool.accept(in);
|
readerPool.accept(in);
|
||||||
return rs;
|
return (V) rs;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertFrom(final InputStream in) {
|
public <V> V convertFrom(final InputStream in) {
|
||||||
if (in == null) return null;
|
if (in == null) return null;
|
||||||
return new JsonAnyDecoder(factory).convertFrom(new JsonStreamReader(in));
|
return (V) new AnyDecoder(factory).convertFrom(new JsonStreamReader(in));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertFrom(final ByteBuffer... buffers) {
|
public <V> V convertFrom(final ByteBuffer... buffers) {
|
||||||
if (buffers == null || buffers.length == 0) return null;
|
if (buffers == null || buffers.length == 0) return null;
|
||||||
return new JsonAnyDecoder(factory).convertFrom(new JsonByteBufferReader((ConvertMask) null, buffers));
|
return (V) new AnyDecoder(factory).convertFrom(new JsonByteBufferReader((ConvertMask) null, buffers));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertFrom(final ConvertMask mask, final ByteBuffer... buffers) {
|
public <V> V convertFrom(final ConvertMask mask, final ByteBuffer... buffers) {
|
||||||
if (buffers == null || buffers.length == 0) return null;
|
if (buffers == null || buffers.length == 0) return null;
|
||||||
return new JsonAnyDecoder(factory).convertFrom(new JsonByteBufferReader(mask, buffers));
|
return (V) new AnyDecoder(factory).convertFrom(new JsonByteBufferReader(mask, buffers));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object convertFrom(final JsonReader reader) {
|
public <V> V convertFrom(final JsonReader reader) {
|
||||||
if (reader == null) return null;
|
if (reader == null) return null;
|
||||||
return new JsonAnyDecoder(factory).convertFrom(reader);
|
return (V) new AnyDecoder(factory).convertFrom(reader);
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------ convertTo -----------------------------------------------------------
|
//------------------------------ convertTo -----------------------------------------------------------
|
||||||
|
|||||||
@@ -19,10 +19,6 @@ import org.redkale.util.*;
|
|||||||
*/
|
*/
|
||||||
public class JsonReader extends Reader {
|
public class JsonReader extends Reader {
|
||||||
|
|
||||||
static enum ValueType {
|
|
||||||
STRING, COLLECTION, JSONOBJECT;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected int position = -1;
|
protected int position = -1;
|
||||||
|
|
||||||
private char[] text;
|
private char[] text;
|
||||||
@@ -162,15 +158,16 @@ public class JsonReader extends Reader {
|
|||||||
this.position--;
|
this.position--;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ValueType readType() {
|
@Override
|
||||||
|
public final ValueType readType() {
|
||||||
char ch = nextGoodChar();
|
char ch = nextGoodChar();
|
||||||
if (ch == '{') {
|
if (ch == '{') {
|
||||||
backChar(ch);
|
backChar(ch);
|
||||||
return ValueType.JSONOBJECT;
|
return ValueType.MAP;
|
||||||
}
|
}
|
||||||
if (ch == '[') {
|
if (ch == '[') {
|
||||||
backChar(ch);
|
backChar(ch);
|
||||||
return ValueType.COLLECTION;
|
return ValueType.ARRAY;
|
||||||
}
|
}
|
||||||
backChar(ch);
|
backChar(ch);
|
||||||
return ValueType.STRING;
|
return ValueType.STRING;
|
||||||
|
|||||||
Reference in New Issue
Block a user