From e82f7e39b0db096f2cf6848707510383c2a10145 Mon Sep 17 00:00:00 2001 From: redkale Date: Sat, 19 Oct 2024 17:50:11 +0800 Subject: [PATCH] anyvalue --- .../org/redkale/convert/AnyValueDecoder.java | 37 --- .../org/redkale/convert/AnyValueEncoder.java | 31 --- .../org/redkale/convert/ConvertFactory.java | 233 ++++++++++-------- .../org/redkale/convert/json/JsonFactory.java | 36 +-- .../convert/pb/ProtobufDynEncoder.java | 3 - .../redkale/convert/pb/ProtobufFactory.java | 123 ++++----- src/main/java/org/redkale/util/AnyValue.java | 14 +- .../java/org/redkale/util/AnyValueWriter.java | 19 +- .../test/convert/json/DyncJsonTest.java | 33 ++- .../org/redkale/test/convert/pb/UserTest.java | 32 ++- 10 files changed, 276 insertions(+), 285 deletions(-) delete mode 100644 src/main/java/org/redkale/convert/AnyValueDecoder.java delete mode 100644 src/main/java/org/redkale/convert/AnyValueEncoder.java diff --git a/src/main/java/org/redkale/convert/AnyValueDecoder.java b/src/main/java/org/redkale/convert/AnyValueDecoder.java deleted file mode 100644 index 1f4cbd2fa..000000000 --- a/src/main/java/org/redkale/convert/AnyValueDecoder.java +++ /dev/null @@ -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实现 - * - *

详情见: https://redkale.org - * - * @author zhangjx - * @param Reader输入的子类型 - * @since 2.5.0 - */ -public class AnyValueDecoder implements Decodeable { - - 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; - } -} diff --git a/src/main/java/org/redkale/convert/AnyValueEncoder.java b/src/main/java/org/redkale/convert/AnyValueEncoder.java deleted file mode 100644 index 97eaccd9b..000000000 --- a/src/main/java/org/redkale/convert/AnyValueEncoder.java +++ /dev/null @@ -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实现 - * - *

详情见: https://redkale.org - * - * @author zhangjx - * @param Writer输出的子类 - * @since 2.5.0 - */ -public class AnyValueEncoder implements Encodeable { - - @Override - public void convertTo(W out, AnyValue value) { - // do nothing - } - - @Override - public Type getType() { - return AnyValue.class; - } -} diff --git a/src/main/java/org/redkale/convert/ConvertFactory.java b/src/main/java/org/redkale/convert/ConvertFactory.java index ddc075ff9..eea11b908 100644 --- a/src/main/java/org/redkale/convert/ConvertFactory.java +++ b/src/main/java/org/redkale/convert/ConvertFactory.java @@ -78,133 +78,150 @@ public abstract class ConvertFactory { private Consumer fieldFuncConsumer; + @SuppressWarnings("OverridableMethodCallInConstructor") protected ConvertFactory(ConvertFactory parent, int features) { this.features = features; this.parent = parent; if (parent == null) { - // --------------------------------------------------------- + this.initPrimitiveCoderInRoot(); + this.initSimpleCoderInRoot(); + this.initObjectCoderInRoot(); + } + } - this.register(boolean.class, BoolSimpledCoder.instance); - this.register(Boolean.class, BoolSimpledCoder.instance); + protected void initPrimitiveCoderInRoot() { + this.register(boolean.class, BoolSimpledCoder.instance); + this.register(Boolean.class, BoolSimpledCoder.instance); - this.register(byte.class, ByteSimpledCoder.instance); - this.register(Byte.class, ByteSimpledCoder.instance); + this.register(byte.class, ByteSimpledCoder.instance); + this.register(Byte.class, ByteSimpledCoder.instance); - this.register(short.class, ShortSimpledCoder.instance); - this.register(Short.class, ShortSimpledCoder.instance); + this.register(short.class, ShortSimpledCoder.instance); + this.register(Short.class, ShortSimpledCoder.instance); - this.register(char.class, CharSimpledCoder.instance); - this.register(Character.class, CharSimpledCoder.instance); + this.register(char.class, CharSimpledCoder.instance); + this.register(Character.class, CharSimpledCoder.instance); - this.register(int.class, IntSimpledCoder.instance); - this.register(Integer.class, IntSimpledCoder.instance); + this.register(int.class, IntSimpledCoder.instance); + this.register(Integer.class, IntSimpledCoder.instance); - this.register(long.class, LongSimpledCoder.instance); - this.register(Long.class, LongSimpledCoder.instance); + this.register(long.class, LongSimpledCoder.instance); + this.register(Long.class, LongSimpledCoder.instance); - this.register(float.class, FloatSimpledCoder.instance); - this.register(Float.class, FloatSimpledCoder.instance); + this.register(float.class, FloatSimpledCoder.instance); + this.register(Float.class, FloatSimpledCoder.instance); - this.register(double.class, DoubleSimpledCoder.instance); - this.register(Double.class, DoubleSimpledCoder.instance); + this.register(double.class, DoubleSimpledCoder.instance); + this.register(Double.class, DoubleSimpledCoder.instance); - this.register(Number.class, NumberSimpledCoder.instance); - this.register(String.class, StringSimpledCoder.instance); - this.register(StringWrapper.class, StringWrapperSimpledCoder.instance); - this.register(CharSequence.class, CharSequenceSimpledCoder.instance); - this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance); - this.register(java.util.Date.class, DateSimpledCoder.instance); - this.register(java.time.Instant.class, InstantSimpledCoder.instance); - this.register(java.time.LocalDate.class, LocalDateSimpledCoder.instance); - this.register(java.time.LocalTime.class, LocalTimeSimpledCoder.instance); - this.register(java.time.LocalDateTime.class, LocalDateTimeSimpledCoder.instance); - this.register(java.time.Duration.class, DurationSimpledCoder.instance); - this.register(AtomicBoolean.class, AtomicBooleanSimpledCoder.instance); - this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance); - this.register(AtomicLong.class, AtomicLongSimpledCoder.instance); - this.register(BigInteger.class, BigIntegerSimpledCoder.instance); - this.register(BigDecimal.class, BigDecimalSimpledCoder.instance); - this.register(InetAddress.class, InetAddressSimpledCoder.instance); - this.register(InetSocketAddress.class, InetAddressSimpledCoder.InetSocketAddressSimpledCoder.instance); - this.register(LongAdder.class, LongAdderSimpledCoder.instance); - this.register(Uint128.class, Uint128SimpledCoder.instance); - this.register(Class.class, TypeSimpledCoder.instance); - this.register(Pattern.class, PatternSimpledCoder.instance); - this.register(File.class, FileSimpledCoder.instance); - this.register(Throwable.class, ThrowableSimpledCoder.instance); - this.register(CompletionHandler.class, CompletionHandlerSimpledCoder.instance); - this.register(URL.class, URLSimpledCoder.instance); - this.register(URI.class, URISimpledCoder.instance); - // --------------------------------------------------------- - this.register(ByteBuffer.class, ByteBufferSimpledCoder.instance); - this.register(boolean[].class, BoolArraySimpledCoder.instance); - this.register(byte[].class, ByteArraySimpledCoder.instance); - this.register(short[].class, ShortArraySimpledCoder.instance); - this.register(char[].class, CharArraySimpledCoder.instance); - this.register(int[].class, IntArraySimpledCoder.instance); - this.register(IntStream.class, IntArraySimpledCoder.IntStreamSimpledCoder.instance); - this.register(long[].class, LongArraySimpledCoder.instance); - this.register(LongStream.class, LongArraySimpledCoder.LongStreamSimpledCoder.instance); - this.register(float[].class, FloatArraySimpledCoder.instance); - this.register(double[].class, DoubleArraySimpledCoder.instance); - this.register(DoubleStream.class, DoubleArraySimpledCoder.DoubleStreamSimpledCoder.instance); - this.register(String[].class, StringArraySimpledCoder.instance); - // --------------------------------------------------------- - this.register(AnyValue.class, Creator.create(AnyValueWriter.class)); - this.register( - HttpCookie.class, (Object... params) -> new HttpCookie((String) params[0], (String) params[1])); - try { - Class sqldateClass = - Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date"); - Invoker sqldateInvoker = Invoker.create(sqldateClass, "valueOf", String.class); - this.register(sqldateClass, new SimpledCoder() { + this.register(Number.class, NumberSimpledCoder.instance); + this.register(String.class, StringSimpledCoder.instance); + } - @Override - public void convertTo(W out, Object value) { - out.writeStandardString(value == null ? null : value.toString()); - } + protected void initSimpleCoderInRoot() { + this.register(StringWrapper.class, StringWrapperSimpledCoder.instance); + this.register(CharSequence.class, CharSequenceSimpledCoder.instance); + this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance); + this.register(java.util.Date.class, DateSimpledCoder.instance); + this.register(java.time.Instant.class, InstantSimpledCoder.instance); + this.register(java.time.LocalDate.class, LocalDateSimpledCoder.instance); + this.register(java.time.LocalTime.class, LocalTimeSimpledCoder.instance); + this.register(java.time.LocalDateTime.class, LocalDateTimeSimpledCoder.instance); + this.register(java.time.Duration.class, DurationSimpledCoder.instance); + this.register(AtomicBoolean.class, AtomicBooleanSimpledCoder.instance); + this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance); + this.register(AtomicLong.class, AtomicLongSimpledCoder.instance); + this.register(BigInteger.class, BigIntegerSimpledCoder.instance); + this.register(BigDecimal.class, BigDecimalSimpledCoder.instance); + this.register(InetAddress.class, InetAddressSimpledCoder.instance); + this.register(InetSocketAddress.class, InetAddressSimpledCoder.InetSocketAddressSimpledCoder.instance); + this.register(LongAdder.class, LongAdderSimpledCoder.instance); + this.register(Uint128.class, Uint128SimpledCoder.instance); + this.register(Class.class, TypeSimpledCoder.instance); + this.register(Pattern.class, PatternSimpledCoder.instance); + this.register(File.class, FileSimpledCoder.instance); + this.register(Throwable.class, ThrowableSimpledCoder.instance); + this.register(CompletionHandler.class, CompletionHandlerSimpledCoder.instance); + this.register(URL.class, URLSimpledCoder.instance); + this.register(URI.class, URISimpledCoder.instance); + // --------------------------------------------------------- + this.register(ByteBuffer.class, ByteBufferSimpledCoder.instance); + this.register(boolean[].class, BoolArraySimpledCoder.instance); + this.register(byte[].class, ByteArraySimpledCoder.instance); + this.register(short[].class, ShortArraySimpledCoder.instance); + this.register(char[].class, CharArraySimpledCoder.instance); + this.register(int[].class, IntArraySimpledCoder.instance); + this.register(IntStream.class, IntArraySimpledCoder.IntStreamSimpledCoder.instance); + this.register(long[].class, LongArraySimpledCoder.instance); + this.register(LongStream.class, LongArraySimpledCoder.LongStreamSimpledCoder.instance); + this.register(float[].class, FloatArraySimpledCoder.instance); + this.register(double[].class, DoubleArraySimpledCoder.instance); + this.register(DoubleStream.class, DoubleArraySimpledCoder.DoubleStreamSimpledCoder.instance); + this.register(String[].class, StringArraySimpledCoder.instance); + // --------------------------------------------------------- + } - @Override - public Object convertFrom(R in) { - String t = in.readStandardString(); - return t == null ? null : sqldateInvoker.invoke(null, t); - } - }); - Class sqltimeClass = - Thread.currentThread().getContextClassLoader().loadClass("java.sql.Time"); - Invoker sqltimeInvoker = Invoker.create(sqltimeClass, "valueOf", String.class); - this.register(sqltimeClass, new SimpledCoder() { + protected void initObjectCoderInRoot() { + this.register(AnyValue.class, Creator.create(AnyValueWriter.class)); + 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); - @Override - public void convertTo(W out, Object value) { - out.writeStandardString(value == null ? null : value.toString()); - } + this.register(HttpCookie.class, (Object... params) -> new HttpCookie((String) params[0], (String) params[1])); + try { + Class sqldateClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Date"); + Invoker sqldateInvoker = Invoker.create(sqldateClass, "valueOf", String.class); + this.register(sqldateClass, new SimpledCoder() { - @Override - public Object convertFrom(R in) { - String t = in.readStandardString(); - return t == null ? null : sqltimeInvoker.invoke(null, t); - } - }); - Class timestampClass = - Thread.currentThread().getContextClassLoader().loadClass("java.sql.Timestamp"); - Invoker timestampInvoker = Invoker.create(timestampClass, "valueOf", String.class); - this.register(timestampClass, new SimpledCoder() { + @Override + public void convertTo(W out, Object value) { + out.writeStandardString(value == null ? null : value.toString()); + } - @Override - public void convertTo(W out, Object value) { - out.writeStandardString(value == null ? null : value.toString()); - } + @Override + public Object convertFrom(R in) { + String t = in.readStandardString(); + return t == null ? null : sqldateInvoker.invoke(null, t); + } + }); + Class sqltimeClass = Thread.currentThread().getContextClassLoader().loadClass("java.sql.Time"); + Invoker sqltimeInvoker = Invoker.create(sqltimeClass, "valueOf", String.class); + this.register(sqltimeClass, new SimpledCoder() { - @Override - public Object convertFrom(R in) { - String t = in.readStandardString(); - return t == null ? null : timestampInvoker.invoke(null, t); - } - }); - } catch (Throwable t) { - // do nothing - } + @Override + public void convertTo(W out, Object value) { + out.writeStandardString(value == null ? null : value.toString()); + } + + @Override + public Object convertFrom(R in) { + String t = in.readStandardString(); + return t == null ? null : sqltimeInvoker.invoke(null, t); + } + }); + Class timestampClass = + Thread.currentThread().getContextClassLoader().loadClass("java.sql.Timestamp"); + Invoker timestampInvoker = Invoker.create(timestampClass, "valueOf", String.class); + this.register(timestampClass, new SimpledCoder() { + + @Override + public void convertTo(W out, Object value) { + out.writeStandardString(value == null ? null : value.toString()); + } + + @Override + public Object convertFrom(R in) { + String t = in.readStandardString(); + return t == null ? null : timestampInvoker.invoke(null, t); + } + }); + } catch (Throwable t) { + // do nothing } } diff --git a/src/main/java/org/redkale/convert/json/JsonFactory.java b/src/main/java/org/redkale/convert/json/JsonFactory.java index 33a5c2def..37e6ecaaa 100644 --- a/src/main/java/org/redkale/convert/json/JsonFactory.java +++ b/src/main/java/org/redkale/convert/json/JsonFactory.java @@ -33,29 +33,29 @@ public final class JsonFactory extends ConvertFactory { 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) { - this.register(InetAddress.class, JsonCoders.InetAddressJsonSimpledCoder.instance); - this.register(InetSocketAddress.class, JsonCoders.InetSocketAddressJsonSimpledCoder.instance); - this.register(Uint128.class, JsonCoders.Uint128JsonSimpledCoder.instance); - this.register(BigInteger.class, JsonCoders.BigIntegerJsonSimpledCoder.instance); - this.register(BigDecimal.class, JsonCoders.BigDecimalJsonSimpledCoder.instance); - this.register(java.time.Instant.class, JsonCoders.InstantJsonSimpledCoder.instance); - this.register(java.time.LocalDate.class, JsonCoders.LocalDateJsonSimpledCoder.instance); - this.register(java.time.LocalTime.class, JsonCoders.LocalTimeJsonSimpledCoder.instance); - this.register(java.time.LocalDateTime.class, JsonCoders.LocalDateTimeJsonSimpledCoder.instance); + } - this.register(JsonElement.class, (Decodeable) JsonElementDecoder.instance); - this.register(JsonString.class, (Decodeable) JsonElementDecoder.instance); - this.register(JsonObject.class, (Decodeable) JsonElementDecoder.instance); - this.register(JsonArray.class, (Decodeable) JsonElementDecoder.instance); - } + @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); + this.register(BigInteger.class, JsonCoders.BigIntegerJsonSimpledCoder.instance); + this.register(BigDecimal.class, JsonCoders.BigDecimalJsonSimpledCoder.instance); + this.register(java.time.Instant.class, JsonCoders.InstantJsonSimpledCoder.instance); + this.register(java.time.LocalDate.class, JsonCoders.LocalDateJsonSimpledCoder.instance); + this.register(java.time.LocalTime.class, JsonCoders.LocalTimeJsonSimpledCoder.instance); + this.register(java.time.LocalDateTime.class, JsonCoders.LocalDateTimeJsonSimpledCoder.instance); + + this.register(JsonElement.class, (Decodeable) JsonElementDecoder.instance); + this.register(JsonString.class, (Decodeable) JsonElementDecoder.instance); + this.register(JsonObject.class, (Decodeable) JsonElementDecoder.instance); + this.register(JsonArray.class, (Decodeable) JsonElementDecoder.instance); } @Override diff --git a/src/main/java/org/redkale/convert/pb/ProtobufDynEncoder.java b/src/main/java/org/redkale/convert/pb/ProtobufDynEncoder.java index d95ea6dc3..6d061eac6 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufDynEncoder.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufDynEncoder.java @@ -372,9 +372,6 @@ public abstract class ProtobufDynEncoder extends ProtobufObjectEncoder { if (!(type instanceof Class)) { return null; } - if (AnyValue.class.isAssignableFrom((Class) type)) { // 不支持 - return null; - } return generateDyncEncoder(factory, (Class) type); } diff --git a/src/main/java/org/redkale/convert/pb/ProtobufFactory.java b/src/main/java/org/redkale/convert/pb/ProtobufFactory.java index 10293a759..50305bbf0 100644 --- a/src/main/java/org/redkale/convert/pb/ProtobufFactory.java +++ b/src/main/java/org/redkale/convert/pb/ProtobufFactory.java @@ -42,77 +42,82 @@ public class ProtobufFactory extends ConvertFactory[] getEntryAnyValueArray( - BiPredicate comparison, Entry[] entitys, String name) { + BiPredicate comparison, Entry[] 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 en : entitys) { + for (Entry en : entitys) { if (comparison.test(en.name, name)) { rs[i++] = en; } @@ -139,7 +140,7 @@ public abstract class AnyValue { } static AnyValue[] getAnyValueArray( - BiPredicate comparison, Entry[] entitys, String name) { + BiPredicate comparison, Entry[] 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 en : entitys) { + for (Entry en : entitys) { if (comparison.test(en.name, name)) { rs[i++] = en.value; } @@ -187,7 +188,7 @@ public abstract class AnyValue { } static AnyValue[] getAnyValueArray( - BiPredicate comparison, Entry[] entitys, String... names) { + BiPredicate comparison, Entry[] 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 en : entitys) { + for (Entry 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(); /** diff --git a/src/main/java/org/redkale/util/AnyValueWriter.java b/src/main/java/org/redkale/util/AnyValueWriter.java index c833ae22b..a9d30c436 100644 --- a/src/main/java/org/redkale/util/AnyValueWriter.java +++ b/src/main/java/org/redkale/util/AnyValueWriter.java @@ -37,7 +37,7 @@ public class AnyValueWriter extends AnyValue { private Entry[] stringEntrys = new Entry[0]; @ConvertColumn(index = 3) - private Entry[] anyEntrys = new Entry[0]; + private Entry[] 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 en = this.anyEntrys[i]; + Entry en = this.anyEntrys[i]; if (en == null) { continue; } @@ -188,7 +188,7 @@ public class AnyValueWriter extends AnyValue { } } if (node.anyEntrys != null) { - for (Entry en : node.anyEntrys) { + for (Entry 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 en : adv.anyEntrys) { + for (Entry en : adv.anyEntrys) { this.addValue(en.name, en.value); } } @@ -314,7 +315,7 @@ public class AnyValueWriter extends AnyValue { } } if (adv.anyEntrys != null) { - for (Entry en : adv.anyEntrys) { + for (Entry en : adv.anyEntrys) { this.setValue(en.name, en.value); } } @@ -372,7 +373,7 @@ public class AnyValueWriter extends AnyValue { return (Entry[]) (Entry[]) anyEntrys; } - public void setAnyEntrys(Entry[] anyEntrys) { + public void setAnyEntrys(Entry[] anyEntrys) { this.anyEntrys = anyEntrys; } @@ -473,7 +474,7 @@ public class AnyValueWriter extends AnyValue { if (!existsValue(name)) { this.addValue(name, value); } else { - for (Entry en : this.anyEntrys) { + for (Entry 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 en : this.anyEntrys) { + for (Entry en : this.anyEntrys) { if (predicate.test(en.name, name)) { return en.value; } diff --git a/src/test/java/org/redkale/test/convert/json/DyncJsonTest.java b/src/test/java/org/redkale/test/convert/json/DyncJsonTest.java index 95d882213..5a781506b 100644 --- a/src/test/java/org/redkale/test/convert/json/DyncJsonTest.java +++ b/src/test/java/org/redkale/test/convert/json/DyncJsonTest.java @@ -8,37 +8,48 @@ 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)); + 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)); + 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)); + 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 { diff --git a/src/test/java/org/redkale/test/convert/pb/UserTest.java b/src/test/java/org/redkale/test/convert/pb/UserTest.java index 009abece4..f728d8b16 100644 --- a/src/test/java/org/redkale/test/convert/pb/UserTest.java +++ b/src/test/java/org/redkale/test/convert/pb/UserTest.java @@ -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)