This commit is contained in:
@@ -21,14 +21,14 @@ import org.redkale.util.Attribute;
|
||||
@SuppressWarnings("unchecked")
|
||||
public final class EnMember<W extends Writer, T, F> implements Comparable<EnMember<W, T, F>> {
|
||||
|
||||
private final Attribute<T, F> attribute;
|
||||
final Attribute<T, F> attribute;
|
||||
|
||||
final Encodeable<W, F> encoder;
|
||||
|
||||
private final boolean istring;
|
||||
final boolean istring;
|
||||
|
||||
//private final boolean isnumber;
|
||||
private final boolean isbool;
|
||||
//final boolean isnumber;
|
||||
final boolean isbool;
|
||||
|
||||
public EnMember(Attribute<T, F> attribute, Encodeable<W, F> encoder) {
|
||||
this.attribute = attribute;
|
||||
@@ -52,21 +52,6 @@ public final class EnMember<W extends Writer, T, F> implements Comparable<EnMemb
|
||||
return attribute.field().equals(name);
|
||||
}
|
||||
|
||||
public boolean write(final W out, final boolean comma, final T obj) {
|
||||
F value = attribute.get(obj);
|
||||
if (value == null) return comma;
|
||||
if (out.tiny()) {
|
||||
if (istring) {
|
||||
if (((CharSequence) value).length() == 0) return comma;
|
||||
} else if (isbool) {
|
||||
if (!((Boolean) value)) return comma;
|
||||
}
|
||||
}
|
||||
out.writeField(comma, attribute);
|
||||
encoder.convertTo(out, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final int compareTo(EnMember<W, T, F> o) {
|
||||
if (o == null) return 1;
|
||||
|
||||
@@ -10,12 +10,13 @@ import java.lang.reflect.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p> 详情见: http://www.redkale.org
|
||||
* <p>
|
||||
* 详情见: http://www.redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <R> Reader输入的子类
|
||||
* @param <T> 反解析的数据类型
|
||||
@@ -164,9 +165,9 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
||||
*/
|
||||
@Override
|
||||
public final T convertFrom(final R in) {
|
||||
final String clazz = in.readClassName();
|
||||
if (clazz != null && !clazz.isEmpty()) return (T) factory.loadDecoder(factory.getEntity(clazz)).convertFrom(in);
|
||||
if (in.readObjectB() == Reader.SIGN_NULL) return null;
|
||||
final String clazz = in.readObjectB(typeClass);
|
||||
if (clazz == null) return null;
|
||||
if (!clazz.isEmpty()) return (T) factory.loadDecoder(factory.getEntity(clazz)).convertFrom(in);
|
||||
if (!this.inited) {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
@@ -178,16 +179,14 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
||||
}
|
||||
if (this.creatorConstructorMembers == null) { //空构造函数
|
||||
final T result = this.creator.create();
|
||||
final AtomicInteger index = new AtomicInteger();
|
||||
while (in.hasNext()) {
|
||||
DeMember member = in.readField(index, members);
|
||||
DeMember member = in.readFieldName(members);
|
||||
in.skipBlank();
|
||||
if (member == null) {
|
||||
in.skipValue(); //跳过不存在的属性的值
|
||||
} else {
|
||||
member.read(in, result);
|
||||
}
|
||||
index.incrementAndGet();
|
||||
}
|
||||
in.readObjectE();
|
||||
return result;
|
||||
@@ -195,10 +194,9 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
||||
final DeMember<R, T, ?>[] fields = this.creatorConstructorMembers;
|
||||
final Object[] constructorParams = new Object[fields.length];
|
||||
final Object[][] otherParams = new Object[this.members.length][2];
|
||||
final AtomicInteger index = new AtomicInteger();
|
||||
int oc = 0;
|
||||
while (in.hasNext()) {
|
||||
DeMember member = in.readField(index, members);
|
||||
DeMember member = in.readFieldName(members);
|
||||
in.skipBlank();
|
||||
if (member == null) {
|
||||
in.skipValue(); //跳过不存在的属性的值
|
||||
@@ -214,7 +212,6 @@ public final class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T
|
||||
}
|
||||
if (flag) otherParams[oc++] = new Object[]{member.attribute, val};
|
||||
}
|
||||
index.incrementAndGet();
|
||||
}
|
||||
in.readObjectE();
|
||||
final T result = this.creator.create(constructorParams);
|
||||
|
||||
@@ -12,7 +12,9 @@ import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p> 详情见: http://www.redkale.org
|
||||
* <p>
|
||||
* 详情见: http://www.redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <W> Writer输出的子类
|
||||
* @param <T> 序列化的数据类型
|
||||
@@ -102,8 +104,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
|
||||
@Override
|
||||
public final void convertTo(W out, T value) {
|
||||
if (value == null) {
|
||||
out.wirteClassName(null);
|
||||
out.writeNull();
|
||||
out.writeObjectNull(null);
|
||||
return;
|
||||
}
|
||||
if (!this.inited) {
|
||||
@@ -121,10 +122,9 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
|
||||
factory.loadEncoder(clz).convertTo(out, value);
|
||||
return;
|
||||
}
|
||||
out.writeObjectB(members.length, value);
|
||||
boolean comma = false;
|
||||
out.writeObjectB(value);
|
||||
for (EnMember member : members) {
|
||||
comma = member.write(out, comma, value);
|
||||
out.writeObjectField(member, value);
|
||||
}
|
||||
out.writeObjectE(value);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,6 @@
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>
|
||||
@@ -14,7 +12,10 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public interface Reader {
|
||||
public abstract class Reader {
|
||||
|
||||
//当前对象字段名的游标
|
||||
protected int fieldIndex;
|
||||
|
||||
public static final short SIGN_NULL = -1;
|
||||
|
||||
@@ -25,141 +26,143 @@ public interface Reader {
|
||||
*
|
||||
* @return 是否还存在下个元素或字段
|
||||
*/
|
||||
public boolean hasNext();
|
||||
public abstract boolean hasNext();
|
||||
|
||||
/**
|
||||
* 跳过值(不包含值前面的字段)
|
||||
*/
|
||||
public void skipValue();
|
||||
public abstract void skipValue();
|
||||
|
||||
/**
|
||||
* /跳过字段与值之间的多余内容, json就是跳过:符, map跳过:
|
||||
*/
|
||||
public void skipBlank();
|
||||
public abstract void skipBlank();
|
||||
|
||||
/**
|
||||
* 读取对象的开头 返回字段数
|
||||
* 读取对象的类名, 返回 null 表示对象为null, 返回空字符串表示当前class与返回的class一致,返回非空字符串表示class是当前class的子类。
|
||||
*
|
||||
* @return 返回字段数
|
||||
*/
|
||||
public int readObjectB();
|
||||
public String readObjectB(final Class clazz) {
|
||||
this.fieldIndex = 0;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取对象的尾端
|
||||
*
|
||||
*/
|
||||
public void readObjectE();
|
||||
public abstract void readObjectE();
|
||||
|
||||
/**
|
||||
* 读取数组的开头并返回数组的长度
|
||||
*
|
||||
* @return 返回数组的长度
|
||||
*/
|
||||
public int readArrayB();
|
||||
public abstract int readArrayB();
|
||||
|
||||
/**
|
||||
* 读取数组的尾端
|
||||
*
|
||||
*/
|
||||
public void readArrayE();
|
||||
public abstract void readArrayE();
|
||||
|
||||
/**
|
||||
* 读取map的开头并返回map的size
|
||||
*
|
||||
* @return 返回map的size
|
||||
*/
|
||||
public int readMapB();
|
||||
public abstract int readMapB();
|
||||
|
||||
/**
|
||||
* 读取数组的尾端
|
||||
*
|
||||
*/
|
||||
public void readMapE();
|
||||
public abstract void readMapE();
|
||||
|
||||
/**
|
||||
* 根据字段读取字段对应的DeMember
|
||||
*
|
||||
* @param index 当前members的游标位置
|
||||
* @param members DeMember的全量集合
|
||||
* @return 匹配的DeMember
|
||||
*/
|
||||
public DeMember readField(final AtomicInteger index, final DeMember[] members);
|
||||
public abstract DeMember readFieldName(final DeMember[] members);
|
||||
|
||||
/**
|
||||
* 读取一个boolean值
|
||||
*
|
||||
* @return boolean值
|
||||
*/
|
||||
public boolean readBoolean();
|
||||
public abstract boolean readBoolean();
|
||||
|
||||
/**
|
||||
* 读取一个byte值
|
||||
*
|
||||
* @return byte值
|
||||
*/
|
||||
public byte readByte();
|
||||
public abstract byte readByte();
|
||||
|
||||
/**
|
||||
* 读取一个char值
|
||||
*
|
||||
* @return char值
|
||||
*/
|
||||
public char readChar();
|
||||
public abstract char readChar();
|
||||
|
||||
/**
|
||||
* 读取一个short值
|
||||
*
|
||||
* @return short值
|
||||
*/
|
||||
public short readShort();
|
||||
public abstract short readShort();
|
||||
|
||||
/**
|
||||
* 读取一个int值
|
||||
*
|
||||
* @return int值
|
||||
*/
|
||||
public int readInt();
|
||||
public abstract int readInt();
|
||||
|
||||
/**
|
||||
* 读取一个long值
|
||||
*
|
||||
* @return long值
|
||||
*/
|
||||
public long readLong();
|
||||
public abstract long readLong();
|
||||
|
||||
/**
|
||||
* 读取一个float值
|
||||
*
|
||||
* @return float值
|
||||
*/
|
||||
public float readFloat();
|
||||
public abstract float readFloat();
|
||||
|
||||
/**
|
||||
* 读取一个double值
|
||||
*
|
||||
* @return double值
|
||||
*/
|
||||
public double readDouble();
|
||||
public abstract double readDouble();
|
||||
|
||||
/**
|
||||
* 读取无转义字符长度不超过255的字符串, 例如枚举值、字段名、类名字符串等
|
||||
*
|
||||
* @return String值
|
||||
*/
|
||||
public String readSmallString();
|
||||
public abstract String readSmallString();
|
||||
|
||||
/**
|
||||
* 读取反解析对象的类名
|
||||
*
|
||||
* @return 类名
|
||||
*/
|
||||
public String readClassName();
|
||||
public abstract String readClassName();
|
||||
|
||||
/**
|
||||
* 读取一个String值
|
||||
*
|
||||
* @return String值
|
||||
*/
|
||||
public String readString();
|
||||
public abstract String readString();
|
||||
|
||||
}
|
||||
|
||||
@@ -14,156 +14,191 @@ import org.redkale.util.Attribute;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public interface Writer {
|
||||
public abstract class Writer {
|
||||
|
||||
//当前对象输出字段名之前是否需要分隔符, JSON字段间的分隔符为,逗号
|
||||
protected boolean comma;
|
||||
|
||||
/**
|
||||
* 当tiny=true时, 字符串为空、boolean为false的字段值都会被跳过, 不会输出。
|
||||
*
|
||||
* @return 是否简化
|
||||
*/
|
||||
public boolean tiny();
|
||||
public abstract boolean tiny();
|
||||
|
||||
/**
|
||||
* 输出null值
|
||||
*/
|
||||
public void writeNull();
|
||||
public abstract void writeNull();
|
||||
|
||||
/**
|
||||
* 写入类名
|
||||
*
|
||||
* @param clazz 类名
|
||||
*/
|
||||
public void wirteClassName(String clazz);
|
||||
public abstract void wirteClassName(String clazz);
|
||||
|
||||
/**
|
||||
* 输出一个对象前的操作
|
||||
* 注: 覆盖此方法必须要先调用父方法 super.writeObjectB(obj);
|
||||
*
|
||||
* @param fieldCount 字段个数
|
||||
*
|
||||
* @param obj 写入的对象
|
||||
* @param obj 写入的对象
|
||||
*/
|
||||
public void writeObjectB(int fieldCount, Object obj);
|
||||
public void writeObjectB(Object obj) {
|
||||
this.comma = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个为null的对象
|
||||
*
|
||||
* @param clazz 对象的类名
|
||||
*/
|
||||
public final void writeObjectNull(final Class clazz) {
|
||||
wirteClassName(null);
|
||||
writeNull();
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个对象的某个字段
|
||||
*
|
||||
* @param member 字段
|
||||
*
|
||||
* @param obj 写入的对象
|
||||
*/
|
||||
public final void writeObjectField(final EnMember member, Object obj) {
|
||||
Object value = member.attribute.get(obj);
|
||||
if (value == null) return;
|
||||
if (tiny()) {
|
||||
if (member.istring) {
|
||||
if (((CharSequence) value).length() == 0) return;
|
||||
} else if (member.isbool) {
|
||||
if (!((Boolean) value)) return;
|
||||
}
|
||||
}
|
||||
this.writeFieldName(member.attribute);
|
||||
member.encoder.convertTo(this, value);
|
||||
this.comma = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出一个对象后的操作
|
||||
*
|
||||
* @param obj 写入的对象
|
||||
*/
|
||||
public void writeObjectE(Object obj);
|
||||
public abstract void writeObjectE(Object obj);
|
||||
|
||||
/**
|
||||
* 输出一个数组前的操作
|
||||
*
|
||||
* @param size 数组长度
|
||||
*/
|
||||
public void writeArrayB(int size);
|
||||
public abstract void writeArrayB(int size);
|
||||
|
||||
/**
|
||||
* 输出数组元素间的间隔符
|
||||
*
|
||||
*/
|
||||
public void writeArrayMark();
|
||||
public abstract void writeArrayMark();
|
||||
|
||||
/**
|
||||
* 输出一个数组后的操作
|
||||
*
|
||||
*/
|
||||
public void writeArrayE();
|
||||
public abstract void writeArrayE();
|
||||
|
||||
/**
|
||||
* 输出一个Map前的操作
|
||||
*
|
||||
* @param size map大小
|
||||
*/
|
||||
public void writeMapB(int size);
|
||||
public abstract void writeMapB(int size);
|
||||
|
||||
/**
|
||||
* 输出一个Map中key与value间的间隔符
|
||||
*
|
||||
*/
|
||||
public void writeMapMark();
|
||||
public abstract void writeMapMark();
|
||||
|
||||
/**
|
||||
* 输出一个Map后的操作
|
||||
*
|
||||
*/
|
||||
public void writeMapE();
|
||||
public abstract void writeMapE();
|
||||
|
||||
/**
|
||||
* 输出一个字段
|
||||
* 输出一个字段名
|
||||
*
|
||||
* @param comma 是否非第一个字段
|
||||
* @param attribute 字段的Attribute对象
|
||||
*/
|
||||
public void writeField(boolean comma, Attribute attribute);
|
||||
public abstract void writeFieldName(Attribute attribute);
|
||||
|
||||
/**
|
||||
* 写入一个boolean值
|
||||
*
|
||||
* @param value boolean值
|
||||
*/
|
||||
public void writeBoolean(boolean value);
|
||||
public abstract void writeBoolean(boolean value);
|
||||
|
||||
/**
|
||||
* 写入一个byte值
|
||||
*
|
||||
* @param value byte值
|
||||
*/
|
||||
public void writeByte(byte value);
|
||||
public abstract void writeByte(byte value);
|
||||
|
||||
/**
|
||||
* 写入一个char值
|
||||
*
|
||||
* @param value char值
|
||||
*/
|
||||
public void writeChar(char value);
|
||||
public abstract void writeChar(char value);
|
||||
|
||||
/**
|
||||
* 写入一个short值
|
||||
*
|
||||
* @param value short值
|
||||
*/
|
||||
public void writeShort(short value);
|
||||
public abstract void writeShort(short value);
|
||||
|
||||
/**
|
||||
* 写入一个int值
|
||||
*
|
||||
* @param value int值
|
||||
*/
|
||||
public void writeInt(int value);
|
||||
public abstract void writeInt(int value);
|
||||
|
||||
/**
|
||||
* 写入一个long值
|
||||
*
|
||||
* @param value long值
|
||||
*/
|
||||
public void writeLong(long value);
|
||||
public abstract void writeLong(long value);
|
||||
|
||||
/**
|
||||
* 写入一个float值
|
||||
*
|
||||
* @param value float值
|
||||
*/
|
||||
public void writeFloat(float value);
|
||||
public abstract void writeFloat(float value);
|
||||
|
||||
/**
|
||||
* 写入一个double值
|
||||
*
|
||||
* @param value double值
|
||||
*/
|
||||
public void writeDouble(double value);
|
||||
public abstract void writeDouble(double value);
|
||||
|
||||
/**
|
||||
* 写入无转义字符长度不超过255的字符串, 例如枚举值、字段名、类名字符串等 *
|
||||
*
|
||||
* @param value 非空且不含需要转义的字符的String值
|
||||
*/
|
||||
public void writeSmallString(String value);
|
||||
public abstract void writeSmallString(String value);
|
||||
|
||||
/**
|
||||
* 写入一个String值
|
||||
*
|
||||
* @param value String值
|
||||
*/
|
||||
public void writeString(String value);
|
||||
public abstract void writeString(String value);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
package org.redkale.convert.bson;
|
||||
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.function.*;
|
||||
import org.redkale.convert.*;
|
||||
import static org.redkale.convert.Reader.SIGN_NULL;
|
||||
@@ -19,7 +18,7 @@ import org.redkale.util.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class BsonReader implements Reader {
|
||||
public class BsonReader extends Reader {
|
||||
|
||||
public static final short SIGN_OBJECTB = (short) 0xBB;
|
||||
|
||||
@@ -156,19 +155,18 @@ public class BsonReader implements Reader {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断下一个非空白字节是否为{
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public final int readObjectB() {
|
||||
public final String readObjectB(final Class clazz) {
|
||||
this.fieldIndex = 0; //必须要重置为0
|
||||
final String newcls = readClassName();
|
||||
if (newcls != null && !newcls.isEmpty()) return newcls;
|
||||
short bt = readShort();
|
||||
if (bt == Reader.SIGN_NULL) return bt;
|
||||
if (bt == Reader.SIGN_NULL) return null;
|
||||
if (bt != SIGN_OBJECTB) {
|
||||
throw new ConvertException("a bson object must begin with " + (SIGN_OBJECTB)
|
||||
+ " (position = " + position + ") but '" + currentByte() + "'");
|
||||
}
|
||||
return bt;
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -230,24 +228,20 @@ public class BsonReader implements Reader {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final DeMember readField(final AtomicInteger index, final DeMember[] members) {
|
||||
public final DeMember readFieldName(final DeMember[] members) {
|
||||
final String exceptedfield = readSmallString();
|
||||
this.typeval = readByte();
|
||||
final int len = members.length;
|
||||
int v = index.get();
|
||||
if (v >= len) {
|
||||
v = 0;
|
||||
index.set(0);
|
||||
}
|
||||
for (int k = v; k < len; k++) {
|
||||
if (this.fieldIndex >= len) this.fieldIndex = 0;
|
||||
for (int k = this.fieldIndex; k < len; k++) {
|
||||
if (exceptedfield.equals(members[k].getAttribute().field())) {
|
||||
index.set(k);
|
||||
this.fieldIndex = k;
|
||||
return members[k];
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < v; k++) {
|
||||
for (int k = 0; k < this.fieldIndex; k++) {
|
||||
if (exceptedfield.equals(members[k].getAttribute().field())) {
|
||||
index.set(k);
|
||||
this.fieldIndex = k;
|
||||
return members[k];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,10 +12,12 @@ import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p> 详情见: http://www.redkale.org
|
||||
* <p>
|
||||
* 详情见: http://www.redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class BsonWriter implements Writer {
|
||||
public class BsonWriter extends Writer {
|
||||
|
||||
private static final int defaultSize = Integer.getInteger("convert.bson.writer.buffer.defsize", 1024);
|
||||
|
||||
@@ -171,7 +173,8 @@ public class BsonWriter implements Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeObjectB(int fieldCount, Object obj) {
|
||||
public final void writeObjectB(Object obj) {
|
||||
super.writeObjectB(obj);
|
||||
writeSmallString("");
|
||||
writeShort(BsonReader.SIGN_OBJECTB);
|
||||
}
|
||||
@@ -183,7 +186,7 @@ public class BsonWriter implements Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeField(boolean comma, Attribute attribute) {
|
||||
public final void writeFieldName( Attribute attribute) {
|
||||
writeByte(BsonReader.SIGN_HASNEXT);
|
||||
writeSmallString(attribute.field());
|
||||
byte typeval = 127; //字段的类型值
|
||||
|
||||
@@ -8,10 +8,12 @@ package org.redkale.convert.ext;
|
||||
import java.nio.channels.*;
|
||||
import org.redkale.convert.*;
|
||||
|
||||
/**
|
||||
/**
|
||||
* java.nio.channels.CompletionHandler 的SimpledCoder实现, 只输出null
|
||||
*
|
||||
* <p> 详情见: http://www.redkale.org
|
||||
*
|
||||
* <p>
|
||||
* 详情见: http://www.redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <R> Reader输入的子类型
|
||||
* @param <W> Writer输出的子类型
|
||||
@@ -22,12 +24,12 @@ public final class CompletionHandlerSimpledCoder<R extends Reader, W extends Wri
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, CompletionHandler value) {
|
||||
out.writeNull();
|
||||
out.writeObjectNull(CompletionHandler.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionHandler convertFrom(R in) {
|
||||
in.readObjectB();
|
||||
in.readObjectB(CompletionHandler.class);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -113,11 +113,11 @@ public class JsonByteBufferReader extends JsonReader {
|
||||
* @return SIGN_NOLENGTH 或 SIGN_NULL
|
||||
*/
|
||||
@Override
|
||||
public final int readObjectB() {
|
||||
public final String readObjectB(final Class clazz) {
|
||||
char ch = nextGoodChar();
|
||||
if (ch == '{') return SIGN_NOLENGTH;
|
||||
if (ch == 'n' && nextChar() == 'u' && nextChar() == 'l' && nextChar() == 'l') return SIGN_NULL;
|
||||
if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') return SIGN_NULL;
|
||||
if (ch == '{') return "";
|
||||
if (ch == 'n' && nextChar() == 'u' && nextChar() == 'l' && nextChar() == 'l') return null;
|
||||
if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') return null;
|
||||
throw new ConvertException("a json object text must begin with '{' (position = " + position + ") but '" + ch + "'");
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
*/
|
||||
package org.redkale.convert.json;
|
||||
|
||||
import java.util.concurrent.atomic.*;
|
||||
import java.util.function.*;
|
||||
import org.redkale.convert.*;
|
||||
import static org.redkale.convert.Reader.*;
|
||||
@@ -18,7 +17,7 @@ import org.redkale.util.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class JsonReader implements Reader {
|
||||
public class JsonReader extends Reader {
|
||||
|
||||
protected int position = -1;
|
||||
|
||||
@@ -169,21 +168,23 @@ public class JsonReader implements Reader {
|
||||
/**
|
||||
* 判断下一个非空白字符是否为{
|
||||
*
|
||||
* @return SIGN_NOLENGTH 或 SIGN_NULL
|
||||
* @param clazz 类名
|
||||
* @return 返回 null 表示对象为null, 返回空字符串表示当前class与返回的class一致,返回非空字符串表示class是当前class的子类。
|
||||
*/
|
||||
@Override
|
||||
public int readObjectB() {
|
||||
public String readObjectB(final Class clazz) {
|
||||
this.fieldIndex = 0; //必须要重置为0
|
||||
char ch = this.text[++this.position];
|
||||
if (ch == '{') return SIGN_NOLENGTH;
|
||||
if (ch == '{') return "";
|
||||
if (ch <= ' ') {
|
||||
for (;;) {
|
||||
ch = this.text[++this.position];
|
||||
if (ch > ' ') break;
|
||||
}
|
||||
if (ch == '{') return SIGN_NOLENGTH;
|
||||
if (ch == '{') return "";
|
||||
}
|
||||
if (ch == 'n' && text[++position] == 'u' && text[++position] == 'l' && text[++position] == 'l') return SIGN_NULL;
|
||||
if (ch == 'N' && text[++position] == 'U' && text[++position] == 'L' && text[++position] == 'L') return SIGN_NULL;
|
||||
if (ch == 'n' && text[++position] == 'u' && text[++position] == 'l' && text[++position] == 'l') return null;
|
||||
if (ch == 'N' && text[++position] == 'U' && text[++position] == 'L' && text[++position] == 'L') return null;
|
||||
throw new ConvertException("a json object text must begin with '{' (position = " + position + ") but '" + ch + "' in (" + new String(this.text) + ")");
|
||||
}
|
||||
|
||||
@@ -416,23 +417,19 @@ public class JsonReader implements Reader {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final DeMember readField(final AtomicInteger index, final DeMember[] members) {
|
||||
public final DeMember readFieldName(final DeMember[] members) {
|
||||
final String exceptedfield = this.readSmallString();
|
||||
final int len = members.length;
|
||||
int v = index.get();
|
||||
if (v >= len) {
|
||||
v = 0;
|
||||
index.set(0);
|
||||
}
|
||||
for (int k = v; k < len; k++) {
|
||||
if (this.fieldIndex >= len) this.fieldIndex = 0;
|
||||
for (int k = this.fieldIndex; k < len; k++) {
|
||||
if (exceptedfield.equals(members[k].getAttribute().field())) {
|
||||
index.set(k);
|
||||
this.fieldIndex = k;
|
||||
return members[k];
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < v; k++) {
|
||||
for (int k = 0; k < this.fieldIndex; k++) {
|
||||
if (exceptedfield.equals(members[k].getAttribute().field())) {
|
||||
index.set(k);
|
||||
this.fieldIndex = k;
|
||||
return members[k];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ import org.redkale.util.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class JsonWriter implements Writer {
|
||||
public class JsonWriter extends Writer {
|
||||
|
||||
private static final char[] CHARS_TUREVALUE = "true".toCharArray();
|
||||
|
||||
@@ -159,8 +159,8 @@ public class JsonWriter implements Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeField(boolean comma, Attribute attribute) {
|
||||
if (comma) writeTo(',');
|
||||
public final void writeFieldName(Attribute attribute) {
|
||||
if (this.comma) writeTo(',');
|
||||
writeTo(true, attribute.field());
|
||||
writeTo(':');
|
||||
}
|
||||
@@ -225,7 +225,8 @@ public class JsonWriter implements Writer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void writeObjectB(int fieldCount, Object obj) {
|
||||
public final void writeObjectB(Object obj) {
|
||||
super.writeObjectB(obj);
|
||||
writeTo('{');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user