This commit is contained in:
redkale
2024-09-30 18:38:50 +08:00
parent 306dbabc7d
commit e7f05c31e0
24 changed files with 954 additions and 100 deletions

View File

@@ -112,7 +112,7 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
field.getGenericType(), field.getAnnotationsByType(ConvertCoder.class), false);
Decodeable<R, ?> fieldCoder;
if (small != null && field.getType() == String.class) {
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
fieldCoder = StringSimpledCoder.StandardStringSimpledCoder.instance;
} else {
fieldCoder = colFactory.findFieldCoder(clazz, field.getName());
}
@@ -216,7 +216,7 @@ public class ObjectDecoder<R extends Reader, T> implements Decodeable<R, T> {
}
Decodeable<R, ?> fieldCoder;
if (small != null && method.getParameterTypes()[0] == String.class) {
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
fieldCoder = StringSimpledCoder.StandardStringSimpledCoder.instance;
} else {
fieldCoder = colFactory.findFieldCoder(clazz, ConvertFactory.readGetSetFieldName(method));
}

View File

@@ -98,7 +98,7 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
field.getGenericType(), field.getAnnotationsByType(ConvertCoder.class), true);
Encodeable<W, ?> fieldCoder;
if (small != null && field.getType() == String.class) {
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
fieldCoder = StringSimpledCoder.StandardStringSimpledCoder.instance;
} else {
fieldCoder = colFactory.findFieldCoder(clazz, field.getName());
}
@@ -182,7 +182,7 @@ public class ObjectEncoder<W extends Writer, T> implements Encodeable<W, T> {
}
Encodeable<W, ?> fieldCoder;
if (small != null && method.getReturnType() == String.class) {
fieldCoder = StringSimpledCoder.SmallStringSimpledCoder.instance;
fieldCoder = StringSimpledCoder.StandardStringSimpledCoder.instance;
} else {
String fieldName = ConvertFactory.readGetSetFieldName(method);
fieldCoder = colFactory.findFieldCoder(clazz, fieldName);

View File

@@ -17,7 +17,7 @@ import org.redkale.convert.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class AtomicLongSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, AtomicLong> {
public class AtomicLongSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, AtomicLong> {
public static final AtomicLongSimpledCoder instance = new AtomicLongSimpledCoder();

View File

@@ -16,7 +16,7 @@ import org.redkale.convert.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class BoolSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Boolean> {
public class BoolSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Boolean> {
public static final BoolSimpledCoder instance = new BoolSimpledCoder();

View File

@@ -16,7 +16,7 @@ import org.redkale.convert.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class ByteSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Byte> {
public class ByteSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Byte> {
public static final ByteSimpledCoder instance = new ByteSimpledCoder();

View File

@@ -16,7 +16,7 @@ import org.redkale.convert.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class CharSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Character> {
public class CharSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Character> {
public static final CharSimpledCoder instance = new CharSimpledCoder();

View File

@@ -19,7 +19,7 @@ import org.redkale.convert.Writer;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class DateSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Date> {
public class DateSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Date> {
public static final DateSimpledCoder instance = new DateSimpledCoder();

View File

@@ -16,7 +16,7 @@ import org.redkale.convert.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class DoubleSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Double> {
public class DoubleSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Double> {
public static final DoubleSimpledCoder instance = new DoubleSimpledCoder();

View File

@@ -16,7 +16,7 @@ import org.redkale.convert.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class FloatSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Float> {
public class FloatSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Float> {
public static final FloatSimpledCoder instance = new FloatSimpledCoder();

View File

@@ -17,7 +17,7 @@ import org.redkale.convert.json.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class IntSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Integer> {
public class IntSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Integer> {
public static final IntSimpledCoder instance = new IntSimpledCoder();

View File

@@ -18,20 +18,20 @@ import org.redkale.convert.json.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class LocalDateSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, LocalDate> {
public class LocalDateSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, LocalDate> {
public static final LocalDateSimpledCoder instance = new LocalDateSimpledCoder();
@Override
public void convertTo(W out, LocalDate value) {
out.writeInt(
value == null ? -1 : value.getYear() * 100_00 + value.getMonthValue() * 100 + value.getDayOfMonth());
value == null ? 0 : value.getYear() * 100_00 + value.getMonthValue() * 100 + value.getDayOfMonth());
}
@Override
public LocalDate convertFrom(R in) {
int t = in.readInt();
return t == -1 ? null : LocalDate.of(t / 100_00, t % 100_00 / 100, t % 100);
return t == 0 ? null : LocalDate.of(t / 100_00, t % 100_00 / 100, t % 100);
}
/**

View File

@@ -18,7 +18,7 @@ import org.redkale.convert.json.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class LocalDateTimeSimpledCoder<R extends Reader, W extends Writer>
public class LocalDateTimeSimpledCoder<R extends Reader, W extends Writer>
extends SimpledCoder<R, W, LocalDateTime> {
private static final ByteArraySimpledCoder bsSimpledCoder = ByteArraySimpledCoder.instance;

View File

@@ -18,7 +18,7 @@ import org.redkale.convert.json.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class LocalTimeSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, LocalTime> {
public class LocalTimeSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, LocalTime> {
public static final LocalTimeSimpledCoder instance = new LocalTimeSimpledCoder();

View File

@@ -17,7 +17,7 @@ import org.redkale.convert.json.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class LongSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Long> {
public class LongSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Long> {
public static final LongSimpledCoder instance = new LongSimpledCoder();

View File

@@ -18,7 +18,7 @@ import org.redkale.convert.Writer;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class NumberSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Number> {
public class NumberSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Number> {
public static final NumberSimpledCoder instance = new NumberSimpledCoder();

View File

@@ -16,7 +16,7 @@ import org.redkale.convert.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class ShortSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Short> {
public class ShortSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, Short> {
public static final ShortSimpledCoder instance = new ShortSimpledCoder();

View File

@@ -18,7 +18,7 @@ import org.redkale.convert.Writer;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class StringSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, String> {
public class StringSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, String> {
public static final StringSimpledCoder instance = new StringSimpledCoder();
@@ -32,10 +32,10 @@ public final class StringSimpledCoder<R extends Reader, W extends Writer> extend
return in.readString();
}
public static final class SmallStringSimpledCoder<R extends Reader, W extends Writer>
public static final class StandardStringSimpledCoder<R extends Reader, W extends Writer>
extends SimpledCoder<R, W, String> {
public static final SmallStringSimpledCoder instance = new SmallStringSimpledCoder();
public static final StandardStringSimpledCoder instance = new StandardStringSimpledCoder();
@Override
public void convertTo(W out, String value) {

View File

@@ -17,7 +17,7 @@ import org.redkale.util.StringWrapper;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class StringWrapperSimpledCoder<R extends Reader, W extends Writer>
public class StringWrapperSimpledCoder<R extends Reader, W extends Writer>
extends SimpledCoder<R, W, StringWrapper> {
public static final StringWrapperSimpledCoder instance = new StringWrapperSimpledCoder();

View File

@@ -13,13 +13,16 @@ import org.redkale.convert.*;
* @author zhangjx
* @param <T> T
*/
public class ProtobufArrayEncoder<T> extends ArrayEncoder<ProtobufWriter, T> {
public class ProtobufArrayEncoder<T> extends ArrayEncoder<ProtobufWriter, T>
implements ProtobufEncodeable<ProtobufWriter, T[]> {
protected final boolean componentSimpled;
protected final boolean requireSizeFlag;
public ProtobufArrayEncoder(ProtobufFactory factory, Type type) {
super(factory, type);
this.componentSimpled = getComponentEncoder() instanceof SimpledCoder;
this.requireSizeFlag = ((ProtobufEncodeable) getComponentEncoder()).requireSize();
}
@Override
@@ -46,4 +49,22 @@ public class ProtobufArrayEncoder<T> extends ArrayEncoder<ProtobufWriter, T> {
}
out.writeArrayE();
}
@Override
public int computeSize(T[] value) {
if (value == null || value.length < 1) {
return 0;
}
int len = 0;
ProtobufEncodeable itemEncoder = (ProtobufEncodeable) this.componentEncoder;
for (T item : value) {
len += itemEncoder.computeSize(item);
}
return len;
}
@Override
public boolean requireSize() {
return requireSizeFlag;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,27 @@
/*
* Copyright (c) 2016-2116 Redkale
* All rights reserved.
*/
package org.redkale.convert.pb;
import org.redkale.convert.Encodeable;
import org.redkale.convert.Writer;
/**
* 带tag的反序列化操作类
*
* <p>详情见: https://redkale.org
*
* @author zhangjx
* @param <W> Writer输出的子类
* @param <T> 序列化的数据类型
*/
public interface ProtobufEncodeable<W extends Writer, T> extends Encodeable<W, T> {
// 计算内容长度
public int computeSize(T value);
// 是否需要计算内容长度
default boolean requireSize() {
return false;
}
}

View File

@@ -5,7 +5,6 @@
*/
package org.redkale.convert.pb;
import java.io.Serializable;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.atomic.*;
@@ -26,45 +25,75 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
Convert.FEATURE_NULLABLE),
Boolean.parseBoolean(System.getProperty("redkale.convert.protobuf.enumtostring", "false")));
static final Decodeable objectDecoder = instance.loadDecoder(Object.class);
static final Encodeable objectEncoder = instance.loadEncoder(Object.class);
protected final boolean enumtostring;
protected boolean reversible = false;
static {
instance.register(Serializable.class, objectDecoder);
instance.register(Serializable.class, objectEncoder);
instance.register(AnyValue.class, instance.loadDecoder(AnyValueWriter.class));
instance.register(AnyValue.class, instance.loadEncoder(AnyValueWriter.class));
instance.register(boolean[].class, ProtobufCoders.ProtobufBoolArraySimpledCoder.instance);
instance.register(byte[].class, ProtobufCoders.ProtobufByteArraySimpledCoder.instance);
instance.register(char[].class, ProtobufCoders.ProtobufCharArraySimpledCoder.instance);
instance.register(short[].class, ProtobufCoders.ProtobufShortArraySimpledCoder.instance);
instance.register(int[].class, ProtobufCoders.ProtobufIntArraySimpledCoder.instance);
instance.register(float[].class, ProtobufCoders.ProtobufFloatArraySimpledCoder.instance);
instance.register(long[].class, ProtobufCoders.ProtobufLongArraySimpledCoder.instance);
instance.register(double[].class, ProtobufCoders.ProtobufDoubleArraySimpledCoder.instance);
instance.register(Boolean[].class, ProtobufCoders.ProtobufBoolArraySimpledCoder2.instance);
instance.register(Byte[].class, ProtobufCoders.ProtobufByteArraySimpledCoder2.instance);
instance.register(Character[].class, ProtobufCoders.ProtobufCharArraySimpledCoder2.instance);
instance.register(Short[].class, ProtobufCoders.ProtobufShortArraySimpledCoder2.instance);
instance.register(Integer[].class, ProtobufCoders.ProtobufIntArraySimpledCoder2.instance);
instance.register(Float[].class, ProtobufCoders.ProtobufFloatArraySimpledCoder2.instance);
instance.register(Long[].class, ProtobufCoders.ProtobufLongArraySimpledCoder2.instance);
instance.register(Double[].class, ProtobufCoders.ProtobufDoubleArraySimpledCoder2.instance);
instance.register(AtomicInteger[].class, ProtobufCoders.ProtobufAtomicIntegerArraySimpledCoder.instance);
instance.register(AtomicLong[].class, ProtobufCoders.ProtobufAtomicLongArraySimpledCoder.instance);
}
@SuppressWarnings("OverridableMethodCallInConstructor")
private ProtobufFactory(ProtobufFactory parent, int features, boolean enumtostring) {
super(parent, features);
this.enumtostring = enumtostring;
if (parent == null) { // root
// ---------------------------------------------------------
this.register(boolean.class, ProtobufCoders.ProtobufBoolSimpledCoder.instance);
this.register(Boolean.class, ProtobufCoders.ProtobufBoolSimpledCoder.instance);
this.register(byte.class, ProtobufCoders.ProtobufByteSimpledCoder.instance);
this.register(Byte.class, ProtobufCoders.ProtobufByteSimpledCoder.instance);
this.register(char.class, ProtobufCoders.ProtobufCharSimpledCoder.instance);
this.register(Character.class, ProtobufCoders.ProtobufCharSimpledCoder.instance);
this.register(short.class, ProtobufCoders.ProtobufShortSimpledCoder.instance);
this.register(Short.class, ProtobufCoders.ProtobufShortSimpledCoder.instance);
this.register(int.class, ProtobufCoders.ProtobufIntSimpledCoder.instance);
this.register(Integer.class, ProtobufCoders.ProtobufIntSimpledCoder.instance);
this.register(float.class, ProtobufCoders.ProtobufFloatSimpledCoder.instance);
this.register(Float.class, ProtobufCoders.ProtobufFloatSimpledCoder.instance);
this.register(long.class, ProtobufCoders.ProtobufLongSimpledCoder.instance);
this.register(Long.class, ProtobufCoders.ProtobufLongSimpledCoder.instance);
this.register(double.class, ProtobufCoders.ProtobufDoubleSimpledCoder.instance);
this.register(Double.class, ProtobufCoders.ProtobufDoubleSimpledCoder.instance);
this.register(Number.class, ProtobufCoders.ProtobufNumberSimpledCoder.instance);
this.register(String.class, ProtobufCoders.ProtobufStringSimpledCoder.instance);
this.register(StringWrapper.class, ProtobufCoders.ProtobufStringWrapperSimpledCoder.instance);
this.register(CharSequence.class, ProtobufCoders.ProtobufCharSequenceSimpledCoder.instance);
this.register(StringBuilder.class, ProtobufCoders.ProtobufStringBuilderSimpledCoder.instance);
this.register(java.util.Date.class, ProtobufCoders.ProtobufDateSimpledCoder.instance);
this.register(java.time.Instant.class, ProtobufCoders.ProtobufInstantSimpledCoder.instance);
this.register(java.time.LocalDate.class, ProtobufCoders.ProtobufLocalDateSimpledCoder.instance);
this.register(java.time.LocalTime.class, ProtobufCoders.ProtobufLocalTimeSimpledCoder.instance);
this.register(java.time.LocalDateTime.class, ProtobufCoders.ProtobufLocalDateTimeSimpledCoder.instance);
this.register(java.time.Duration.class, ProtobufCoders.ProtobufDurationSimpledCoder.instance);
this.register(AtomicBoolean.class, ProtobufCoders.ProtobufAtomicBooleanSimpledCoder.instance);
this.register(AtomicInteger.class, ProtobufCoders.ProtobufAtomicIntegerSimpledCoder.instance);
this.register(AtomicLong.class, ProtobufCoders.ProtobufAtomicLongSimpledCoder.instance);
this.register(boolean[].class, ProtobufCoders.ProtobufBoolArraySimpledCoder.instance);
this.register(byte[].class, ProtobufCoders.ProtobufByteArraySimpledCoder.instance);
this.register(char[].class, ProtobufCoders.ProtobufCharArraySimpledCoder.instance);
this.register(short[].class, ProtobufCoders.ProtobufShortArraySimpledCoder.instance);
this.register(int[].class, ProtobufCoders.ProtobufIntArraySimpledCoder.instance);
this.register(float[].class, ProtobufCoders.ProtobufFloatArraySimpledCoder.instance);
this.register(long[].class, ProtobufCoders.ProtobufLongArraySimpledCoder.instance);
this.register(double[].class, ProtobufCoders.ProtobufDoubleArraySimpledCoder.instance);
this.register(Boolean[].class, ProtobufCoders.ProtobufBoolArraySimpledCoder2.instance);
this.register(Byte[].class, ProtobufCoders.ProtobufByteArraySimpledCoder2.instance);
this.register(Character[].class, ProtobufCoders.ProtobufCharArraySimpledCoder2.instance);
this.register(Short[].class, ProtobufCoders.ProtobufShortArraySimpledCoder2.instance);
this.register(Integer[].class, ProtobufCoders.ProtobufIntArraySimpledCoder2.instance);
this.register(Float[].class, ProtobufCoders.ProtobufFloatArraySimpledCoder2.instance);
this.register(Long[].class, ProtobufCoders.ProtobufLongArraySimpledCoder2.instance);
this.register(Double[].class, ProtobufCoders.ProtobufDoubleArraySimpledCoder2.instance);
this.register(AtomicInteger[].class, ProtobufCoders.ProtobufAtomicIntegerArraySimpledCoder.instance);
this.register(AtomicLong[].class, ProtobufCoders.ProtobufAtomicLongArraySimpledCoder.instance);
this.register(String[].class, this.createArrayDecoder(String[].class));
this.register(String[].class, this.createArrayEncoder(String[].class));
}

View File

@@ -14,7 +14,8 @@ import org.redkale.util.Utility;
* @author zhangjx
* @param <T> T
*/
public class ProtobufObjectEncoder<T> extends ObjectEncoder<ProtobufWriter, T> {
public class ProtobufObjectEncoder<T> extends ObjectEncoder<ProtobufWriter, T>
implements ProtobufEncodeable<ProtobufWriter, T> {
protected ProtobufObjectEncoder(Type type) {
super(type);
@@ -45,4 +46,14 @@ public class ProtobufObjectEncoder<T> extends ObjectEncoder<ProtobufWriter, T> {
parent.offerChild(out);
}
}
@Override
public int computeSize(T value) {
return 0;
}
@Override
public final boolean requireSize() {
return true;
}
}

View File

@@ -26,11 +26,11 @@ public class SelectColumnTest {
SelectColumn sel = SelectColumn.includes(User::getUserId, User::getUserName);
SelectColumn sel2 = SelectColumn.includes("userId", "userName");
Assertions.assertTrue(sel.equals(sel2));
sel.setPatterns(new Pattern[] {Pattern.compile("aaa", 2)});
sel.setPatterns(new Pattern[] {Pattern.compile("aaa")});
System.out.println(JsonConvert.root().convertTo(sel));
String json = "{\"flags\":2,\"pattern\":\"aaa\"}";
String json = "aaa";
Pattern pattern = JsonConvert.root().convertFrom(Pattern.class, json);
Assertions.assertEquals(2, pattern.flags());
Assertions.assertEquals(0, pattern.flags());
Assertions.assertEquals("aaa", pattern.pattern());
}