This commit is contained in:
地平线
2015-05-29 09:21:30 +08:00
parent a365d7c59c
commit f3ff45d8f5
9 changed files with 91 additions and 14 deletions

View File

@@ -21,14 +21,32 @@ public final class EnMember<W extends Writer, T, F> implements Comparable<EnMemb
final Encodeable<W, F> encoder;
private final boolean istring;
private final boolean isnumber;
private final boolean isbool;
public EnMember(Attribute<T, F> attribute, Encodeable<W, F> encoder) {
this.attribute = attribute;
this.encoder = encoder;
this.istring = CharSequence.class.isAssignableFrom(attribute.type());
this.isnumber = Number.class.isAssignableFrom(attribute.type()) || attribute.type().isPrimitive();
this.isbool = attribute.type() == Boolean.class || attribute.type() == boolean.class;
}
public boolean write(final W out, final boolean comma, final T obj) {
F value = attribute.get(obj);
if (value == null) return comma;
if (out.isTiny()) {
if (isnumber) {
if (((Number) value).intValue() == 0) return comma;
} else 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;

View File

@@ -29,6 +29,8 @@ public abstract class Factory<R extends Reader, W extends Writer> {
protected Convert<R, W> convert;
protected boolean tiny;
private final Encodeable<W, ?> anyEncoder = new AnyEncoder(this);
//-----------------------------------------------------------------------------------
@@ -44,7 +46,8 @@ public abstract class Factory<R extends Reader, W extends Writer> {
private boolean skipAllIgnore = false;
protected Factory(Factory<R, W> parent) {
protected Factory(Factory<R, W> parent, boolean tiny) {
this.tiny = tiny;
this.parent = parent;
if (parent == null) {
//---------------------------------------------------------
@@ -105,10 +108,16 @@ public abstract class Factory<R extends Reader, W extends Writer> {
public abstract Factory createChild();
public abstract Factory createChild(boolean tiny);
public Convert getConvert() {
return convert;
}
public void setTiny(boolean tiny) {
this.tiny = tiny;
}
public ConvertColumnEntry findRef(AccessibleObject field) {
if (field == null) return null;
ConvertColumnEntry en = this.columnEntrys.get(field);

View File

@@ -13,6 +13,13 @@ import com.wentch.redkale.util.Attribute;
*/
public interface Writer {
/**
* 当tiny=true时 字符串为空、数字为0、boolean为false的字段值都会被跳过 不会输出。
* <p>
* @return
*/
public boolean isTiny();
/**
* 输出null值
*/
@@ -81,7 +88,7 @@ public interface Writer {
/**
* 输出一个字段
*
* @param comma 是否非第一个字段
* @param comma 是否非第一个字段
* @param attribute
*/
public void writeField(boolean comma, Attribute attribute);

View File

@@ -19,8 +19,11 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
private static final ObjectPool<BsonWriter> writerPool = BsonWriter.createPool(Integer.getInteger("convert.bson.pool.size", 16));
protected BsonConvert(Factory<BsonReader, BsonWriter> factory) {
private final boolean tiny;
protected BsonConvert(Factory<BsonReader, BsonWriter> factory, boolean tiny) {
super(factory);
this.tiny = tiny;
}
public <T> T convertFrom(final Type type, final byte[] bytes) {
@@ -41,6 +44,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
public byte[] convertTo(final Type type, Object value) {
if (type == null) return null;
final BsonWriter out = writerPool.poll();
out.setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value);
byte[] result = out.toArray();
writerPool.offer(out);
@@ -50,6 +54,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
public byte[] convertTo(Object value) {
if (value == null) {
final BsonWriter out = writerPool.poll();
out.setTiny(tiny);
out.writeNull();
byte[] result = out.toArray();
writerPool.offer(out);

View File

@@ -15,16 +15,15 @@ import java.io.Serializable;
*/
public final class BsonFactory extends Factory<BsonReader, BsonWriter> {
private static final BsonFactory instance = new BsonFactory(null);
private static final BsonFactory instance = new BsonFactory(null, Boolean.getBoolean("convert.bson.tiny"));
static {
instance.register(Serializable.class, instance.loadDecoder(Object.class));
instance.register(Serializable.class, instance.loadEncoder(Object.class));
}
private BsonFactory(BsonFactory parent) {
super(parent);
this.convert = new BsonConvert(this);
private BsonFactory(BsonFactory parent, boolean tiny) {
super(parent, tiny);
}
public static BsonFactory root() {
@@ -33,12 +32,18 @@ public final class BsonFactory extends Factory<BsonReader, BsonWriter> {
@Override
public final BsonConvert getConvert() {
if (convert == null) convert = new BsonConvert(this, tiny);
return (BsonConvert) convert;
}
@Override
public BsonFactory createChild() {
return new BsonFactory(this);
return new BsonFactory(this, this.tiny);
}
@Override
public BsonFactory createChild(boolean tiny) {
return new BsonFactory(this, tiny);
}
@Override

View File

@@ -20,6 +20,8 @@ public final class BsonWriter implements Writer {
private byte[] content;
protected boolean tiny;
public static ObjectPool<BsonWriter> createPool(int max) {
return new ObjectPool<>(max, (Object... params) -> new BsonWriter(), null, (x) -> x.recycle());
}
@@ -39,6 +41,15 @@ public final class BsonWriter implements Writer {
this.content = new byte[size > 32 ? size : 32];
}
@Override
public boolean isTiny() {
return tiny;
}
public void setTiny(boolean tiny) {
this.tiny = tiny;
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
/**

View File

@@ -20,8 +20,11 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
private static final ObjectPool<JsonWriter> writerPool = JsonWriter.createPool(Integer.getInteger("convert.json.pool.size", 16));
protected JsonConvert(JsonFactory factory) {
private final boolean tiny;
protected JsonConvert(JsonFactory factory, boolean tiny) {
super(factory);
this.tiny = tiny;
}
@Override
@@ -52,6 +55,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
if (type == null) return null;
if (value == null) return "null";
final JsonWriter out = writerPool.poll();
out.setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value);
String result = out.toString();
writerPool.offer(out);
@@ -72,6 +76,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
if (type == null) return null;
if (value == null) return new byte[]{110, 117, 108, 108};
final JsonWriter out = writerPool.poll();
out.setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value);
byte[] result = out.toUTF8Bytes();
writerPool.offer(out);

View File

@@ -15,15 +15,14 @@ import java.io.Serializable;
*/
public final class JsonFactory extends Factory<JsonReader, JsonWriter> {
private static final JsonFactory instance = new JsonFactory(null);
private static final JsonFactory instance = new JsonFactory(null, Boolean.getBoolean("convert.json.tiny"));
static {
instance.register(Serializable.class, instance.loadEncoder(Object.class));
}
private JsonFactory(JsonFactory parent) {
super(parent);
this.convert = new JsonConvert(this);
private JsonFactory(JsonFactory parent, boolean tiny) {
super(parent, tiny);
}
public static JsonFactory root() {
@@ -32,12 +31,18 @@ public final class JsonFactory extends Factory<JsonReader, JsonWriter> {
@Override
public final JsonConvert getConvert() {
if (convert == null) convert = new JsonConvert(this, tiny);
return (JsonConvert) convert;
}
@Override
public JsonFactory createChild() {
return new JsonFactory(this);
return new JsonFactory(this, this.tiny);
}
@Override
public JsonFactory createChild(boolean tiny) {
return new JsonFactory(this, tiny);
}
@Override

View File

@@ -26,6 +26,8 @@ public final class JsonWriter implements Writer {
private char[] content;
protected boolean tiny;
public static ObjectPool<JsonWriter> createPool(int max) {
return new ObjectPool<>(max, (Object... params) -> new JsonWriter(), null, (x) -> x.recycle());
}
@@ -38,8 +40,18 @@ public final class JsonWriter implements Writer {
this.content = new char[size > 128 ? size : 128];
}
@Override
public boolean isTiny() {
return tiny;
}
public void setTiny(boolean tiny) {
this.tiny = tiny;
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
/**
* 返回指定至少指定长度的缓冲区
*