This commit is contained in:
@@ -82,7 +82,7 @@ public final class ArrayEncoder<T> implements Encodeable<Writer, T[]> {
|
||||
boolean first = true;
|
||||
for (Object v : value) {
|
||||
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;
|
||||
}
|
||||
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();
|
||||
if (out.needWriteClassName()) out.writeClassName(factory.getEntityAlias(clz));
|
||||
factory.loadEncoder(clz).convertTo(out, value);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import org.redkale.util.Attribute;
|
||||
|
||||
/**
|
||||
@@ -20,6 +21,33 @@ public abstract class Writer {
|
||||
//当前对象输出字段名之前是否需要分隔符, JSON字段间的分隔符为,逗号
|
||||
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的字段值都会被跳过, 不会输出。
|
||||
*
|
||||
|
||||
@@ -132,6 +132,7 @@ public class BsonByteBufferWriter extends BsonWriter {
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
this.index = 0;
|
||||
this.specify = null;
|
||||
this.buffers = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ public class BsonWriter extends Writer {
|
||||
|
||||
protected boolean recycle() {
|
||||
this.count = 0;
|
||||
this.specify = null;
|
||||
if (this.content.length > defaultSize) {
|
||||
this.content = new byte[defaultSize];
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ public class JsonByteBufferWriter extends JsonWriter {
|
||||
@Override
|
||||
protected boolean recycle() {
|
||||
this.index = 0;
|
||||
this.specify = null;
|
||||
this.charset = null;
|
||||
this.buffers = null;
|
||||
return false;
|
||||
|
||||
@@ -80,8 +80,8 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
return writerPool.get().tiny(tiny);
|
||||
}
|
||||
|
||||
public void offerJsonWriter(final JsonWriter out) {
|
||||
if (out != null) writerPool.accept(out);
|
||||
public void offerJsonWriter(final JsonWriter writer) {
|
||||
if (writer != null) writerPool.accept(writer);
|
||||
}
|
||||
|
||||
//------------------------------ convertFrom -----------------------------------------------------------
|
||||
@@ -139,20 +139,21 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
public String convertTo(final Type type, final Object value) {
|
||||
if (type == null) return null;
|
||||
if (value == null) return "null";
|
||||
final JsonWriter out = writerPool.get().tiny(tiny);
|
||||
factory.loadEncoder(type).convertTo(out, value);
|
||||
String result = out.toString();
|
||||
writerPool.accept(out);
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
String result = writer.toString();
|
||||
writerPool.accept(writer);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertMapTo(final Object... values) {
|
||||
if (values == null) return "null";
|
||||
final JsonWriter out = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
||||
String result = out.toString();
|
||||
writerPool.accept(out);
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
String result = writer.toString();
|
||||
writerPool.accept(writer);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -170,6 +171,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
new JsonStreamWriter(tiny, out).writeNull();
|
||||
} else {
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
byte[] bs = writer.toBytes();
|
||||
writerPool.accept(writer);
|
||||
@@ -216,6 +218,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
out.specify(type);
|
||||
factory.loadEncoder(type).convertTo(out, value);
|
||||
}
|
||||
return out.toBuffers();
|
||||
@@ -246,6 +249,7 @@ public final class JsonConvert extends TextConvert<JsonReader, JsonWriter> {
|
||||
if (value == null) {
|
||||
writer.writeNull();
|
||||
} else {
|
||||
writer.specify(type);
|
||||
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) {
|
||||
if (type == null) return null;
|
||||
final JsonWriter out = writerPool.get().tiny(tiny);
|
||||
factory.loadEncoder(type).convertTo(out, value);
|
||||
return out;
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
writer.specify(type);
|
||||
factory.loadEncoder(type).convertTo(writer, value);
|
||||
return writer;
|
||||
}
|
||||
|
||||
public JsonWriter convertMapToWriter(final Object... values) {
|
||||
final JsonWriter out = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(out, values);
|
||||
return out;
|
||||
final JsonWriter writer = writerPool.get().tiny(tiny);
|
||||
((AnyEncoder) factory.getAnyEncoder()).convertMapTo(writer, values);
|
||||
return writer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,6 +99,7 @@ public class JsonWriter extends Writer {
|
||||
|
||||
protected boolean recycle() {
|
||||
this.count = 0;
|
||||
this.specify = null;
|
||||
if (this.content.length > defaultSize) {
|
||||
this.content = new char[defaultSize];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user