protobuf
This commit is contained in:
@@ -7,11 +7,6 @@ package org.redkale.convert.json;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.util.*;
|
||||
import org.redkale.asm.*;
|
||||
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
|
||||
@@ -82,24 +77,6 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
|
||||
if (type == Double.class) {
|
||||
return true;
|
||||
}
|
||||
if (type == BigInteger.class) {
|
||||
return true;
|
||||
}
|
||||
if (type == BigDecimal.class) {
|
||||
return true;
|
||||
}
|
||||
if (type == LocalDate.class) {
|
||||
return true;
|
||||
}
|
||||
if (type == LocalTime.class) {
|
||||
return true;
|
||||
}
|
||||
if (type == LocalDateTime.class) {
|
||||
return true;
|
||||
}
|
||||
if (type == java.util.Date.class) {
|
||||
return true;
|
||||
}
|
||||
if (type == boolean[].class) {
|
||||
return true;
|
||||
}
|
||||
@@ -151,7 +128,9 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
|
||||
if (type == String[].class) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (factory.rootFactory().findEncoder(type) != null) {
|
||||
return true;
|
||||
}
|
||||
if (declaringClass == clazz) {
|
||||
return false;
|
||||
}
|
||||
@@ -168,12 +147,7 @@ public abstract class JsonDynEncoder<T> implements Encodeable<JsonWriter, T> {
|
||||
|| t == Long.class
|
||||
|| t == Double.class
|
||||
|| t == String.class
|
||||
|| t == BigInteger.class
|
||||
|| t == BigDecimal.class
|
||||
|| t == LocalDate.class
|
||||
|| t == LocalTime.class
|
||||
|| t == LocalDateTime.class
|
||||
|| t == java.util.Date.class
|
||||
|| factory.rootFactory().findEncoder(t) != null
|
||||
|| ((t instanceof Class) && ((Class) t).isEnum())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class ProtobufCollectionEncoder<T> extends CollectionEncoder<ProtobufWrit
|
||||
protected void writeMemberValue(ProtobufWriter out, EnMember member, Object item, boolean first) {
|
||||
if (simple) {
|
||||
if (item == null) {
|
||||
((ProtobufWriter) out).writeUInt32(0);
|
||||
out.writeUInt32(0);
|
||||
} else {
|
||||
componentEncoder.convertTo(out, item);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import org.redkale.util.RedkaleClassLoader;
|
||||
* @param <W> Writer输出的子类型
|
||||
* @param <E> Enum的子类
|
||||
*/
|
||||
public class ProtobufEnumSimpledCoder<R extends Reader, W extends Writer, E extends Enum>
|
||||
public class ProtobufEnumSimpledCoder<R extends ProtobufReader, W extends ProtobufWriter, E extends Enum>
|
||||
extends SimpledCoder<R, W, E> {
|
||||
|
||||
private final Map<Integer, E> values = new HashMap<>();
|
||||
@@ -48,7 +48,7 @@ public class ProtobufEnumSimpledCoder<R extends Reader, W extends Writer, E exte
|
||||
} else if (enumtostring) {
|
||||
out.writeSmallString(value.toString());
|
||||
} else {
|
||||
((ProtobufWriter) out).writeUInt32(value.ordinal());
|
||||
out.writeUInt32(value.ordinal());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -190,23 +190,21 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
if (member == null || first) {
|
||||
return in;
|
||||
}
|
||||
ProtobufReader reader = (ProtobufReader) in;
|
||||
int tag = reader.readTag();
|
||||
int tag = in.readTag();
|
||||
if (tag != member.getTag()) {
|
||||
reader.backTag(tag);
|
||||
in.backTag(tag);
|
||||
return null;
|
||||
}
|
||||
return in;
|
||||
} else {
|
||||
ProtobufReader reader = (ProtobufReader) in;
|
||||
if (!first && member != null) {
|
||||
int tag = reader.readTag();
|
||||
int tag = in.readTag();
|
||||
if (tag != member.getTag()) {
|
||||
reader.backTag(tag);
|
||||
in.backTag(tag);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
byte[] bs = reader.readByteArray();
|
||||
byte[] bs = in.readByteArray();
|
||||
return new ProtobufReader(bs);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.util.ObjectPool;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class ProtobufReader extends Reader {
|
||||
@@ -25,12 +24,10 @@ public class ProtobufReader extends Reader {
|
||||
|
||||
protected boolean enumtostring;
|
||||
|
||||
public static ObjectPool<ProtobufReader> createPool(int max) {
|
||||
return ObjectPool.createSafePool(max, (Object... params) -> new ProtobufReader(), null, t -> t.recycle());
|
||||
public ProtobufReader() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public ProtobufReader() {}
|
||||
|
||||
public ProtobufReader(byte[] bytes) {
|
||||
setBytes(bytes, 0, bytes.length);
|
||||
}
|
||||
|
||||
@@ -31,10 +31,6 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
|
||||
protected ProtobufWriter parent;
|
||||
|
||||
public static ObjectPool<ProtobufWriter> createPool(int max) {
|
||||
return ObjectPool.createSafePool(max, (Object... params) -> new ProtobufWriter(), null, t -> t.recycle());
|
||||
}
|
||||
|
||||
protected ProtobufWriter(ProtobufWriter parent, int features) {
|
||||
this();
|
||||
this.parent = parent;
|
||||
@@ -484,8 +480,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
this.writeFieldName(member);
|
||||
ProtobufWriter tmp = new ProtobufWriter().configFieldFunc(this);
|
||||
collectionEncoder.convertTo(tmp, member, (Collection) value);
|
||||
int length = tmp.count();
|
||||
this.writeUInt32(length);
|
||||
this.writeLength(tmp.count());
|
||||
this.writeTo(tmp.toArray());
|
||||
}
|
||||
} else {
|
||||
@@ -497,8 +492,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
this.writeFieldName(member);
|
||||
ProtobufWriter tmp = new ProtobufWriter().configFieldFunc(this);
|
||||
streamEncoder.convertTo(tmp, member, (Stream) value);
|
||||
int length = tmp.count();
|
||||
this.writeUInt32(length);
|
||||
this.writeLength(tmp.count());
|
||||
this.writeTo(tmp.toArray());
|
||||
} else {
|
||||
streamEncoder.convertTo(this, member, (Stream) value);
|
||||
@@ -614,6 +608,10 @@ public class ProtobufWriter extends Writer implements ByteTuple {
|
||||
}
|
||||
|
||||
protected void writeUInt32(int value) {
|
||||
if (value >= 0 && value < 128) {
|
||||
writeTo((byte) value);
|
||||
return;
|
||||
}
|
||||
while (true) {
|
||||
if ((value & ~0x7F) == 0) {
|
||||
writeTo((byte) value);
|
||||
|
||||
@@ -20,11 +20,8 @@ import org.redkale.util.*;
|
||||
/** @author zhangjx */
|
||||
public class PBCustMessageTest {
|
||||
|
||||
private boolean main;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
PBCustMessageTest test = new PBCustMessageTest();
|
||||
test.main = true;
|
||||
test.run();
|
||||
}
|
||||
|
||||
@@ -40,20 +37,19 @@ public class PBCustMessageTest {
|
||||
byte[] bs2 = ProtobufConvert.root().convertTo(msg2);
|
||||
System.out.println(Arrays.toString(bs1));
|
||||
System.out.println(Arrays.toString(bs2));
|
||||
if (!main) Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(bs2));
|
||||
Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(bs2));
|
||||
System.out.println();
|
||||
|
||||
OnPlayerLeaveMessage2 newmsg2 = ProtobufConvert.root().convertFrom(OnPlayerLeaveMessage2.class, bs1);
|
||||
byte[] newbs2 = ProtobufConvert.root().convertTo(newmsg2);
|
||||
System.out.println(Arrays.toString(newbs2));
|
||||
if (!main) Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(newbs2));
|
||||
Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(newbs2));
|
||||
System.out.println();
|
||||
|
||||
ProtobufConvert convert = ProtobufConvert.root().newConvert(objFieldFunc);
|
||||
System.out.println(Arrays.toString(convert.convertTo(msg1)));
|
||||
System.out.println(Arrays.toString(convert.convertTo(msg2)));
|
||||
if (!main)
|
||||
Assertions.assertEquals(Arrays.toString(convert.convertTo(msg1)), Arrays.toString(convert.convertTo(msg2)));
|
||||
Assertions.assertEquals(Arrays.toString(convert.convertTo(msg1)), Arrays.toString(convert.convertTo(msg2)));
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ public class User {
|
||||
private String name;
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
private String trueName;
|
||||
private String nickName;
|
||||
|
||||
@ConvertColumn(index = 4)
|
||||
private Integer age;
|
||||
@@ -34,10 +34,10 @@ public class User {
|
||||
public static User create() {
|
||||
User user = new User();
|
||||
user.setId(1L);
|
||||
user.setName("赵侠客"); //
|
||||
user.setAge(29);
|
||||
user.setName("Hello");
|
||||
user.setAge(18);
|
||||
user.setSex("男");
|
||||
user.setTrueName("公众号");
|
||||
user.setNickName("测试号");
|
||||
user.setCreateTime(new Date(1451577600000L));
|
||||
return user;
|
||||
}
|
||||
@@ -58,12 +58,12 @@ public class User {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTrueName() {
|
||||
return trueName;
|
||||
public String getNickName() {
|
||||
return nickName;
|
||||
}
|
||||
|
||||
public void setTrueName(String trueName) {
|
||||
this.trueName = trueName;
|
||||
public void setNickName(String nickName) {
|
||||
this.nickName = nickName;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
package org.redkale.test.convert.pb;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
|
||||
@@ -22,6 +23,10 @@ public class UserTest {
|
||||
public void run() throws Exception {
|
||||
User user = User.create();
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
convert.convertTo(user);
|
||||
byte[] bytes = convert.convertTo(user);
|
||||
User user2 = convert.convertFrom(User.class, bytes);
|
||||
System.out.println(user);
|
||||
System.out.println(user2);
|
||||
Assertions.assertEquals(user.toString(), user2.toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user