This commit is contained in:
@@ -82,7 +82,7 @@ public final class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
|||||||
boolean first = true;
|
boolean first = true;
|
||||||
for (Object v : value) {
|
for (Object v : value) {
|
||||||
if (!first) out.writeArrayMark();
|
if (!first) out.writeArrayMark();
|
||||||
((v != null && v.getClass() == comp) ? encoder : anyEncoder).convertTo(out, v);
|
((v != null && (v.getClass() == comp || out.specify() == comp)) ? encoder : anyEncoder).convertTo(out, v);
|
||||||
if (first) first = false;
|
if (first) first = false;
|
||||||
}
|
}
|
||||||
out.writeArrayE();
|
out.writeArrayE();
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ public final class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (value.getClass() != this.typeClass) {
|
if (value.getClass() != this.typeClass && !this.type.equals(out.specify())) {
|
||||||
final Class clz = value.getClass();
|
final Class clz = value.getClass();
|
||||||
if (out.needWriteClassName()) out.writeClassName(factory.getEntityAlias(clz));
|
if (out.needWriteClassName()) out.writeClassName(factory.getEntityAlias(clz));
|
||||||
factory.loadEncoder(clz).convertTo(out, value);
|
factory.loadEncoder(clz).convertTo(out, value);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.convert;
|
package org.redkale.convert;
|
||||||
|
|
||||||
|
import java.lang.reflect.*;
|
||||||
import org.redkale.util.Attribute;
|
import org.redkale.util.Attribute;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,6 +21,33 @@ public abstract class Writer {
|
|||||||
//当前对象输出字段名之前是否需要分隔符, JSON字段间的分隔符为,逗号
|
//当前对象输出字段名之前是否需要分隔符, JSON字段间的分隔符为,逗号
|
||||||
protected boolean comma;
|
protected boolean comma;
|
||||||
|
|
||||||
|
//convertTo时是否以指定Type的ObjectEncoder进行处理
|
||||||
|
protected Type specify;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置specify
|
||||||
|
*
|
||||||
|
* @param value Type
|
||||||
|
*/
|
||||||
|
public void specify(Type value) {
|
||||||
|
if (value instanceof GenericArrayType) {
|
||||||
|
this.specify = ((GenericArrayType) value).getGenericComponentType();
|
||||||
|
} else if (value instanceof Class && ((Class) value).isArray()) {
|
||||||
|
this.specify = ((Class) value).getComponentType();
|
||||||
|
} else {
|
||||||
|
this.specify = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回specify
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public Type specify() {
|
||||||
|
return this.specify;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 当tiny=true时, 字符串为空、boolean为false的字段值都会被跳过, 不会输出。
|
* 当tiny=true时, 字符串为空、boolean为false的字段值都会被跳过, 不会输出。
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -132,6 +132,7 @@ public class BsonByteBufferWriter extends BsonWriter {
|
|||||||
@Override
|
@Override
|
||||||
protected boolean recycle() {
|
protected boolean recycle() {
|
||||||
this.index = 0;
|
this.index = 0;
|
||||||
|
this.specify = null;
|
||||||
this.buffers = null;
|
this.buffers = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ public class BsonWriter extends Writer {
|
|||||||
|
|
||||||
protected boolean recycle() {
|
protected boolean recycle() {
|
||||||
this.count = 0;
|
this.count = 0;
|
||||||
|
this.specify = null;
|
||||||
if (this.content.length > defaultSize) {
|
if (this.content.length > defaultSize) {
|
||||||
this.content = new byte[defaultSize];
|
this.content = new byte[defaultSize];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ public class JsonByteBufferWriter extends JsonWriter {
|
|||||||
@Override
|
@Override
|
||||||
protected boolean recycle() {
|
protected boolean recycle() {
|
||||||
this.index = 0;
|
this.index = 0;
|
||||||
|
this.specify = null;
|
||||||
this.charset = null;
|
this.charset = null;
|
||||||
this.buffers = null;
|
this.buffers = null;
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -80,8 +80,8 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
return writerPool.get().tiny(tiny);
|
return writerPool.get().tiny(tiny);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void offerJsonWriter(final JsonWriter out) {
|
public void offerJsonWriter(final JsonWriter writer) {
|
||||||
if (out != null) writerPool.accept(out);
|
if (writer != null) writerPool.accept(writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
//------------------------------ convertFrom -----------------------------------------------------------
|
//------------------------------ convertFrom -----------------------------------------------------------
|
||||||
@@ -139,20 +139,21 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
public String convertTo(final Type type, final Object value) {
|
public String convertTo(final Type type, final Object value) {
|
||||||
if (type == null) return null;
|
if (type == null) return null;
|
||||||
if (value == null) return "null";
|
if (value == null) return "null";
|
||||||
final JsonWriter out = writerPool.get().tiny(tiny);
|
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||||
factory.loadEncoder(type).convertTo(out, value);
|
writer.specify(type);
|
||||||
String result = out.toString();
|
factory.loadEncoder(type).convertTo(writer, value);
|
||||||
writerPool.accept(out);
|
String result = writer.toString();
|
||||||
|
writerPool.accept(writer);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String convertMapTo(final Object... values) {
|
public String convertMapTo(final Object... values) {
|
||||||
if (values == null) return "null";
|
if (values == null) return "null";
|
||||||
final JsonWriter out = writerPool.get().tiny(tiny);
|
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||||
String result = out.toString();
|
String result = writer.toString();
|
||||||
writerPool.accept(out);
|
writerPool.accept(writer);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,6 +171,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
new JsonStreamWriter(tiny, out).writeNull();
|
new JsonStreamWriter(tiny, out).writeNull();
|
||||||
} else {
|
} else {
|
||||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||||
|
writer.specify(type);
|
||||||
factory.loadEncoder(type).convertTo(writer, value);
|
factory.loadEncoder(type).convertTo(writer, value);
|
||||||
byte[] bs = writer.toBytes();
|
byte[] bs = writer.toBytes();
|
||||||
writerPool.accept(writer);
|
writerPool.accept(writer);
|
||||||
@@ -216,6 +218,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
if (value == null) {
|
if (value == null) {
|
||||||
out.writeNull();
|
out.writeNull();
|
||||||
} else {
|
} else {
|
||||||
|
out.specify(type);
|
||||||
factory.loadEncoder(type).convertTo(out, value);
|
factory.loadEncoder(type).convertTo(out, value);
|
||||||
}
|
}
|
||||||
return out.toBuffers();
|
return out.toBuffers();
|
||||||
@@ -246,6 +249,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
if (value == null) {
|
if (value == null) {
|
||||||
writer.writeNull();
|
writer.writeNull();
|
||||||
} else {
|
} else {
|
||||||
|
writer.specify(type);
|
||||||
factory.loadEncoder(type).convertTo(writer, value);
|
factory.loadEncoder(type).convertTo(writer, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -265,14 +269,15 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
|||||||
|
|
||||||
public JsonWriter convertToWriter(final Type type, final Object value) {
|
public JsonWriter convertToWriter(final Type type, final Object value) {
|
||||||
if (type == null) return null;
|
if (type == null) return null;
|
||||||
final JsonWriter out = writerPool.get().tiny(tiny);
|
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||||
factory.loadEncoder(type).convertTo(out, value);
|
writer.specify(type);
|
||||||
return out;
|
factory.loadEncoder(type).convertTo(writer, value);
|
||||||
|
return writer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonWriter convertMapToWriter(final Object... values) {
|
public JsonWriter convertMapToWriter(final Object... values) {
|
||||||
final JsonWriter out = writerPool.get().tiny(tiny);
|
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||||
return out;
|
return writer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ public class JsonWriter extends Writer {
|
|||||||
|
|
||||||
protected boolean recycle() {
|
protected boolean recycle() {
|
||||||
this.count = 0;
|
this.count = 0;
|
||||||
|
this.specify = null;
|
||||||
if (this.content.length > defaultSize) {
|
if (this.content.length > defaultSize) {
|
||||||
this.content = new char[defaultSize];
|
this.content = new char[defaultSize];
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user