diff --git a/src/org/redkale/convert/json/JsonAnyDecoder.java b/src/org/redkale/convert/AnyDecoder.java
similarity index 69%
rename from src/org/redkale/convert/json/JsonAnyDecoder.java
rename to src/org/redkale/convert/AnyDecoder.java
index 7b259971d..6c7050521 100644
--- a/src/org/redkale/convert/json/JsonAnyDecoder.java
+++ b/src/org/redkale/convert/AnyDecoder.java
@@ -3,23 +3,19 @@
* To change this template file, choose Tools | Templates
* 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 org.redkale.convert.*;
-import org.redkale.convert.json.JsonReader.ValueType;
import org.redkale.util.*;
+import org.redkale.convert.Reader.ValueType;
+import static org.redkale.convert.Reader.ValueType.MAP;
/**
- * 对不明类型的对象进行JSON反序列化。
- *
- * 详情见: https://redkale.org
*
* @author zhangjx
*/
-@SuppressWarnings("unchecked")
-public final class JsonAnyDecoder implements Decodeable {
+public class AnyDecoder implements Decodeable {
private static final Type collectionObjectType = new TypeToken>() {
}.getType();
@@ -31,26 +27,26 @@ public final class JsonAnyDecoder implements Decodeable {
private static final Creator mapCreator = Creator.create(HashMap.class);
- protected final Decodeable stringDecoder;
+ protected final Decodeable stringDecoder;
protected final CollectionDecoder collectionDecoder;
protected final MapDecoder mapDecoder;
- public JsonAnyDecoder(final ConvertFactory factory) {
+ public AnyDecoder(final ConvertFactory factory) {
this.stringDecoder = factory.loadDecoder(String.class);
this.collectionDecoder = new CollectionDecoder(factory, collectionObjectType, Object.class, collectionCreator, this);
this.mapDecoder = new MapDecoder(factory, mapObjectType, String.class, Object.class, mapCreator, stringDecoder, this);
}
@Override
- public Object convertFrom(JsonReader in) {
+ public Object convertFrom(Reader in) {
ValueType vt = in.readType();
if (vt == null) return null;
switch (vt) {
- case COLLECTION:
+ case ARRAY:
return this.collectionDecoder.convertFrom(in);
- case JSONOBJECT:
+ case MAP:
return this.mapDecoder.convertFrom(in);
}
return this.stringDecoder.convertFrom(in);
diff --git a/src/org/redkale/convert/Reader.java b/src/org/redkale/convert/Reader.java
index 7aaedf689..c9aa9a102 100644
--- a/src/org/redkale/convert/Reader.java
+++ b/src/org/redkale/convert/Reader.java
@@ -15,6 +15,10 @@ package org.redkale.convert;
*/
public abstract class Reader {
+ public static enum ValueType {
+ STRING, ARRAY, MAP;
+ }
+
//当前对象字段名的游标
protected int fieldIndex;
@@ -73,6 +77,13 @@ public abstract class Reader {
*/
public abstract void readBlank();
+ /**
+ * 读取下个值的类型
+ *
+ * @return ValueType
+ */
+ public abstract ValueType readType();
+
/**
* 读取对象的类名, 返回 null 表示对象为null, 返回空字符串表示当前class与返回的class一致,返回非空字符串表示class是当前class的子类。
*
diff --git a/src/org/redkale/convert/bson/BsonReader.java b/src/org/redkale/convert/bson/BsonReader.java
index fd7649e78..651e0cfdc 100644
--- a/src/org/redkale/convert/bson/BsonReader.java
+++ b/src/org/redkale/convert/bson/BsonReader.java
@@ -346,4 +346,9 @@ public class BsonReader extends Reader {
return value;
}
+ @Override
+ public ValueType readType() {
+ throw new UnsupportedOperationException("Not supported yet.");
+ }
+
}
diff --git a/src/org/redkale/convert/json/JsonConvert.java b/src/org/redkale/convert/json/JsonConvert.java
index 275f9adc5..b611f2f2e 100644
--- a/src/org/redkale/convert/json/JsonConvert.java
+++ b/src/org/redkale/convert/json/JsonConvert.java
@@ -128,43 +128,43 @@ public final class JsonConvert extends TextConvert {
return rs;
}
- public Object convertFrom(final String text) {
+ public V convertFrom(final String text) {
if (text == null) return null;
- return convertFrom(Utility.charArray(text));
+ return (V) convertFrom(Utility.charArray(text));
}
- public Object convertFrom(final char[] text) {
+ public V convertFrom(final char[] text) {
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 convertFrom(final char[] text, final int start, final int len) {
if (text == null) return null;
final JsonReader in = readerPool.get();
in.setText(text, start, len);
- Object rs = new JsonAnyDecoder(factory).convertFrom(in);
+ Object rs = new AnyDecoder(factory).convertFrom(in);
readerPool.accept(in);
- return rs;
+ return (V) rs;
}
- public Object convertFrom(final InputStream in) {
+ public V convertFrom(final InputStream in) {
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 convertFrom(final ByteBuffer... buffers) {
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 convertFrom(final ConvertMask mask, final ByteBuffer... buffers) {
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 convertFrom(final JsonReader reader) {
if (reader == null) return null;
- return new JsonAnyDecoder(factory).convertFrom(reader);
+ return (V) new AnyDecoder(factory).convertFrom(reader);
}
//------------------------------ convertTo -----------------------------------------------------------
diff --git a/src/org/redkale/convert/json/JsonReader.java b/src/org/redkale/convert/json/JsonReader.java
index 30a765295..c3435aac8 100644
--- a/src/org/redkale/convert/json/JsonReader.java
+++ b/src/org/redkale/convert/json/JsonReader.java
@@ -19,10 +19,6 @@ import org.redkale.util.*;
*/
public class JsonReader extends Reader {
- static enum ValueType {
- STRING, COLLECTION, JSONOBJECT;
- }
-
protected int position = -1;
private char[] text;
@@ -162,15 +158,16 @@ public class JsonReader extends Reader {
this.position--;
}
- final ValueType readType() {
+ @Override
+ public final ValueType readType() {
char ch = nextGoodChar();
if (ch == '{') {
backChar(ch);
- return ValueType.JSONOBJECT;
+ return ValueType.MAP;
}
if (ch == '[') {
backChar(ch);
- return ValueType.COLLECTION;
+ return ValueType.ARRAY;
}
backChar(ch);
return ValueType.STRING;