test
This commit is contained in:
@@ -243,7 +243,7 @@ public abstract class ProtobufDynEncoder<T> extends ProtobufObjectEncoder<T> {
|
||||
} else if (componentType == AtomicLong.class) {
|
||||
wmethodName = "writeFieldAtomicLongsValue";
|
||||
} else if (componentType == String.class) {
|
||||
wmethodName = "writeFieldStringValue";
|
||||
wmethodName = "writeFieldStringsValue";
|
||||
}
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, pbwriterName, wmethodName, "(ILjava/util/Collection;)V", false);
|
||||
} else if (simpledCoders.containsKey(fieldName)) {
|
||||
|
||||
@@ -16,11 +16,8 @@ import org.redkale.convert.json.JsonConvert;
|
||||
/** @author zhangjx */
|
||||
public class ConvertCoderTest {
|
||||
|
||||
private boolean main;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
ConvertCoderTest test = new ConvertCoderTest();
|
||||
test.main = true;
|
||||
test.run();
|
||||
}
|
||||
|
||||
@@ -43,19 +40,13 @@ public class ConvertCoderTest {
|
||||
|
||||
System.out.println(convert.convertTo(msg));
|
||||
String json = "{\"big\":\"0xff\",\"big2\":\"0xff\",\"big3\":\"255\",\"map\":{\"haha\":\"0xfe\"},\"num1\":0}";
|
||||
if (!main) {
|
||||
Assertions.assertEquals(convert.convertTo(msg), json);
|
||||
}
|
||||
Assertions.assertEquals(convert.convertTo(msg), json);
|
||||
BigMessage msg12 = convert.convertFrom(BigMessage.class, json);
|
||||
if (!main) {
|
||||
Assertions.assertEquals(convert.convertTo(msg12), json);
|
||||
}
|
||||
Assertions.assertEquals(convert.convertTo(msg12), json);
|
||||
|
||||
byte[] bs1 = BsonConvert.root().convertTo(msg);
|
||||
byte[] bs2 = BsonConvert.root().convertTo(msg2);
|
||||
if (!main) {
|
||||
Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(bs2));
|
||||
}
|
||||
Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(bs2));
|
||||
}
|
||||
|
||||
public static class BigMessage {
|
||||
|
||||
43
src/test/java/org/redkale/test/convert/ConvertHelper.java
Normal file
43
src/test/java/org/redkale/test/convert/ConvertHelper.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public abstract class ConvertHelper {
|
||||
private ConvertHelper() {
|
||||
//
|
||||
}
|
||||
|
||||
public static byte[] toBytes(ByteBuffer[] buffers) {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
for (ByteBuffer buffer : buffers) {
|
||||
byte[] bs = new byte[buffer.remaining()];
|
||||
buffer.get(bs);
|
||||
out.write(bs, 0, bs.length);
|
||||
}
|
||||
return out.toByteArray();
|
||||
}
|
||||
|
||||
public static Supplier<ByteBuffer> createSupplier() {
|
||||
return () -> ByteBuffer.allocate(1024);
|
||||
}
|
||||
|
||||
public static ByteBuffer createByteBuffer(byte[] bs) {
|
||||
return ByteBuffer.wrap(bs);
|
||||
}
|
||||
|
||||
public static InputStream createInputStream(byte[] bs) {
|
||||
return new ByteArrayInputStream(bs);
|
||||
}
|
||||
}
|
||||
@@ -1,106 +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.test.convert;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.TypeToken;
|
||||
|
||||
/**
|
||||
* 支持泛型的
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <T>
|
||||
* @param <K>
|
||||
* @param <V>
|
||||
*/
|
||||
public class GenericEntity<T, K, V> {
|
||||
|
||||
private K name;
|
||||
|
||||
private List<? extends T> list;
|
||||
|
||||
private Entry<K, V> entry;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
GenericEntity<Long, String, SimpleEntity> bean = new GenericEntity<>();
|
||||
bean.setName("你好");
|
||||
List<Long> list = new ArrayList<>();
|
||||
list.add(1234567890L);
|
||||
bean.setList(list);
|
||||
bean.setEntry(new Entry<>("aaaa", SimpleEntity.create()));
|
||||
final Type type = new TypeToken<GenericEntity<Long, String, SimpleEntity>>() {}.getType();
|
||||
JsonFactory.root().withTinyFeature(true);
|
||||
String json = JsonConvert.root().convertTo(bean);
|
||||
System.out.println(json);
|
||||
System.out.println(JsonConvert.root().convertFrom(type, json).toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"entry\":" + entry + ",\"list\":" + list + ",\"name\":\"" + name + "\"}";
|
||||
}
|
||||
|
||||
public K getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(K name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<? extends T> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<? extends T> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Entry<K, V> getEntry() {
|
||||
return entry;
|
||||
}
|
||||
|
||||
public void setEntry(Entry<K, V> entry) {
|
||||
this.entry = entry;
|
||||
}
|
||||
|
||||
public static class Entry<K, V> {
|
||||
|
||||
private K key;
|
||||
|
||||
private V value;
|
||||
|
||||
public Entry() {}
|
||||
|
||||
public Entry(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(K key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(V value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
254
src/test/java/org/redkale/test/convert/GenericEntityTest.java
Normal file
254
src/test/java/org/redkale/test/convert/GenericEntityTest.java
Normal file
@@ -0,0 +1,254 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.json.JsonFactory;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.util.TypeToken;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class GenericEntityTest {
|
||||
private static final Type ENTITY_TYPE = new TypeToken<GenericEntity<Long, String, SimpleEntity>>() {}.getType();
|
||||
private static final String JSON =
|
||||
"{\"entry\":{\"key\":\"aaaa\",\"value\":{\"addr\":\"127.0.0.1:6666\",\"addrs\":[22222,33333,44444,55555,66666,77777,88888,99999],\"id\":1000000001,\"lists\":[\"aaaa\",\"bbbb\",\"cccc\"],\"map\":{\"AAA\":111,\"CCC\":333,\"BBB\":222},\"name\":\"this is name\\n \\\"test\",\"strings\":[\"zzz\",\"yyy\",\"xxx\"]}},\"list\":[1234567890],\"name\":\"你好\"}";
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
GenericEntityTest test = new GenericEntityTest();
|
||||
test.runJson1();
|
||||
test.runJson2();
|
||||
test.runJson3();
|
||||
test.runPb1();
|
||||
test.runPb2();
|
||||
test.runPb3();
|
||||
test.runBson1();
|
||||
test.runBson2();
|
||||
test.runBson3();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runJson1() throws Exception {
|
||||
JsonFactory.root().withTinyFeature(true);
|
||||
JsonConvert convert = JsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
String json = convert.convertTo(bean);
|
||||
System.out.println(json);
|
||||
System.out.println(convert.convertFrom(ENTITY_TYPE, json).toString());
|
||||
Assertions.assertEquals(JSON, json);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runJson2() throws Exception {
|
||||
JsonFactory.root().withTinyFeature(true);
|
||||
JsonConvert convert = JsonConvert.root();
|
||||
InputStream in = ConvertHelper.createInputStream(createBytes());
|
||||
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, bean.toString());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
convert.convertTo(out, ENTITY_TYPE, bean);
|
||||
Assertions.assertArrayEquals(createBytes(), out.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runJson3() throws Exception {
|
||||
JsonFactory.root().withTinyFeature(true);
|
||||
JsonConvert convert = JsonConvert.root();
|
||||
ByteBuffer in = ConvertHelper.createByteBuffer(createBytes());
|
||||
GenericEntity<Long, String, SimpleEntity> bean = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, bean.toString());
|
||||
Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
|
||||
ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, bean);
|
||||
Assertions.assertArrayEquals(createBytes(), ConvertHelper.toBytes(buffers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runPb1() throws Exception {
|
||||
ProtobufFactory.root().withTinyFeature(true);
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
Utility.println("proto", bs);
|
||||
String rs = convert.convertFrom(ENTITY_TYPE, bs).toString();
|
||||
System.out.println();
|
||||
Assertions.assertEquals(JSON, rs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runPb2() throws Exception {
|
||||
ProtobufFactory.root().withTinyFeature(true);
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
InputStream in = ConvertHelper.createInputStream(bs);
|
||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, rs.toString());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
Assertions.assertArrayEquals(bs, out.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runPb3() throws Exception {
|
||||
ProtobufFactory.root().withTinyFeature(true);
|
||||
ProtobufConvert convert = ProtobufConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
ByteBuffer in = ConvertHelper.createByteBuffer(bs);
|
||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, rs.toString());
|
||||
Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
|
||||
ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson1() throws Exception {
|
||||
BsonFactory.root().withTinyFeature(true);
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
Utility.println("bson", bs);
|
||||
String rs = convert.convertFrom(ENTITY_TYPE, bs).toString();
|
||||
System.out.println();
|
||||
Assertions.assertEquals(JSON, rs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson2() throws Exception {
|
||||
BsonFactory.root().withTinyFeature(true);
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
InputStream in = ConvertHelper.createInputStream(bs);
|
||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, rs.toString());
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
Assertions.assertArrayEquals(bs, out.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson3() throws Exception {
|
||||
BsonFactory.root().withTinyFeature(true);
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(bean);
|
||||
ByteBuffer in = ConvertHelper.createByteBuffer(bs);
|
||||
GenericEntity<Long, String, SimpleEntity> rs = convert.convertFrom(ENTITY_TYPE, in);
|
||||
Assertions.assertEquals(JSON, rs.toString());
|
||||
Supplier<ByteBuffer> out = ConvertHelper.createSupplier();
|
||||
ByteBuffer[] buffers = convert.convertTo(out, ENTITY_TYPE, rs);
|
||||
Assertions.assertArrayEquals(bs, ConvertHelper.toBytes(buffers));
|
||||
}
|
||||
|
||||
private byte[] createBytes() {
|
||||
return JSON.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private GenericEntity<Long, String, SimpleEntity> createBean() {
|
||||
GenericEntity<Long, String, SimpleEntity> bean = new GenericEntity<>();
|
||||
bean.setName("你好");
|
||||
List<Long> list = new ArrayList<>();
|
||||
list.add(1234567890L);
|
||||
bean.setList(list);
|
||||
bean.setEntry(new GenericEntity.Entry<>("aaaa", SimpleEntity.create()));
|
||||
return bean;
|
||||
}
|
||||
|
||||
public static class GenericEntity<T, K, V> {
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
private K name;
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
private List<? extends T> list;
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
private Entry<K, V> entry;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
|
||||
public K getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(K name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<? extends T> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<? extends T> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public Entry<K, V> getEntry() {
|
||||
return entry;
|
||||
}
|
||||
|
||||
public void setEntry(Entry<K, V> entry) {
|
||||
this.entry = entry;
|
||||
}
|
||||
|
||||
public static class Entry<K, V> {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
private K key;
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
private V value;
|
||||
|
||||
public Entry() {}
|
||||
|
||||
public Entry(K key, V value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
|
||||
public K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(K key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(V value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,120 +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.test.convert;
|
||||
|
||||
import org.redkale.convert.*;
|
||||
import org.redkale.convert.bson.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class InnerCoderEntity {
|
||||
|
||||
private final String val;
|
||||
|
||||
private final int id;
|
||||
|
||||
private InnerCoderEntity(int id, String value) {
|
||||
this.id = id;
|
||||
this.val = value;
|
||||
}
|
||||
|
||||
public static InnerCoderEntity create(int id, String value) {
|
||||
return new InnerCoderEntity(id, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该方法提供给Convert组件自动加载。 1) 方法名可以随意。 2) 方法必须是static 3)方法的参数有且只能有一个, 且必须是org.redkale.convert.ConvertFactory或子类。 —3.1)
|
||||
* 参数类型为org.redkale.convert.ConvertFactory 表示适合JSON和BSON。 —3.2) 参数类型为org.redkale.convert.json.JsonFactory 表示仅适合JSON。
|
||||
* —3.3) 参数类型为org.redkale.convert.bson.BsonFactory 表示仅适合BSON。
|
||||
* 4)方法的返回类型必须是org.redkale.convert.Decodeable/org.redkale.convert.Encodeable/org.redkale.convert.SimpledCoder
|
||||
* 若返回类型不是org.redkale.convert.SimpledCoder, 就必须提供两个方法: 一个返回Decodeable 一个返回 Encodeable。
|
||||
*
|
||||
* @param factory
|
||||
* @return
|
||||
*/
|
||||
static SimpledCoder<Reader, Writer, InnerCoderEntity> createConvertCoder(
|
||||
final org.redkale.convert.ConvertFactory factory) {
|
||||
return new SimpledCoder<Reader, Writer, InnerCoderEntity>() {
|
||||
|
||||
private DeMemberInfo memberInfo;
|
||||
|
||||
// 必须与EnMember[] 顺序一致
|
||||
private final DeMember[] deMembers = new DeMember[] {
|
||||
DeMember.create(factory, InnerCoderEntity.class, "id"),
|
||||
DeMember.create(factory, InnerCoderEntity.class, "val")
|
||||
};
|
||||
|
||||
// 必须与DeMember[] 顺序一致
|
||||
private final EnMember[] enMembers = new EnMember[] {
|
||||
EnMember.create(factory, InnerCoderEntity.class, "id"),
|
||||
EnMember.create(factory, InnerCoderEntity.class, "val")
|
||||
};
|
||||
|
||||
{
|
||||
this.memberInfo = DeMemberInfo.create(deMembers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertTo(Writer out, InnerCoderEntity value) {
|
||||
if (value == null) {
|
||||
out.writeObjectNull(InnerCoderEntity.class);
|
||||
return;
|
||||
}
|
||||
out.writeObjectB(value);
|
||||
for (EnMember member : enMembers) {
|
||||
out.writeObjectField(member, value);
|
||||
}
|
||||
out.writeObjectE(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InnerCoderEntity convertFrom(Reader in) {
|
||||
if (in.readObjectB(InnerCoderEntity.class) == null) return null;
|
||||
int index = 0;
|
||||
final Object[] params = new Object[deMembers.length];
|
||||
while (in.hasNext()) {
|
||||
DeMember member = in.readFieldName(memberInfo); // 读取字段名
|
||||
in.readBlank(); // 读取字段名与字段值之间的间隔符,JSON则是跳过冒号:
|
||||
if (member == null) {
|
||||
in.skipValue(); // 跳过不存在的字段的值, 一般不会发生
|
||||
} else {
|
||||
params[index++] = member.read(in);
|
||||
}
|
||||
}
|
||||
in.readObjectE(InnerCoderEntity.class);
|
||||
return InnerCoderEntity.create(params[0] == null ? 0 : (Integer) params[0], (String) params[1]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getVal() {
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
InnerCoderEntity record = InnerCoderEntity.create(200, "haha");
|
||||
final JsonConvert convert = JsonConvert.root();
|
||||
String json = convert.convertTo(record);
|
||||
System.out.println(json);
|
||||
System.out.println(convert.convertFrom(InnerCoderEntity.class, json).toString());
|
||||
|
||||
final BsonConvert convert2 = BsonFactory.root().getConvert();
|
||||
byte[] bs = convert2.convertTo(InnerCoderEntity.class, null);
|
||||
Utility.println("--", bs);
|
||||
InnerCoderEntity r = convert2.convertFrom(InnerCoderEntity.class, bs);
|
||||
System.out.println(r);
|
||||
}
|
||||
}
|
||||
137
src/test/java/org/redkale/test/convert/InnerCoderEntityTest.java
Normal file
137
src/test/java/org/redkale/test/convert/InnerCoderEntityTest.java
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.convert.DeMember;
|
||||
import org.redkale.convert.DeMemberInfo;
|
||||
import org.redkale.convert.EnMember;
|
||||
import org.redkale.convert.Reader;
|
||||
import org.redkale.convert.SimpledCoder;
|
||||
import org.redkale.convert.Writer;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class InnerCoderEntityTest {
|
||||
public static void main(String[] args) throws Throwable {
|
||||
InnerCoderEntityTest test = new InnerCoderEntityTest();
|
||||
test.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run() throws Exception {
|
||||
InnerCoderEntity record = InnerCoderEntity.create(200, "haha");
|
||||
final JsonConvert convert = JsonConvert.root();
|
||||
String json = convert.convertTo(record);
|
||||
System.out.println(json);
|
||||
System.out.println(convert.convertFrom(InnerCoderEntity.class, json).toString());
|
||||
|
||||
final BsonConvert convert2 = BsonFactory.root().getConvert();
|
||||
byte[] bs = convert2.convertTo(InnerCoderEntity.class, null);
|
||||
Utility.println("--", bs);
|
||||
InnerCoderEntity r = convert2.convertFrom(InnerCoderEntity.class, bs);
|
||||
System.out.println(r);
|
||||
}
|
||||
|
||||
public static class InnerCoderEntity {
|
||||
|
||||
private final String val;
|
||||
|
||||
private final int id;
|
||||
|
||||
private InnerCoderEntity(int id, String value) {
|
||||
this.id = id;
|
||||
this.val = value;
|
||||
}
|
||||
|
||||
public static InnerCoderEntity create(int id, String value) {
|
||||
return new InnerCoderEntity(id, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 该方法提供给Convert组件自动加载。 1) 方法名可以随意。 2) 方法必须是static 3)方法的参数有且只能有一个, 且必须是org.redkale.convert.ConvertFactory或子类。 —3.1)
|
||||
* 参数类型为org.redkale.convert.ConvertFactory 表示适合JSON和BSON。 —3.2) 参数类型为org.redkale.convert.json.JsonFactory 表示仅适合JSON。
|
||||
* —3.3) 参数类型为org.redkale.convert.bson.BsonFactory 表示仅适合BSON。
|
||||
* 4)方法的返回类型必须是org.redkale.convert.Decodeable/org.redkale.convert.Encodeable/org.redkale.convert.SimpledCoder
|
||||
* 若返回类型不是org.redkale.convert.SimpledCoder, 就必须提供两个方法: 一个返回Decodeable 一个返回 Encodeable。
|
||||
*
|
||||
* @param factory
|
||||
* @return
|
||||
*/
|
||||
static SimpledCoder<Reader, Writer, InnerCoderEntity> createConvertCoder(
|
||||
final org.redkale.convert.ConvertFactory factory) {
|
||||
return new SimpledCoder<Reader, Writer, InnerCoderEntity>() {
|
||||
|
||||
private DeMemberInfo memberInfo;
|
||||
|
||||
// 必须与EnMember[] 顺序一致
|
||||
private final DeMember[] deMembers = new DeMember[] {
|
||||
DeMember.create(factory, InnerCoderEntity.class, "id"),
|
||||
DeMember.create(factory, InnerCoderEntity.class, "val")
|
||||
};
|
||||
|
||||
// 必须与DeMember[] 顺序一致
|
||||
private final EnMember[] enMembers = new EnMember[] {
|
||||
EnMember.create(factory, InnerCoderEntity.class, "id"),
|
||||
EnMember.create(factory, InnerCoderEntity.class, "val")
|
||||
};
|
||||
|
||||
{
|
||||
this.memberInfo = DeMemberInfo.create(deMembers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void convertTo(Writer out, InnerCoderEntity value) {
|
||||
if (value == null) {
|
||||
out.writeObjectNull(InnerCoderEntity.class);
|
||||
return;
|
||||
}
|
||||
out.writeObjectB(value);
|
||||
for (EnMember member : enMembers) {
|
||||
out.writeObjectField(member, value);
|
||||
}
|
||||
out.writeObjectE(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public InnerCoderEntity convertFrom(Reader in) {
|
||||
if (in.readObjectB(InnerCoderEntity.class) == null) return null;
|
||||
int index = 0;
|
||||
final Object[] params = new Object[deMembers.length];
|
||||
while (in.hasNext()) {
|
||||
DeMember member = in.readFieldName(memberInfo); // 读取字段名
|
||||
in.readBlank(); // 读取字段名与字段值之间的间隔符,JSON则是跳过冒号:
|
||||
if (member == null) {
|
||||
in.skipValue(); // 跳过不存在的字段的值, 一般不会发生
|
||||
} else {
|
||||
params[index++] = member.read(in);
|
||||
}
|
||||
}
|
||||
in.readObjectE(InnerCoderEntity.class);
|
||||
return InnerCoderEntity.create(params[0] == null ? 0 : (Integer) params[0], (String) params[1]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getVal() {
|
||||
return val;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,26 +7,35 @@ package org.redkale.test.convert;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.util.Creator;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class SimpleEntity {
|
||||
|
||||
@ConvertColumn(index = 7)
|
||||
private String name;
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
private String desc = "";
|
||||
|
||||
@ConvertColumn(index = 4)
|
||||
private int id = (int) System.currentTimeMillis();
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
private int[] addrs;
|
||||
|
||||
@ConvertColumn(index = 5)
|
||||
private List<String> lists;
|
||||
|
||||
@ConvertColumn(index = 8)
|
||||
private String[] strings;
|
||||
|
||||
@ConvertColumn(index = 6)
|
||||
private Map<String, Integer> map;
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
private InetSocketAddress addr;
|
||||
|
||||
public static SimpleEntity create() {
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.bson;
|
||||
|
||||
import org.redkale.test.convert.SimpleEntity;
|
||||
import org.redkale.test.convert.SimpleChildEntity;
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.bson;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.bson;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.bson;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -3,8 +3,10 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.ConvertImpl;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
@@ -12,11 +14,21 @@ import org.redkale.convert.json.JsonConvert;
|
||||
/** @author zhangjx */
|
||||
public class ConvertImplTest {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
ConvertImplTest test = new ConvertImplTest();
|
||||
test.run1();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run1() throws Throwable {
|
||||
String json = "{'name':'hellow'}";
|
||||
OneEntity one = JsonConvert.root().convertFrom(OneEntity.class, json);
|
||||
Assertions.assertTrue(one instanceof OneImpl);
|
||||
byte[] bs = json.getBytes();
|
||||
one = JsonConvert.root().convertFrom(OneEntity.class, new ByteArrayInputStream(bs));
|
||||
Assertions.assertTrue(one instanceof OneImpl);
|
||||
one = JsonConvert.root().convertFrom(OneEntity.class, ByteBuffer.wrap(bs));
|
||||
Assertions.assertTrue(one instanceof OneImpl);
|
||||
}
|
||||
|
||||
@ConvertImpl(OneImpl.class)
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import static java.lang.annotation.ElementType.*;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.persistence.Id;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.*;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -11,6 +11,8 @@ import java.util.Map;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.Convert;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.test.convert.SimpleChildEntity;
|
||||
import org.redkale.test.convert.SimpleEntity;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class JsonMainTest {
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.junit.jupiter.api.*;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.ConvertImpl;
|
||||
@@ -16,7 +16,6 @@ public class JsonMultiObjectDecoderTest {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
JsonMultiObjectDecoderTest test = new JsonMultiObjectDecoderTest();
|
||||
test.main = true;
|
||||
test.run();
|
||||
}
|
||||
|
||||
@@ -25,19 +24,19 @@ public class JsonMultiObjectDecoderTest {
|
||||
{
|
||||
String json = "{\"a0\":\"000\",\"a6\":\"666\"}";
|
||||
AbstractBean bean = JsonConvert.root().convertFrom(AbstractBean.class, json);
|
||||
if (!main) Assertions.assertEquals(bean.getClass().getName(), Bean69.class.getName());
|
||||
Assertions.assertEquals(bean.getClass().getName(), Bean69.class.getName());
|
||||
System.out.println(bean);
|
||||
}
|
||||
{
|
||||
String json = "{\"a0\":\"000\",\"a2\":\"222\",\"a4\":\"444\"}";
|
||||
AbstractBean bean = JsonConvert.root().convertFrom(AbstractBean.class, json);
|
||||
if (!main) Assertions.assertEquals(bean.getClass().getName(), Bean423.class.getName());
|
||||
Assertions.assertEquals(bean.getClass().getName(), Bean423.class.getName());
|
||||
System.out.println(bean);
|
||||
}
|
||||
{
|
||||
String json = "{\"a0\":\"000\",\"a6\":\"666\",\"a5\":\"555\"}";
|
||||
AbstractBean bean = JsonConvert.root().convertFrom(AbstractBean.class, json);
|
||||
if (!main) Assertions.assertEquals(bean.getClass().getName(), Bean65.class.getName());
|
||||
Assertions.assertEquals(bean.getClass().getName(), Bean65.class.getName());
|
||||
System.out.println(bean);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import org.junit.jupiter.api.*;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.Convert;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.persistence.Id;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.convert.ObjectEncoder;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.convert.ObjectEncoder;
|
||||
@@ -3,7 +3,7 @@
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
package org.redkale.test.convert.json;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.convert.ObjectEncoder;
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
package org.redkale.test.convert.media;
|
||||
|
||||
import org.redkale.test.convert.bson.ConvertRecord;
|
||||
import java.util.*;
|
||||
import org.redkale.convert.json.*;
|
||||
import org.redkale.test.convert.*;
|
||||
|
||||
Reference in New Issue
Block a user