This commit is contained in:
redkale
2024-10-24 20:59:54 +08:00
parent ec17eea1e0
commit 60a47d33c4
23 changed files with 234 additions and 384 deletions

View File

@@ -324,7 +324,9 @@ public class InnerCoderEntity {
@Override
public InnerCoderEntity convertFrom(Reader in) {
if (in.readObjectB(InnerCoderEntity.class) == null) return null;
if (!in.readObjectB(this)) {
return null;
}
int index = 0;
final Object[] params = new Object[deMembers.length];
while (in.hasNext()) {
@@ -336,7 +338,7 @@ public class InnerCoderEntity {
params[index++] = member.read(in);
}
}
in.readObjectE(InnerCoderEntity.class);
in.readObjectE();
return InnerCoderEntity.create(params[0] == null ? 0 : (Integer) params[0], (String) params[1]);
}
};

View File

@@ -90,19 +90,12 @@ public class ArrayDecoder<R extends Reader, T> implements Decodeable<R, T[]> {
public T[] convertFrom(R in) {
this.checkInited();
final Decodeable<R, T> itemDecoder = this.componentDecoder;
int len = in.readArrayB(itemDecoder);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(itemDecoder)) {
return null;
}
final List<T> result = new ArrayList();
if (len == Reader.SIGN_VARIABLE) {
while (in.hasNext()) {
result.add(itemDecoder.convertFrom(in));
}
} else { // 固定长度
for (int i = 0; i < len; i++) {
result.add(itemDecoder.convertFrom(in));
}
while (in.hasNext()) {
result.add(itemDecoder.convertFrom(in));
}
in.readArrayE();
T[] rs = this.componentArrayFunction.apply(result.size());

View File

@@ -95,19 +95,12 @@ public class CollectionDecoder<R extends Reader, T> implements Decodeable<R, Col
public Collection<T> convertFrom(R in) {
this.checkInited();
final Decodeable<R, T> itemDecoder = this.componentDecoder;
int size = in.readArrayB(itemDecoder);
if (size == Reader.SIGN_NULL) {
if (!in.readArrayB(itemDecoder)) {
return null;
}
final Collection<T> result = this.creator.create();
if (size == Reader.SIGN_VARIABLE) {
while (in.hasNext()) {
result.add(itemDecoder.convertFrom(in));
}
} else { // 固定长度
for (int i = 0; i < size; i++) {
result.add(itemDecoder.convertFrom(in));
}
while (in.hasNext()) {
result.add(itemDecoder.convertFrom(in));
}
in.readArrayE();
return result;

View File

@@ -118,25 +118,15 @@ public class MapDecoder<R extends Reader, K, V> implements Decodeable<R, Map<K,
this.checkInited();
Decodeable<R, K> kdecoder = this.keyDecoder;
Decodeable<R, V> vdecoder = this.valueDecoder;
int len = in.readMapB(kdecoder, vdecoder);
if (len == Reader.SIGN_NULL) {
if (!in.readMapB(kdecoder, vdecoder)) {
return null;
}
final Map<K, V> result = this.creator.create();
if (len == Reader.SIGN_VARIABLE) {
while (in.hasNext()) {
K key = kdecoder.convertFrom(in);
in.readColon();
V value = vdecoder.convertFrom(in);
result.put(key, value);
}
} else { // 固定长度
for (int i = 0; i < len; i++) {
K key = kdecoder.convertFrom(in);
in.readColon();
V value = vdecoder.convertFrom(in);
result.put(key, value);
}
while (in.hasNext()) {
K key = kdecoder.convertFrom(in);
in.readColon();
V value = vdecoder.convertFrom(in);
result.put(key, value);
}
in.readMapE();
return result;

View File

@@ -360,8 +360,7 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
@Override
public T convertFrom(final R in) {
this.checkInited();
final String clazz = in.readObjectB(typeClass);
if (clazz == null) {
if (!in.readObjectB(this)) {
return null;
}
if (this.creator == null) {
@@ -384,7 +383,7 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
member.getAttribute().set(result, val);
}
}
in.readObjectE(typeClass);
in.readObjectE();
return result;
} else { // 带参数的构造函数
final DeMember<R, T, ?>[] constructorFields = this.creatorConstructorMembers;
@@ -411,7 +410,7 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
}
}
}
in.readObjectE(typeClass);
in.readObjectE();
if (this.creator == null) {
return null;
}

View File

@@ -16,20 +16,6 @@ import org.redkale.annotation.Nullable;
*/
public abstract class Reader {
/**
* 集合对象为null
*
* @see #readArrayB(org.redkale.convert.Decodeable)
*/
public static final short SIGN_NULL = -1;
/**
* 不确定的长度, 比如解析json数组
*
* @see #readArrayB(org.redkale.convert.Decodeable)
*/
public static final short SIGN_VARIABLE = -2;
/**
* 设置Reader的内容通常结合对象池使用
*
@@ -62,32 +48,26 @@ public abstract class Reader {
public abstract void readColon();
/**
* 读取对象的类名, 返回 null 表示对象为null 返回空字符串表示当前class与返回的class一致返回非空字符串表示class是当前class的子类。
* 读取对象返回false表示对象为null
*
* @param clazz 类名
*
* @return 返回字段数
* @param decoder Decodeable
* @return 是否存在对象
*/
public String readObjectB(final Class clazz) {
return null;
}
public abstract boolean readObjectB(final Decodeable decoder);
/**
* 读取对象的尾端
*
* @param clazz 类名
*/
public abstract void readObjectE(final Class clazz);
public abstract void readObjectE();
/**
* 读取数组的开头并返回数组的长度
* 读取数组返回false表示数组为null
*
* @see #SIGN_NULL
* @see #SIGN_VARIABLE
* @param componentDecoder Decodeable
* @return 返回数组的长度
* @return 是否存在对象
*/
public abstract int readArrayB(@Nullable Decodeable componentDecoder);
public abstract boolean readArrayB(@Nullable Decodeable componentDecoder);
/**
* 读取数组的尾端
@@ -95,13 +75,13 @@ public abstract class Reader {
public abstract void readArrayE();
/**
* 读取map的开头并返回map的size
* 读取map返回false表示map为null
*
* @param keyDecoder Decodeable
* @param valueDecoder Decodeable
* @return 返回map的size
* @return 是否存在对象
*/
public abstract int readMapB(Decodeable keyDecoder, Decodeable valueDecoder);
public abstract boolean readMapB(Decodeable keyDecoder, Decodeable valueDecoder);
/**
* 读取Map的尾端

View File

@@ -80,19 +80,12 @@ public class StreamDecoder<R extends Reader, T> implements Decodeable<R, Stream<
public Stream<T> convertFrom(R in) {
this.checkInited();
final Decodeable<R, T> itemDecoder = this.componentDecoder;
int len = in.readArrayB(itemDecoder);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(itemDecoder)) {
return null;
}
final List<T> result = new ArrayList();
if (len == Reader.SIGN_VARIABLE) {
while (in.hasNext()) {
result.add(itemDecoder.convertFrom(in));
}
} else { // 固定长度
for (int i = 0; i < len; i++) {
result.add(itemDecoder.convertFrom(in));
}
while (in.hasNext()) {
result.add(itemDecoder.convertFrom(in));
}
in.readArrayE();
return result.stream();

View File

@@ -40,32 +40,22 @@ public final class BoolArraySimpledCoder<R extends Reader, W extends Writer> ext
@Override
public boolean[] convertFrom(R in) {
int len = in.readArrayB(BoolSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(BoolSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
boolean[] data = new boolean[8];
while (in.hasNext()) {
if (size >= data.length) {
boolean[] newdata = new boolean[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readBoolean();
int size = 0;
boolean[] data = new boolean[8];
while (in.hasNext()) {
if (size >= data.length) {
boolean[] newdata = new boolean[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
boolean[] newdata = new boolean[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else {
boolean[] values = new boolean[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readBoolean();
}
in.readArrayE();
return values;
data[size++] = in.readBoolean();
}
in.readArrayE();
boolean[] newdata = new boolean[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
}

View File

@@ -41,30 +41,20 @@ public final class ByteBufferSimpledCoder<R extends Reader, W extends Writer> ex
@Override
public ByteBuffer convertFrom(R in) {
int len = in.readArrayB(ByteSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(ByteSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
byte[] data = new byte[8];
while (in.hasNext()) {
if (size >= data.length) {
byte[] newdata = new byte[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readByte();
int size = 0;
byte[] data = new byte[8];
while (in.hasNext()) {
if (size >= data.length) {
byte[] newdata = new byte[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
return ByteBuffer.wrap(data, 0, size);
} else {
byte[] values = new byte[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readByte();
}
in.readArrayE();
return ByteBuffer.wrap(values);
data[size++] = in.readByte();
}
in.readArrayE();
return ByteBuffer.wrap(data, 0, size);
}
}

View File

@@ -40,32 +40,22 @@ public final class CharArraySimpledCoder<R extends Reader, W extends Writer> ext
@Override
public char[] convertFrom(R in) {
int len = in.readArrayB(CharSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(CharSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
char[] data = new char[8];
while (in.hasNext()) {
if (size >= data.length) {
char[] newdata = new char[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readChar();
int size = 0;
char[] data = new char[8];
while (in.hasNext()) {
if (size >= data.length) {
char[] newdata = new char[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
char[] newdata = new char[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else {
char[] values = new char[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readChar();
}
in.readArrayE();
return values;
data[size++] = in.readChar();
}
in.readArrayE();
char[] newdata = new char[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
}

View File

@@ -29,7 +29,7 @@ public final class CompletionHandlerSimpledCoder<R extends Reader, W extends Wri
@Override
public CompletionHandler convertFrom(R in) {
in.readObjectB(CompletionHandler.class);
in.readObjectB(this);
return null;
}
}

View File

@@ -41,33 +41,23 @@ public final class DoubleArraySimpledCoder<R extends Reader, W extends Writer> e
@Override
public double[] convertFrom(R in) {
int len = in.readArrayB(DoubleSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(DoubleSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
double[] data = new double[8];
while (in.hasNext()) {
if (size >= data.length) {
double[] newdata = new double[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readDouble();
int size = 0;
double[] data = new double[8];
while (in.hasNext()) {
if (size >= data.length) {
double[] newdata = new double[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
double[] newdata = new double[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else {
double[] values = new double[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readDouble();
}
in.readArrayE();
return values;
data[size++] = in.readDouble();
}
in.readArrayE();
double[] newdata = new double[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
public static final class DoubleStreamSimpledCoder<R extends Reader, W extends Writer>

View File

@@ -40,32 +40,22 @@ public final class FloatArraySimpledCoder<R extends Reader, W extends Writer> ex
@Override
public float[] convertFrom(R in) {
int len = in.readArrayB(FloatSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(FloatSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
float[] data = new float[8];
while (in.hasNext()) {
if (size >= data.length) {
float[] newdata = new float[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readFloat();
int size = 0;
float[] data = new float[8];
while (in.hasNext()) {
if (size >= data.length) {
float[] newdata = new float[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
float[] newdata = new float[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else {
float[] values = new float[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readFloat();
}
in.readArrayE();
return values;
data[size++] = in.readFloat();
}
in.readArrayE();
float[] newdata = new float[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
}

View File

@@ -41,33 +41,23 @@ public final class IntArraySimpledCoder<R extends Reader, W extends Writer> exte
@Override
public int[] convertFrom(R in) {
int len = in.readArrayB(IntSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(IntSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
int[] data = new int[8];
while (in.hasNext()) {
if (size >= data.length) {
int[] newdata = new int[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readInt();
int size = 0;
int[] data = new int[8];
while (in.hasNext()) {
if (size >= data.length) {
int[] newdata = new int[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
int[] newdata = new int[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else {
int[] values = new int[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readInt();
}
in.readArrayE();
return values;
data[size++] = in.readInt();
}
in.readArrayE();
int[] newdata = new int[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
public static final class IntStreamSimpledCoder<R extends Reader, W extends Writer>

View File

@@ -41,33 +41,23 @@ public final class LongArraySimpledCoder<R extends Reader, W extends Writer> ext
@Override
public long[] convertFrom(R in) {
int len = in.readArrayB(LongSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(LongSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
long[] data = new long[8];
while (in.hasNext()) {
if (size >= data.length) {
long[] newdata = new long[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readLong();
int size = 0;
long[] data = new long[8];
while (in.hasNext()) {
if (size >= data.length) {
long[] newdata = new long[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
long[] newdata = new long[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else {
long[] values = new long[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readLong();
}
in.readArrayE();
return values;
data[size++] = in.readLong();
}
in.readArrayE();
long[] newdata = new long[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
public static final class LongStreamSimpledCoder<R extends Reader, W extends Writer>

View File

@@ -40,32 +40,22 @@ public final class ShortArraySimpledCoder<R extends Reader, W extends Writer> ex
@Override
public short[] convertFrom(R in) {
int len = in.readArrayB(ShortSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(ShortSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
short[] data = new short[8];
while (in.hasNext()) {
if (size >= data.length) {
short[] newdata = new short[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readShort();
int size = 0;
short[] data = new short[8];
while (in.hasNext()) {
if (size >= data.length) {
short[] newdata = new short[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
short[] newdata = new short[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else {
short[] values = new short[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readShort();
}
in.readArrayE();
return values;
data[size++] = in.readShort();
}
in.readArrayE();
short[] newdata = new short[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
}

View File

@@ -40,32 +40,22 @@ public final class StringArraySimpledCoder<R extends Reader, W extends Writer> e
@Override
public String[] convertFrom(R in) {
int len = in.readArrayB(StringSimpledCoder.instance);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(StringSimpledCoder.instance)) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
String[] data = new String[8];
while (in.hasNext()) {
if (size >= data.length) {
String[] newdata = new String[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = in.readString();
int size = 0;
String[] data = new String[8];
while (in.hasNext()) {
if (size >= data.length) {
String[] newdata = new String[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
in.readArrayE();
String[] newdata = new String[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else {
String[] values = new String[len];
for (int i = 0; i < values.length; i++) {
values[i] = in.readString();
}
in.readArrayE();
return values;
data[size++] = in.readString();
}
in.readArrayE();
String[] newdata = new String[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
}

View File

@@ -8,7 +8,6 @@ package org.redkale.convert.json;
import java.nio.ByteBuffer;
import java.nio.charset.UnmappableCharacterException;
import org.redkale.convert.*;
import static org.redkale.convert.Reader.*;
import org.redkale.util.ByteTreeNode;
/**
@@ -170,42 +169,43 @@ public class JsonByteBufferReader extends JsonReader {
}
/**
* 判断下一个非空白字符是否为{
* 读取对象返回false表示对象为null
*
* @return SIGN_VARIABLE 或 SIGN_NULL
* @param decoder Decodeable
* @return 是否存在对象
*/
@Override
public final String readObjectB(final Class clazz) {
public final boolean readObjectB(Decodeable decoder) {
char ch = nextGoodChar(true);
if (ch == '{') {
return "";
return true;
}
if (ch == 'n' && nextChar() == 'u' && nextChar() == 'l' && nextChar() == 'l') {
return null;
return false;
}
if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') {
return null;
return false;
}
throw new ConvertException("a json object must begin with '{' (position = " + position + ") but '" + ch + "'");
}
/**
* 判断下一个非空白字符是否为[
* 读取数组返回false表示数组为null
*
* @param decoder Decodeable
* @return SIGN_VARIABLE 或 SIGN_NULL
* @param componentDecoder Decodeable
* @return 是否存在对象
*/
@Override
public final int readArrayB(Decodeable decoder) {
public final boolean readArrayB(Decodeable componentDecoder) {
char ch = nextGoodChar(true);
if (ch == '[' || ch == '{') {
return SIGN_VARIABLE;
return true;
}
if (ch == 'n' && nextChar() == 'u' && nextChar() == 'l' && nextChar() == 'l') {
return SIGN_NULL;
return false;
}
if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') {
return SIGN_NULL;
return false;
}
int pos = this.position;
throw new ConvertException("a json array must begin with '[' (position = " + pos + ") but '" + ch + "'");

View File

@@ -35,8 +35,7 @@ public class JsonMultiArrayDecoder implements Decodeable<JsonReader, Object[]> {
@Override
public Object[] convertFrom(JsonReader in) {
int len = in.readArrayB(null);
if (len == Reader.SIGN_NULL) {
if (!in.readArrayB(null)) {
return null;
}
// len must be Reader.SIGN_VARIABLE

View File

@@ -102,8 +102,7 @@ public class JsonMultiImplDecoder<T> implements Decodeable<JsonReader, T> {
@Override
public T convertFrom(JsonReader in) {
final String clazz = in.readObjectB(null);
if (clazz == null) {
if (!in.readObjectB(this)) {
return null;
}
ObjectDecoder decoder = this.firstDecoder;
@@ -150,7 +149,7 @@ public class JsonMultiImplDecoder<T> implements Decodeable<JsonReader, T> {
params[++index] = new Object[] {member.getAttribute(), member.read(in)};
}
}
in.readObjectE(null);
in.readObjectE();
if (decoder.getConstructorMembers() == null) { // 空构造函数
T result = (T) decoder.getCreator().create();
for (int i = 0; i <= index; i++) {

View File

@@ -8,7 +8,6 @@ package org.redkale.convert.json;
import java.nio.charset.StandardCharsets;
import java.util.*;
import org.redkale.convert.*;
import static org.redkale.convert.Reader.*;
import org.redkale.util.ByteTreeNode;
import org.redkale.util.Utility;
@@ -270,75 +269,58 @@ public class JsonReader extends Reader {
}
/**
* 判断下一个非空白字符是否为{
* 读取对象返回false表示对象为null
*
* @param clazz 类名
* @return 返回 null 表示对象为null 返回空字符串表示当前class与返回的class一致返回非空字符串表示class是当前class的子类。
* @param decoder Decodeable
* @return 是否存在对象
*/
@Override
public String readObjectB(final Class clazz) {
public boolean readObjectB(final Decodeable decoder) {
if (this.text.length == 0) {
return null;
return false;
}
char ch = nextGoodChar(true);
if (ch == '{') {
return "";
return true;
}
if (ch == 'n' && text[++position] == 'u' && text[++position] == 'l' && text[++position] == 'l') {
return null;
return false;
}
if (ch == 'N' && text[++position] == 'U' && text[++position] == 'L' && text[++position] == 'L') {
return null;
return false;
}
throw new ConvertException("a json object must begin with '{' (position = " + position + ") but '" + ch
+ "' in " + new String(this.text));
}
@Override
public final void readObjectE(final Class clazz) {
public final void readObjectE() {
// do nothing
}
/**
* 判断下一个非空白字符是否为{
*
* @param keyDecoder Decodeable
* @param valuedecoder Decodeable
* @return SIGN_VARIABLE 或 SIGN_NULL
*/
@Override
public final int readMapB(Decodeable keyDecoder, Decodeable valuedecoder) {
return readArrayB(keyDecoder);
}
@Override
public final void readMapE() {
// do nothing
}
/**
* 判断下一个非空白字符是否为[
* 读取数组返回false表示数组为null
*
* @param componentDecoder Decodeable
* @return SIGN_VARIABLE 或 SIGN_NULL
* @return 是否存在对象
*/
@Override
public int readArrayB(Decodeable componentDecoder) {
public boolean readArrayB(Decodeable componentDecoder) {
if (this.text.length == 0) {
return SIGN_NULL;
return false;
}
char ch = nextGoodChar(true);
if (ch == '[') {
return SIGN_VARIABLE;
return true;
}
if (ch == '{') {
return SIGN_VARIABLE;
return true;
}
if (ch == 'n' && text[++position] == 'u' && text[++position] == 'l' && text[++position] == 'l') {
return SIGN_NULL;
return false;
}
if (ch == 'N' && text[++position] == 'U' && text[++position] == 'L' && text[++position] == 'L') {
return SIGN_NULL;
return false;
}
throw new ConvertException("a json array text must begin with '[' (position = " + position + ") but '" + ch
+ "' in " + new String(this.text));
@@ -349,6 +331,23 @@ public class JsonReader extends Reader {
// do nothing
}
/**
* 读取map返回false表示map为null
*
* @param keyDecoder Decodeable
* @param valueDecoder Decodeable
* @return 是否存在对象
*/
@Override
public final boolean readMapB(Decodeable keyDecoder, Decodeable valueDecoder) {
return readArrayB(keyDecoder);
}
@Override
public final void readMapE() {
// do nothing
}
/** 判断下一个非空白字符是否: */
@Override
public void readColon() {
@@ -720,33 +719,24 @@ public class JsonReader extends Reader {
@Override
public final byte[] readByteArray() {
int len = readArrayB(null);
if (len == Reader.SIGN_NULL) {
boolean has = readArrayB(null);
if (!has) {
return null;
}
if (len == Reader.SIGN_VARIABLE) {
int size = 0;
byte[] data = new byte[8];
while (hasNext()) {
if (size >= data.length) {
byte[] newdata = new byte[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
data[size++] = readByte();
int size = 0;
byte[] data = new byte[8];
while (hasNext()) {
if (size >= data.length) {
byte[] newdata = new byte[data.length + 4];
System.arraycopy(data, 0, newdata, 0, size);
data = newdata;
}
readArrayE();
byte[] newdata = new byte[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
} else { // 固定长度
byte[] values = new byte[len];
for (int i = 0; i < values.length; i++) {
values[i] = readByte();
}
readArrayE();
return values;
data[size++] = readByte();
}
readArrayE();
byte[] newdata = new byte[size];
System.arraycopy(data, 0, newdata, 0, size);
return newdata;
}
@Override

View File

@@ -110,22 +110,12 @@ public class ProtobufReader extends Reader {
}
@Override
public final String readObjectB(final Class clazz) {
return hasNext() ? "" : null;
public final boolean readObjectB(final Decodeable decoder) {
return hasNext();
}
@Override
public final void readObjectE(final Class clazz) {
// do nothing
}
@Override
public final int readMapB(Decodeable keyDecoder, Decodeable valueDecoder) {
return Reader.SIGN_VARIABLE;
}
@Override
public final void readMapE() {
public final void readObjectE() {
// do nothing
}
@@ -133,11 +123,11 @@ public class ProtobufReader extends Reader {
* 判断下一个非空白字符是否为[
*
* @param componentDecoder Decodeable
* @return SIGN_VARIABLE 或 SIGN_NULL
* @return 是否存在对象
*/
@Override
public final int readArrayB(Decodeable componentDecoder) {
return Reader.SIGN_VARIABLE;
public final boolean readArrayB(Decodeable componentDecoder) {
return true;
}
@Override
@@ -145,6 +135,16 @@ public class ProtobufReader extends Reader {
// do nothing
}
@Override
public final boolean readMapB(Decodeable keyDecoder, Decodeable valueDecoder) {
return true;
}
@Override
public final void readMapE() {
// do nothing
}
/** 判断下一个非空白字节是否: */
@Override
public final void readColon() {

View File

@@ -106,7 +106,9 @@ public class InnerCoderEntityTest {
@Override
public InnerCoderEntity convertFrom(Reader in) {
if (in.readObjectB(InnerCoderEntity.class) == null) return null;
if (!in.readObjectB(this)) {
return null;
}
int index = 0;
final Object[] params = new Object[deMembers.length];
while (in.hasNext()) {
@@ -118,7 +120,7 @@ public class InnerCoderEntityTest {
params[index++] = member.read(in);
}
}
in.readObjectE(InnerCoderEntity.class);
in.readObjectE();
return InnerCoderEntity.create(params[0] == null ? 0 : (Integer) params[0], (String) params[1]);
}
};