anyvalue
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
/**
|
||||
* AnyValue的Decoder实现
|
||||
*
|
||||
* <p>详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <R> Reader输入的子类型
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public class AnyValueDecoder<R extends Reader> implements Decodeable<R, AnyValue> {
|
||||
|
||||
protected final ConvertFactory factory;
|
||||
|
||||
public AnyValueDecoder(final ConvertFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AnyValue convertFrom(R in) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return AnyValue.class;
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.util.AnyValue;
|
||||
|
||||
/**
|
||||
* AnyValue的Encoder实现
|
||||
*
|
||||
* <p>详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <W> Writer输出的子类
|
||||
* @since 2.5.0
|
||||
*/
|
||||
public class AnyValueEncoder<W extends Writer> implements Encodeable<W, AnyValue> {
|
||||
|
||||
@Override
|
||||
public void convertTo(W out, AnyValue value) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return AnyValue.class;
|
||||
}
|
||||
}
|
||||
@@ -78,12 +78,18 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
|
||||
private Consumer<BiFunction> fieldFuncConsumer;
|
||||
|
||||
@SuppressWarnings("OverridableMethodCallInConstructor")
|
||||
protected ConvertFactory(ConvertFactory<R, W> parent, int features) {
|
||||
this.features = features;
|
||||
this.parent = parent;
|
||||
if (parent == null) {
|
||||
// ---------------------------------------------------------
|
||||
this.initPrimitiveCoderInRoot();
|
||||
this.initSimpleCoderInRoot();
|
||||
this.initObjectCoderInRoot();
|
||||
}
|
||||
}
|
||||
|
||||
protected void initPrimitiveCoderInRoot() {
|
||||
this.register(boolean.class, BoolSimpledCoder.instance);
|
||||
this.register(Boolean.class, BoolSimpledCoder.instance);
|
||||
|
||||
@@ -110,6 +116,9 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
|
||||
this.register(Number.class, NumberSimpledCoder.instance);
|
||||
this.register(String.class, StringSimpledCoder.instance);
|
||||
}
|
||||
|
||||
protected void initSimpleCoderInRoot() {
|
||||
this.register(StringWrapper.class, StringWrapperSimpledCoder.instance);
|
||||
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
|
||||
this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance);
|
||||
@@ -150,12 +159,22 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
this.register(DoubleStream.class, DoubleArraySimpledCoder.DoubleStreamSimpledCoder.instance);
|
||||
this.register(String[].class, StringArraySimpledCoder.instance);
|
||||
// ---------------------------------------------------------
|
||||
}
|
||||
|
||||
protected void initObjectCoderInRoot() {
|
||||
this.register(AnyValue.class, Creator.create(AnyValueWriter.class));
|
||||
this.register(
|
||||
HttpCookie.class, (Object... params) -> new HttpCookie((String) params[0], (String) params[1]));
|
||||
ObjectDecoder anyValueDecoder = createObjectDecoder(AnyValueWriter.class);
|
||||
ObjectEncoder anyValueEncoder = createObjectEncoder(AnyValueWriter.class);
|
||||
this.register(AnyValue.class, anyValueDecoder);
|
||||
this.register(AnyValue.class, anyValueEncoder);
|
||||
this.register(AnyValueWriter.class, anyValueDecoder);
|
||||
this.register(AnyValueWriter.class, anyValueEncoder);
|
||||
anyValueDecoder.init(this);
|
||||
anyValueEncoder.init(this);
|
||||
|
||||
this.register(HttpCookie.class, (Object... params) -> new HttpCookie((String) params[0], (String) params[1]));
|
||||
try {
|
||||
Class sqldateClass =
|
||||
Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date");
|
||||
Class sqldateClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date");
|
||||
Invoker<Object, Object> sqldateInvoker = Invoker.create(sqldateClass, "valueOf", String.class);
|
||||
this.register(sqldateClass, new SimpledCoder<R, W, Object>() {
|
||||
|
||||
@@ -170,8 +189,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
return t == null ? null : sqldateInvoker.invoke(null, t);
|
||||
}
|
||||
});
|
||||
Class sqltimeClass =
|
||||
Thread.currentThread().getContextClassLoader().loadClass("java.sql.Time");
|
||||
Class sqltimeClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Time");
|
||||
Invoker<Object, Object> sqltimeInvoker = Invoker.create(sqltimeClass, "valueOf", String.class);
|
||||
this.register(sqltimeClass, new SimpledCoder<R, W, Object>() {
|
||||
|
||||
@@ -206,7 +224,6 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public ConvertFactory parent() {
|
||||
return this.parent;
|
||||
|
||||
@@ -33,14 +33,15 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
|
||||
|
||||
static {
|
||||
instance.register(Serializable.class, instance.loadEncoder(Object.class));
|
||||
|
||||
// instance.register(AnyValue.class, instance.loadDecoder(SimpleAnyValue.class));
|
||||
// instance.register(AnyValue.class, instance.loadEncoder(SimpleAnyValue.class));
|
||||
}
|
||||
|
||||
private JsonFactory(JsonFactory parent, int features) {
|
||||
super(parent, features);
|
||||
if (parent == null) {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initSimpleCoderInRoot() {
|
||||
super.initSimpleCoderInRoot();
|
||||
this.register(InetAddress.class, JsonCoders.InetAddressJsonSimpledCoder.instance);
|
||||
this.register(InetSocketAddress.class, JsonCoders.InetSocketAddressJsonSimpledCoder.instance);
|
||||
this.register(Uint128.class, JsonCoders.Uint128JsonSimpledCoder.instance);
|
||||
@@ -56,7 +57,6 @@ public final class JsonFactory extends ConvertFactory<JsonReader, JsonWriter> {
|
||||
this.register(JsonObject.class, (Decodeable) JsonElementDecoder.instance);
|
||||
this.register(JsonArray.class, (Decodeable) JsonElementDecoder.instance);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonFactory withFeatures(int features) {
|
||||
|
||||
@@ -372,9 +372,6 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
|
||||
if (!(type instanceof Class)) {
|
||||
return null;
|
||||
}
|
||||
if (AnyValue.class.isAssignableFrom((Class) type)) { // 不支持
|
||||
return null;
|
||||
}
|
||||
return generateDyncEncoder(factory, (Class) type);
|
||||
}
|
||||
|
||||
|
||||
@@ -42,9 +42,10 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
private ProtobufFactory(ProtobufFactory parent, int features, boolean enumtostring) {
|
||||
super(parent, features);
|
||||
this.enumtostring = enumtostring;
|
||||
if (parent == null) { // root
|
||||
// ---------------------------------------------------------
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initPrimitiveCoderInRoot() {
|
||||
this.register(boolean.class, ProtobufCoders.ProtobufBoolSimpledCoder.instance);
|
||||
this.register(Boolean.class, ProtobufCoders.ProtobufBoolSimpledCoder.instance);
|
||||
|
||||
@@ -71,6 +72,11 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
|
||||
this.register(Number.class, ProtobufCoders.ProtobufNumberSimpledCoder.instance);
|
||||
this.register(String.class, ProtobufCoders.ProtobufStringSimpledCoder.instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initSimpleCoderInRoot() {
|
||||
super.initSimpleCoderInRoot();
|
||||
this.register(StringWrapper.class, ProtobufCoders.ProtobufStringWrapperSimpledCoder.instance);
|
||||
this.register(CharSequence.class, ProtobufCoders.ProtobufCharSequenceSimpledCoder.instance);
|
||||
this.register(StringBuilder.class, ProtobufCoders.ProtobufStringBuilderSimpledCoder.instance);
|
||||
@@ -113,7 +119,6 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
this.register(String[].class, this.createArrayDecoder(String[].class));
|
||||
this.register(String[].class, this.createArrayEncoder(String[].class));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ConvertFactory rootFactory() {
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.util.*;
|
||||
import java.util.function.*;
|
||||
import org.redkale.annotation.ConstructorParameters;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.ConvertDisabled;
|
||||
import org.redkale.convert.json.JsonArray;
|
||||
import org.redkale.convert.json.JsonObject;
|
||||
import static org.redkale.util.Utility.isEmpty;
|
||||
@@ -98,7 +99,7 @@ public abstract class AnyValue {
|
||||
}
|
||||
|
||||
static Entry<AnyValue>[] getEntryAnyValueArray(
|
||||
BiPredicate<String, String> comparison, Entry<AnyValueWriter>[] entitys, String name) {
|
||||
BiPredicate<String, String> comparison, Entry<AnyValue>[] entitys, String name) {
|
||||
int len = 0;
|
||||
for (Entry en : entitys) {
|
||||
if (comparison.test(en.name, name)) {
|
||||
@@ -110,7 +111,7 @@ public abstract class AnyValue {
|
||||
}
|
||||
Entry[] rs = new Entry[len];
|
||||
int i = 0;
|
||||
for (Entry<AnyValueWriter> en : entitys) {
|
||||
for (Entry<AnyValue> en : entitys) {
|
||||
if (comparison.test(en.name, name)) {
|
||||
rs[i++] = en;
|
||||
}
|
||||
@@ -139,7 +140,7 @@ public abstract class AnyValue {
|
||||
}
|
||||
|
||||
static AnyValue[] getAnyValueArray(
|
||||
BiPredicate<String, String> comparison, Entry<AnyValueWriter>[] entitys, String name) {
|
||||
BiPredicate<String, String> comparison, Entry<AnyValue>[] entitys, String name) {
|
||||
int len = 0;
|
||||
for (Entry en : entitys) {
|
||||
if (comparison.test(en.name, name)) {
|
||||
@@ -151,7 +152,7 @@ public abstract class AnyValue {
|
||||
}
|
||||
AnyValue[] rs = new AnyValue[len];
|
||||
int i = 0;
|
||||
for (Entry<AnyValueWriter> en : entitys) {
|
||||
for (Entry<AnyValue> en : entitys) {
|
||||
if (comparison.test(en.name, name)) {
|
||||
rs[i++] = en.value;
|
||||
}
|
||||
@@ -187,7 +188,7 @@ public abstract class AnyValue {
|
||||
}
|
||||
|
||||
static AnyValue[] getAnyValueArray(
|
||||
BiPredicate<String, String> comparison, Entry<AnyValueWriter>[] entitys, String... names) {
|
||||
BiPredicate<String, String> comparison, Entry<AnyValue>[] entitys, String... names) {
|
||||
int len = 0;
|
||||
for (Entry en : entitys) {
|
||||
for (String name : names) {
|
||||
@@ -202,7 +203,7 @@ public abstract class AnyValue {
|
||||
}
|
||||
AnyValue[] rs = new AnyValue[len];
|
||||
int i = 0;
|
||||
for (Entry<AnyValueWriter> en : entitys) {
|
||||
for (Entry<AnyValue> en : entitys) {
|
||||
for (String name : names) {
|
||||
if (comparison.test(en.name, name)) {
|
||||
rs[i++] = en.value;
|
||||
@@ -663,6 +664,7 @@ public abstract class AnyValue {
|
||||
*
|
||||
* @return String[]
|
||||
*/
|
||||
@ConvertDisabled
|
||||
public abstract String[] getNames();
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,7 +37,7 @@ public class AnyValueWriter extends AnyValue {
|
||||
private Entry<String>[] stringEntrys = new Entry[0];
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
private Entry<AnyValueWriter>[] anyEntrys = new Entry[0];
|
||||
private Entry<AnyValue>[] anyEntrys = new Entry[0];
|
||||
|
||||
int parentArrayIndex = -1; // 只可能被loadFromProperties方法赋值
|
||||
|
||||
@@ -130,7 +130,7 @@ public class AnyValueWriter extends AnyValue {
|
||||
if (this.anyEntrys != null) {
|
||||
rs.anyEntrys = new Entry[this.anyEntrys.length];
|
||||
for (int i = 0; i < rs.anyEntrys.length; i++) {
|
||||
Entry<AnyValueWriter> en = this.anyEntrys[i];
|
||||
Entry<AnyValue> en = this.anyEntrys[i];
|
||||
if (en == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -188,7 +188,7 @@ public class AnyValueWriter extends AnyValue {
|
||||
}
|
||||
}
|
||||
if (node.anyEntrys != null) {
|
||||
for (Entry<AnyValueWriter> en : node.anyEntrys) {
|
||||
for (Entry<AnyValue> en : node.anyEntrys) {
|
||||
if (en == null || en.value == null) {
|
||||
continue;
|
||||
}
|
||||
@@ -202,7 +202,8 @@ public class AnyValueWriter extends AnyValue {
|
||||
continue;
|
||||
}
|
||||
if (item.value != null
|
||||
&& en.value.parentArrayIndex == ((AnyValueWriter) item.value).parentArrayIndex) {
|
||||
&& ((AnyValueWriter) en.value).parentArrayIndex
|
||||
== ((AnyValueWriter) item.value).parentArrayIndex) {
|
||||
if (func == null) {
|
||||
item.value.merge(en.value, func);
|
||||
ok = true;
|
||||
@@ -274,7 +275,7 @@ public class AnyValueWriter extends AnyValue {
|
||||
}
|
||||
}
|
||||
if (adv.anyEntrys != null) {
|
||||
for (Entry<AnyValueWriter> en : adv.anyEntrys) {
|
||||
for (Entry<AnyValue> en : adv.anyEntrys) {
|
||||
this.addValue(en.name, en.value);
|
||||
}
|
||||
}
|
||||
@@ -314,7 +315,7 @@ public class AnyValueWriter extends AnyValue {
|
||||
}
|
||||
}
|
||||
if (adv.anyEntrys != null) {
|
||||
for (Entry<AnyValueWriter> en : adv.anyEntrys) {
|
||||
for (Entry<AnyValue> en : adv.anyEntrys) {
|
||||
this.setValue(en.name, en.value);
|
||||
}
|
||||
}
|
||||
@@ -372,7 +373,7 @@ public class AnyValueWriter extends AnyValue {
|
||||
return (Entry<AnyValue>[]) (Entry[]) anyEntrys;
|
||||
}
|
||||
|
||||
public void setAnyEntrys(Entry<AnyValueWriter>[] anyEntrys) {
|
||||
public void setAnyEntrys(Entry<AnyValue>[] anyEntrys) {
|
||||
this.anyEntrys = anyEntrys;
|
||||
}
|
||||
|
||||
@@ -473,7 +474,7 @@ public class AnyValueWriter extends AnyValue {
|
||||
if (!existsValue(name)) {
|
||||
this.addValue(name, value);
|
||||
} else {
|
||||
for (Entry<AnyValueWriter> en : this.anyEntrys) {
|
||||
for (Entry<AnyValue> en : this.anyEntrys) {
|
||||
if (predicate.test(en.name, name)) {
|
||||
en.value = (AnyValueWriter) value;
|
||||
return this;
|
||||
@@ -571,7 +572,7 @@ public class AnyValueWriter extends AnyValue {
|
||||
|
||||
@Override
|
||||
public AnyValue getAnyValue(String name, boolean create) {
|
||||
for (Entry<AnyValueWriter> en : this.anyEntrys) {
|
||||
for (Entry<AnyValue> en : this.anyEntrys) {
|
||||
if (predicate.test(en.name, name)) {
|
||||
return en.value;
|
||||
}
|
||||
|
||||
@@ -8,39 +8,50 @@ package org.redkale.test.convert.json;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.AnyValue;
|
||||
import org.redkale.util.AnyValueWriter;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class DyncJsonTest {
|
||||
|
||||
private boolean main;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
DyncJsonTest test = new DyncJsonTest();
|
||||
test.main = true;
|
||||
test.run();
|
||||
test.run1();
|
||||
test.run2();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run() throws Exception {
|
||||
public void run1() throws Exception {
|
||||
SimpleDyncBean bean = new SimpleDyncBean();
|
||||
bean.name = "haha";
|
||||
System.out.println(JsonConvert.root().convertTo(bean));
|
||||
if (!main)
|
||||
Assertions.assertEquals("{\"name\":\"haha\"}", JsonConvert.root().convertTo(bean));
|
||||
|
||||
SimpleDyncBean2 bean2 = new SimpleDyncBean2();
|
||||
bean2.name = "haha";
|
||||
|
||||
System.out.println(JsonConvert.root().convertTo(bean2));
|
||||
if (!main)
|
||||
Assertions.assertEquals("{\"name\":\"haha\"}", JsonConvert.root().convertTo(bean2));
|
||||
SimpleDyncBean3 bean3 = new SimpleDyncBean3();
|
||||
bean3.name = "haha";
|
||||
System.out.println(JsonConvert.root().convertTo(bean3));
|
||||
if (!main)
|
||||
Assertions.assertEquals("{\"name\":\"haha\"}", JsonConvert.root().convertTo(bean3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run2() throws Exception {
|
||||
AnyValueWriter writer = AnyValueWriter.create();
|
||||
writer.addValue("name", "aaa");
|
||||
writer.addValue("name", "bbb");
|
||||
writer.addValue("node", AnyValueWriter.create("id", "123"));
|
||||
writer.addValue("node", AnyValueWriter.create("id", "456"));
|
||||
System.out.println(writer);
|
||||
String bs = JsonConvert.root().convertTo(AnyValue.class, writer);
|
||||
AnyValue other = JsonConvert.root().convertFrom(AnyValue.class, bs);
|
||||
System.out.println(other);
|
||||
Assertions.assertEquals(writer.toString(), other.toString());
|
||||
}
|
||||
|
||||
public static class SimpleDyncBean {
|
||||
|
||||
public String name;
|
||||
|
||||
@@ -4,13 +4,18 @@
|
||||
*/
|
||||
package org.redkale.test.convert.pb;
|
||||
|
||||
import org.redkale.test.convert.User;
|
||||
import java.io.Serializable;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufArrayEncoder;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.convert.pb.ProtobufObjectEncoder;
|
||||
import org.redkale.test.convert.User;
|
||||
import org.redkale.util.AnyValue;
|
||||
import org.redkale.util.AnyValueWriter;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -20,12 +25,13 @@ public class UserTest {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
UserTest test = new UserTest();
|
||||
test.run();
|
||||
test.run1();
|
||||
test.run2();
|
||||
test.run3();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run() throws Exception {
|
||||
public void run1() throws Exception {
|
||||
User user = User.create();
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
byte[] bytes = convert.convertTo(user);
|
||||
@@ -47,6 +53,26 @@ public class UserTest {
|
||||
Assertions.assertEquals(bean.toString(), bean2.toString());
|
||||
}
|
||||
|
||||
// @Test
|
||||
public void run3() throws Exception {
|
||||
ProtobufFactory factory = ProtobufFactory.root();
|
||||
AnyValueWriter writer = AnyValueWriter.create();
|
||||
writer.addValue("name", "aaa");
|
||||
writer.addValue("name", "bbb");
|
||||
writer.addValue("node", AnyValueWriter.create("id", "123"));
|
||||
writer.addValue("node", AnyValueWriter.create("id", "456"));
|
||||
System.out.println(writer);
|
||||
ProtobufObjectEncoder encoder = (ProtobufObjectEncoder) factory.loadEncoder(AnyValueWriter.class);
|
||||
System.out.println(encoder);
|
||||
ProtobufArrayEncoder stringEntrys = (ProtobufArrayEncoder) encoder.getMembers()[2].getEncoder();
|
||||
System.out.println(stringEntrys);
|
||||
byte[] bs = factory.getConvert().convertTo(AnyValue.class, writer);
|
||||
System.out.println("长度: " + bs.length);
|
||||
AnyValue other = factory.getConvert().convertFrom(AnyValue.class, bs);
|
||||
System.out.println(other);
|
||||
Assertions.assertEquals(writer.toString(), other.toString());
|
||||
}
|
||||
|
||||
public static class InnerBean {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
|
||||
Reference in New Issue
Block a user