移除BSON功能
This commit is contained in:
@@ -9,9 +9,10 @@ import java.math.BigInteger;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.ConvertCoder;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.ext.BigIntegerSimpledCoder.BigIntegerHexJsonSimpledCoder;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class ConvertCoderTest {
|
||||
@@ -44,37 +45,47 @@ public class ConvertCoderTest {
|
||||
BigMessage msg12 = convert.convertFrom(BigMessage.class, json);
|
||||
Assertions.assertEquals(convert.convertTo(msg12), json);
|
||||
|
||||
byte[] bs1 = BsonConvert.root().convertTo(msg);
|
||||
byte[] bs2 = BsonConvert.root().convertTo(msg2);
|
||||
byte[] bs1 = ProtobufConvert.root().convertTo(msg);
|
||||
byte[] bs2 = ProtobufConvert.root().convertTo(msg2);
|
||||
Assertions.assertEquals(Arrays.toString(bs1), Arrays.toString(bs2));
|
||||
}
|
||||
|
||||
public static class BigMessage {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
||||
public BigInteger big;
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
||||
public BigInteger big2;
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
public BigInteger big3;
|
||||
|
||||
public int num1;
|
||||
|
||||
@ConvertColumn(index = 4)
|
||||
@ConvertCoder(encoder = BigIntegerHexJsonSimpledCoder.class, decoder = BigIntegerHexJsonSimpledCoder.class)
|
||||
public Map<String, BigInteger> map;
|
||||
|
||||
@ConvertColumn(index = 5)
|
||||
public int num1;
|
||||
}
|
||||
|
||||
public static class BigMessage2 {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
public BigInteger big;
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
public BigInteger big2;
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
public BigInteger big3;
|
||||
|
||||
public int num1;
|
||||
|
||||
@ConvertColumn(index = 4)
|
||||
public Map<String, BigInteger> map;
|
||||
|
||||
@ConvertColumn(index = 5)
|
||||
public int num1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
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.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.util.TypeToken;
|
||||
@@ -39,9 +38,6 @@ public class GenericEntityTest {
|
||||
test.runPb1();
|
||||
test.runPb2();
|
||||
test.runPb3();
|
||||
test.runBson1();
|
||||
test.runBson2();
|
||||
test.runBson3();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -123,51 +119,6 @@ public class GenericEntityTest {
|
||||
Assertions.assertArrayEquals(bs, bs2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson1() throws Exception {
|
||||
System.out.println("-------------------- runBson1 ---------------------------------");
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(ENTITY_TYPE, bean);
|
||||
Utility.println("bson", bs);
|
||||
String rs = convert.convertFrom(ENTITY_TYPE, bs).toString();
|
||||
Assertions.assertEquals(JSON, rs);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson2() throws Exception {
|
||||
System.out.println("-------------------- runBson2 ---------------------------------");
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(ENTITY_TYPE, bean);
|
||||
Utility.println("bson1 ", bs);
|
||||
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);
|
||||
byte[] bs2 = ConvertHelper.toBytes(buffers);
|
||||
Utility.println("bson2 ", bs2);
|
||||
Assertions.assertArrayEquals(bs, bs2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void runBson3() throws Exception {
|
||||
System.out.println("-------------------- runBson3 ---------------------------------");
|
||||
BsonConvert convert = BsonConvert.root();
|
||||
GenericEntity<Long, String, SimpleEntity> bean = createBean();
|
||||
byte[] bs = convert.convertTo(ENTITY_TYPE, bean);
|
||||
Utility.println("bson1 ", bs);
|
||||
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);
|
||||
byte[] bs2 = out.toByteArray();
|
||||
Utility.println("bson2 ", bs2);
|
||||
Assertions.assertArrayEquals(bs, bs2);
|
||||
}
|
||||
|
||||
private byte[] createBytes() {
|
||||
return JSON.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@@ -5,15 +5,16 @@
|
||||
package org.redkale.test.convert;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
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.convert.pb.ProtobufConvert;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
/**
|
||||
@@ -34,7 +35,7 @@ public class InnerCoderEntityTest {
|
||||
System.out.println(json);
|
||||
System.out.println(convert.convertFrom(InnerCoderEntity.class, json).toString());
|
||||
|
||||
final BsonConvert convert2 = BsonFactory.root().getConvert();
|
||||
final ProtobufConvert convert2 = ProtobufFactory.root().getConvert();
|
||||
byte[] bs = convert2.convertTo(InnerCoderEntity.class, null);
|
||||
Utility.println("--", bs);
|
||||
InnerCoderEntity r = convert2.convertFrom(InnerCoderEntity.class, bs);
|
||||
@@ -42,11 +43,12 @@ public class InnerCoderEntityTest {
|
||||
}
|
||||
|
||||
public static class InnerCoderEntity {
|
||||
|
||||
private final String val;
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
private final int id;
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
private final String val;
|
||||
|
||||
private InnerCoderEntity(int id, String value) {
|
||||
this.id = id;
|
||||
this.val = value;
|
||||
@@ -58,8 +60,8 @@ public class InnerCoderEntityTest {
|
||||
|
||||
/**
|
||||
* 该方法提供给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。
|
||||
* 参数类型为org.redkale.convert.ConvertFactory 表示适合JSON和PROTOBUF。 —3.2) 参数类型为org.redkale.convert.json.JsonFactory 表示仅适合JSON。
|
||||
* —3.3) 参数类型为org.redkale.convert.pb.ProtobufFactory 表示仅适合PROTOBUF。
|
||||
* 4)方法的返回类型必须是org.redkale.convert.Decodeable/org.redkale.convert.Encodeable/org.redkale.convert.SimpledCoder
|
||||
* 若返回类型不是org.redkale.convert.SimpledCoder, 就必须提供两个方法: 一个返回Decodeable 一个返回 Encodeable。
|
||||
*
|
||||
|
||||
@@ -1,288 +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.bson;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.annotation.ConstructorParameters;
|
||||
import org.redkale.convert.bson.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.persistence.*;
|
||||
import org.redkale.test.convert.SimpleChildEntity;
|
||||
import org.redkale.test.convert.SimpleEntity;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class BsonMainTest {
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
BsonMainTest test = new BsonMainTest();
|
||||
test.run1();
|
||||
test.run2();
|
||||
test.run3();
|
||||
test.run4();
|
||||
test.run5();
|
||||
test.run6();
|
||||
test.run7();
|
||||
test.run8();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run1() throws Throwable {
|
||||
Serializable[] sers = new Serializable[] {"aaa", 4};
|
||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
||||
byte[] bytes = convert.convertTo(sers);
|
||||
Utility.println("---", bytes);
|
||||
byte[] checks = new byte[] {
|
||||
0x00, 0x00, 0x00, 0x02, 0x7f, 0x01, 0x41, 0x00, 0x00, 0x00, 0x03, 0x61, 0x61, 0x61, 0x01, 0x69, 0x00, 0x00,
|
||||
0x00, 0x04
|
||||
};
|
||||
Assertions.assertArrayEquals(checks, bytes);
|
||||
Serializable[] a = convert.convertFrom(Serializable[].class, bytes);
|
||||
Assertions.assertEquals("[aaa, 4]", Arrays.toString(a));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run2() throws Exception {
|
||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
||||
SimpleChildEntity entry = SimpleChildEntity.create();
|
||||
byte[] bytes = convert.convertTo(SimpleEntity.class, entry);
|
||||
System.out.println("长度: " + bytes.length);
|
||||
Assertions.assertEquals(258, bytes.length);
|
||||
BsonByteBufferWriter writer = convert.pollWriter(() -> ByteBuffer.allocate(1));
|
||||
convert.convertTo(writer, SimpleEntity.class, entry);
|
||||
ByteBuffer[] buffers = writer.toBuffers();
|
||||
int len = 0;
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
for (ByteBuffer b : buffers) {
|
||||
len += b.remaining();
|
||||
byte[] ts = new byte[b.remaining()];
|
||||
b.get(ts);
|
||||
out.write(ts);
|
||||
b.flip();
|
||||
}
|
||||
System.out.println("长度: " + len);
|
||||
Assertions.assertEquals(258, len);
|
||||
SimpleChildEntity entry2 = convert.convertFrom(SimpleChildEntity.class, buffers);
|
||||
System.out.println(entry);
|
||||
Assertions.assertEquals(entry.toString(), entry2.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run3() throws Exception {
|
||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
||||
SimpleChildEntity entry = SimpleChildEntity.create();
|
||||
byte[] bytes = convert.convertTo(SimpleEntity.class, entry);
|
||||
Utility.println(null, bytes);
|
||||
System.out.println(JsonConvert.root().convertTo(entry));
|
||||
SimpleEntity rs = convert.convertFrom(SimpleEntity.class, bytes);
|
||||
Assertions.assertEquals(JsonConvert.root().convertTo(entry), rs.toString());
|
||||
|
||||
ComplextEntity bean = new ComplextEntity();
|
||||
byte[] bytes2 = convert.convertTo(Object.class, bean);
|
||||
final int len = bytes2.length;
|
||||
BsonByteBufferWriter writer = convert.pollWriter(() -> ByteBuffer.allocate(len / 2));
|
||||
convert.convertTo(writer, bean);
|
||||
bytes2 = writer.toByteArray().getBytes();
|
||||
System.out.println(convert.convertFrom(ComplextEntity.class, bytes2).toString());
|
||||
Assertions.assertEquals(
|
||||
"{\"chname\":\"\",\"flag\":true,\"userid\":0}",
|
||||
convert.convertFrom(ComplextEntity.class, bytes2).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run4() throws Exception {
|
||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
||||
SimpleChildEntity entry = SimpleChildEntity.create();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
convert.convertTo(out, SimpleEntity.class, entry);
|
||||
byte[] bytes = out.toByteArray();
|
||||
Utility.println(null, bytes);
|
||||
SimpleEntity rs = convert.convertFrom(SimpleEntity.class, new ByteArrayInputStream(bytes));
|
||||
System.out.println(rs.toString());
|
||||
Assertions.assertEquals(JsonConvert.root().convertTo(entry), rs.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run5() throws Exception {
|
||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
||||
|
||||
LinkedHashMap map = new LinkedHashMap();
|
||||
map.put("1", 1);
|
||||
map.put("2", "a2");
|
||||
byte[] bs = convert.convertTo(Object.class, map);
|
||||
Object mapobj = convert.convertFrom(Object.class, bs);
|
||||
System.out.println(mapobj);
|
||||
Assertions.assertEquals("{1=1, 2=a2}", mapobj.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run6() throws Exception {
|
||||
final BsonConvert convert = BsonFactory.root().getConvert();
|
||||
|
||||
Optional<String> val = Optional.ofNullable("haha");
|
||||
byte[] bs = convert.convertTo(val);
|
||||
Object obj = convert.convertFrom(Optional.class, bs);
|
||||
System.out.println(obj);
|
||||
Assertions.assertEquals("Optional[haha]", obj.toString());
|
||||
bs = convert.convertTo(Object.class, val);
|
||||
obj = convert.convertFrom(Object.class, bs);
|
||||
Assertions.assertEquals("Optional[haha]", obj.toString());
|
||||
bs = convert.convertTo(new TypeToken<Optional<String>>() {}.getType(), val);
|
||||
obj = convert.convertFrom(new TypeToken<Optional<String>>() {}.getType(), bs);
|
||||
Assertions.assertEquals("Optional[haha]", obj.toString());
|
||||
System.out.println(JsonConvert.root().convertTo(val));
|
||||
Assertions.assertEquals("\"haha\"", JsonConvert.root().convertTo(val));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run7() throws Throwable {
|
||||
Two two = new Two();
|
||||
two.setKey("key111");
|
||||
two.setCode(12345);
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("haha");
|
||||
two.setList(list);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("222", "333");
|
||||
two.setStringMap(map);
|
||||
|
||||
List<ConvertRecord> records = new ArrayList<>();
|
||||
records.add(ConvertRecord.createDefault());
|
||||
two.setRecords(records);
|
||||
|
||||
Map<String, ConvertRecord> rmap = new HashMap<>();
|
||||
rmap.put("222", ConvertRecord.createDefault());
|
||||
two.setRecordMap(rmap);
|
||||
|
||||
byte[] bs = BsonFactory.root().getConvert().convertTo(two);
|
||||
|
||||
One one = BsonFactory.root().getConvert().convertFrom(One.class, bs);
|
||||
System.out.println(one);
|
||||
Assertions.assertEquals(
|
||||
"{\"bytes\":[3,4,5],\"code\":12345,\"ints\":[3000,4000,5000],\"key\":\"key111\"}", one.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run8() throws Exception {
|
||||
final JsonConvert jsonConvert = JsonConvert.root();
|
||||
final BsonConvert bsonConvert = BsonFactory.root().getConvert();
|
||||
ConstructorArgsEntity bean = new ConstructorArgsEntity(12345678, "哈哈");
|
||||
bean.setCreatetime(12345678901L);
|
||||
String json = jsonConvert.convertTo(bean);
|
||||
System.out.println(json);
|
||||
Assertions.assertEquals("{\"createtime\":12345678901,\"name\":\"哈哈\",\"userid\":12345678}", json);
|
||||
Assertions.assertEquals(
|
||||
jsonConvert.convertFrom(ConstructorArgsEntity.class, json).toString(), json);
|
||||
byte[] bytes = bsonConvert.convertTo(bean);
|
||||
Assertions.assertEquals(
|
||||
bsonConvert.convertFrom(ConstructorArgsEntity.class, bytes).toString(), json);
|
||||
}
|
||||
|
||||
public static class ComplextEntity {
|
||||
|
||||
@Id
|
||||
private int userid;
|
||||
|
||||
private String chname = "";
|
||||
|
||||
@Transient
|
||||
private boolean flag = true;
|
||||
|
||||
@Transient
|
||||
private List<SimpleChildEntity> children;
|
||||
|
||||
@Transient
|
||||
private SimpleEntity user;
|
||||
|
||||
public int getUserid() {
|
||||
return userid;
|
||||
}
|
||||
|
||||
public void setUserid(int userid) {
|
||||
this.userid = userid;
|
||||
}
|
||||
|
||||
public String getChname() {
|
||||
return chname;
|
||||
}
|
||||
|
||||
public void setChname(String chname) {
|
||||
this.chname = chname;
|
||||
}
|
||||
|
||||
public boolean isFlag() {
|
||||
return flag;
|
||||
}
|
||||
|
||||
public void setFlag(boolean flag) {
|
||||
this.flag = flag;
|
||||
}
|
||||
|
||||
public List<SimpleChildEntity> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<SimpleChildEntity> children) {
|
||||
this.children = children;
|
||||
}
|
||||
|
||||
public SimpleEntity getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(SimpleEntity user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ConstructorArgsEntity {
|
||||
|
||||
private final int userid;
|
||||
|
||||
private String name;
|
||||
|
||||
private long createtime;
|
||||
|
||||
@ConstructorParameters({"userid", "name"})
|
||||
public ConstructorArgsEntity(int userid, String name) {
|
||||
this.userid = userid;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getUserid() {
|
||||
return userid;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getCreatetime() {
|
||||
return createtime;
|
||||
}
|
||||
|
||||
public void setCreatetime(long createtime) {
|
||||
this.createtime = createtime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,155 +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.bson;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/** @author redkale */
|
||||
public class ConvertRecord {
|
||||
|
||||
private String aname;
|
||||
|
||||
private String desc = "";
|
||||
|
||||
private int id = (int) System.currentTimeMillis();
|
||||
|
||||
private int[] integers;
|
||||
|
||||
private long[] longs;
|
||||
|
||||
private List<String> strings;
|
||||
|
||||
private Map<String, Integer> map;
|
||||
|
||||
public static ConvertRecord createDefault() {
|
||||
ConvertRecord v = new ConvertRecord();
|
||||
v.setAname("this is name\n \"test");
|
||||
v.setId(1000000001);
|
||||
v.setIntegers(new int[] {12, 34, 56, 78, 90, 123, 456, 789});
|
||||
v.setLongs(new long[] {
|
||||
10000012L, 10000034L, 10000056L, 10000078L, -10000090L, -100000123L, -100000456L, -100000789L
|
||||
});
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("str_a");
|
||||
list.add("str_b");
|
||||
list.add("str_c");
|
||||
v.setStrings(list);
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
map.put("key_a", 111);
|
||||
map.put("key_b", 222);
|
||||
map.put("key_c", 333);
|
||||
v.setMap(map);
|
||||
return v;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
final ConvertRecord entry = ConvertRecord.createDefault();
|
||||
run(ConvertRecord.class, entry);
|
||||
}
|
||||
|
||||
public static <T> void run(final Class<T> type, final T entry) throws Exception {
|
||||
/**
|
||||
* final org.redkale.convert.json.JsonConvert convert =
|
||||
* org.redkale.convert.json.JsonFactory.root().getConvert(); final String entryString =
|
||||
* convert.convertTo(entry); convert.convertFrom(type, entryString); System.out.println("redkale-convert: " +
|
||||
* convert.convertTo(entry));
|
||||
*
|
||||
* <p>com.alibaba.fastjson.JSON.parseObject(entryString, type); System.out.println("fastjson 1.2.7: " +
|
||||
* com.alibaba.fastjson.JSON.toJSONString(entry));
|
||||
*
|
||||
* <p>final com.fasterxml.jackson.databind.ObjectMapper mapper = new
|
||||
* com.fasterxml.jackson.databind.ObjectMapper(); mapper.readValue(entryString, type);
|
||||
* System.out.println("jackson 2.7.2: " + mapper.writeValueAsString(entry));
|
||||
*
|
||||
* <p>final com.google.gson.Gson gson = new com.google.gson.Gson(); gson.fromJson(entryString, type);
|
||||
* System.out.println("google-gson 2.4: " + gson.toJson(entry));
|
||||
*
|
||||
* <p>System.out.println("------------------------------------------------"); System.out.println("组件 序列化耗时(ms)
|
||||
* 反序列化耗时(ms)"); final int count = 10_0000; long s = System.currentTimeMillis(); for (int i = 0; i < count; i++)
|
||||
* { convert.convertTo(entry); } long e = System.currentTimeMillis() - s; System.out.print("redkale-convert " +
|
||||
* e);
|
||||
*
|
||||
* <p>s = System.currentTimeMillis(); for (int i = 0; i < count; i++) { convert.convertFrom(type, entryString);
|
||||
* } e = System.currentTimeMillis() - s; System.out.println("\t " + e);
|
||||
*
|
||||
* <p>//---------------------------------------------------------------------------- s =
|
||||
* System.currentTimeMillis(); for (int i = 0; i < count; i++) { com.alibaba.fastjson.JSON.toJSONString(entry);
|
||||
* } e = System.currentTimeMillis() - s; System.out.print("fastjson 1.2.7 " + e);
|
||||
*
|
||||
* <p>s = System.currentTimeMillis(); for (int i = 0; i < count; i++) {
|
||||
* com.alibaba.fastjson.JSON.parseObject(entryString, type); } e = System.currentTimeMillis() - s;
|
||||
* System.out.println("\t " + e); //----------------------------------------------------------------------------
|
||||
* s = System.currentTimeMillis(); for (int i = 0; i < count; i++) { mapper.writeValueAsString(entry); } e =
|
||||
* System.currentTimeMillis() - s; System.out.print("jackson 2.7.2 " + e);
|
||||
*
|
||||
* <p>s = System.currentTimeMillis(); for (int i = 0; i < count; i++) { mapper.readValue(entryString, type); } e
|
||||
* = System.currentTimeMillis() - s; System.out.println("\t " + e);
|
||||
* //---------------------------------------------------------------------------- s =
|
||||
* System.currentTimeMillis(); for (int i = 0; i < count; i++) { gson.toJson(entry); } e =
|
||||
* System.currentTimeMillis() - s; System.out.print("google-gson 2.4 " + e);
|
||||
*
|
||||
* <p>s = System.currentTimeMillis(); for (int i = 0; i < count; i++) { gson.fromJson(entryString, type); } e =
|
||||
* System.currentTimeMillis() - s; System.out.println("\t " + e);
|
||||
* //----------------------------------------------------------------------------
|
||||
*/
|
||||
}
|
||||
|
||||
public String getAname() {
|
||||
return aname;
|
||||
}
|
||||
|
||||
public void setAname(String aname) {
|
||||
this.aname = aname;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
|
||||
public void setDesc(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int[] getIntegers() {
|
||||
return integers;
|
||||
}
|
||||
|
||||
public void setIntegers(int[] integers) {
|
||||
this.integers = integers;
|
||||
}
|
||||
|
||||
public long[] getLongs() {
|
||||
return longs;
|
||||
}
|
||||
|
||||
public void setLongs(long[] longs) {
|
||||
this.longs = longs;
|
||||
}
|
||||
|
||||
public List<String> getStrings() {
|
||||
return strings;
|
||||
}
|
||||
|
||||
public void setStrings(List<String> strings) {
|
||||
this.strings = strings;
|
||||
}
|
||||
|
||||
public Map<String, Integer> getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setMap(Map<String, Integer> map) {
|
||||
this.map = map;
|
||||
}
|
||||
}
|
||||
@@ -1,85 +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.bson;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.util.Utility;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class One {
|
||||
|
||||
protected String key;
|
||||
|
||||
protected int code;
|
||||
|
||||
protected byte[] bytes = new byte[] {3, 4, 5};
|
||||
|
||||
protected int[] ints = new int[] {3000, 4000, 5000};
|
||||
|
||||
public One(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public void setBytes(byte[] bytes) {
|
||||
this.bytes = bytes;
|
||||
}
|
||||
|
||||
public int[] getInts() {
|
||||
return ints;
|
||||
}
|
||||
|
||||
public void setInts(int[] ints) {
|
||||
this.ints = ints;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return JsonConvert.root().convertTo(this);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
int count = 100_0000;
|
||||
One one = new One(234);
|
||||
one.bytes = new byte[] {1, 2, 3};
|
||||
one.key = "哈哈";
|
||||
|
||||
System.out.println(Arrays.toString(Utility.encodeUTF8(JsonConvert.root().convertTo(one))));
|
||||
System.out.println(Arrays.toString(JsonConvert.root().convertToBytes(one)));
|
||||
long s = System.currentTimeMillis();
|
||||
for (int i = 0; i < count; i++) {
|
||||
JsonConvert.root().convertTo(one).getBytes();
|
||||
}
|
||||
long e = System.currentTimeMillis() - s;
|
||||
|
||||
long s2 = System.currentTimeMillis();
|
||||
for (int i = 0; i < count; i++) {
|
||||
JsonConvert.root().convertToBytes(one);
|
||||
}
|
||||
long e2 = System.currentTimeMillis() - s2;
|
||||
System.out.println(e);
|
||||
System.out.println(e2);
|
||||
}
|
||||
}
|
||||
@@ -1,66 +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.bson;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class Two extends One {
|
||||
|
||||
public Two() {
|
||||
super(90100119);
|
||||
}
|
||||
|
||||
protected List<String> list;
|
||||
|
||||
protected Map<String, String> stringMap;
|
||||
|
||||
protected List<ConvertRecord> records;
|
||||
|
||||
protected Map<String, ConvertRecord> recordMap;
|
||||
|
||||
public Map<String, String> getStringMap() {
|
||||
return stringMap;
|
||||
}
|
||||
|
||||
public void setStringMap(Map<String, String> stringMap) {
|
||||
this.stringMap = stringMap;
|
||||
}
|
||||
|
||||
String ip;
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public List<String> getList() {
|
||||
return list;
|
||||
}
|
||||
|
||||
public void setList(List<String> list) {
|
||||
this.list = list;
|
||||
}
|
||||
|
||||
public List<ConvertRecord> getRecords() {
|
||||
return records;
|
||||
}
|
||||
|
||||
public void setRecords(List<ConvertRecord> records) {
|
||||
this.records = records;
|
||||
}
|
||||
|
||||
public Map<String, ConvertRecord> getRecordMap() {
|
||||
return recordMap;
|
||||
}
|
||||
|
||||
public void setRecordMap(Map<String, ConvertRecord> recordMap) {
|
||||
this.recordMap = recordMap;
|
||||
}
|
||||
}
|
||||
@@ -5,10 +5,8 @@
|
||||
*/
|
||||
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.*;
|
||||
|
||||
/** @author redkale */
|
||||
public class MediaContent implements java.io.Serializable {
|
||||
@@ -26,7 +24,6 @@ public class MediaContent implements java.io.Serializable {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
final MediaContent entry = MediaContent.createDefault();
|
||||
ConvertRecord.run(MediaContent.class, entry);
|
||||
}
|
||||
|
||||
public static MediaContent createDefault() {
|
||||
|
||||
@@ -12,8 +12,8 @@ import java.net.Socket;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.boot.Application;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.net.AsyncIOGroup;
|
||||
import org.redkale.net.http.HttpServer;
|
||||
@@ -40,7 +40,7 @@ public class RestConvertTest {
|
||||
asyncGroup.start();
|
||||
final ResourceFactory resFactory = ResourceFactory.create();
|
||||
resFactory.register(JsonConvert.root());
|
||||
resFactory.register(BsonConvert.root());
|
||||
resFactory.register(ProtobufConvert.root());
|
||||
Method method = Application.class.getDeclaredMethod("initWorkExecutor");
|
||||
method.setAccessible(true);
|
||||
method.invoke(application);
|
||||
|
||||
@@ -10,8 +10,8 @@ import java.lang.reflect.Method;
|
||||
import java.net.*;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.boot.Application;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.net.AsyncIOGroup;
|
||||
import org.redkale.net.http.*;
|
||||
@@ -34,7 +34,7 @@ public class RestSleepTest {
|
||||
asyncGroup.start();
|
||||
final ResourceFactory resFactory = ResourceFactory.create();
|
||||
resFactory.register(JsonConvert.root());
|
||||
resFactory.register(BsonConvert.root());
|
||||
resFactory.register(ProtobufConvert.root());
|
||||
Method method = Application.class.getDeclaredMethod("initWorkExecutor");
|
||||
method.setAccessible(true);
|
||||
method.invoke(application);
|
||||
|
||||
@@ -15,8 +15,8 @@ import java.util.concurrent.atomic.*;
|
||||
import java.util.logging.Level;
|
||||
import org.redkale.annotation.Resource;
|
||||
import org.redkale.boot.*;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.net.AsyncIOGroup;
|
||||
import org.redkale.net.client.ClientAddress;
|
||||
@@ -44,7 +44,7 @@ public class ABMainService implements Service {
|
||||
new SncpClient("", asyncGroup, "0", sncpAddress, new ClientAddress(sncpAddress), "TCP", 16, 100);
|
||||
final ResourceFactory resFactory = ResourceFactory.create();
|
||||
resFactory.register(JsonConvert.root());
|
||||
resFactory.register(BsonConvert.root());
|
||||
resFactory.register(ProtobufConvert.root());
|
||||
final SncpRpcGroups rpcGroups = application.getSncpRpcGroups();
|
||||
rpcGroups.computeIfAbsent("g77", "TCP").putAddress(new InetSocketAddress("127.0.0.1", 5577));
|
||||
rpcGroups.computeIfAbsent("g88", "TCP").putAddress(new InetSocketAddress("127.0.0.1", 5588));
|
||||
|
||||
@@ -5,8 +5,8 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.boot.Application;
|
||||
import org.redkale.convert.bson.BsonConvert;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.net.AsyncIOGroup;
|
||||
import org.redkale.net.WorkThread;
|
||||
@@ -35,7 +35,7 @@ public class SncpSleepTest {
|
||||
final ResourceFactory resFactory = ResourceFactory.create();
|
||||
resFactory.register(Application.RESNAME_APP_EXECUTOR, ExecutorService.class, workExecutor);
|
||||
resFactory.register(JsonConvert.root());
|
||||
resFactory.register(BsonConvert.root());
|
||||
resFactory.register(ProtobufConvert.root());
|
||||
|
||||
// ------------------------ 初始化 CService ------------------------------------
|
||||
SncpSleepService service = Sncp.createSimpleLocalService(SncpSleepService.class, resFactory);
|
||||
@@ -64,6 +64,6 @@ public class SncpSleepTest {
|
||||
System.out.println("耗时: " + e + " ms");
|
||||
server.shutdown();
|
||||
workExecutor.shutdown();
|
||||
Assertions.assertTrue(e < 600);
|
||||
Assertions.assertTrue(e < 660);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.boot.*;
|
||||
import org.redkale.convert.bson.*;
|
||||
import org.redkale.convert.pb.ProtobufConvert;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.inject.ResourceFactory;
|
||||
import org.redkale.net.*;
|
||||
import org.redkale.net.client.ClientAddress;
|
||||
@@ -52,7 +53,7 @@ public class SncpTest {
|
||||
application = Application.create(true);
|
||||
rpcGroups = application.getSncpRpcGroups();
|
||||
factory = application.getResourceFactory();
|
||||
factory.register("", BsonConvert.class, BsonFactory.root().getConvert());
|
||||
factory.register("", ProtobufConvert.class, ProtobufFactory.root().getConvert());
|
||||
factory.register("", Application.class, application);
|
||||
|
||||
if (System.getProperty("client") == null) {
|
||||
|
||||
@@ -5,8 +5,9 @@
|
||||
*/
|
||||
package org.redkale.test.sncp;
|
||||
|
||||
import org.redkale.convert.bson.BsonFactory;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
import org.redkale.convert.pb.ProtobufFactory;
|
||||
import org.redkale.persistence.Id;
|
||||
import org.redkale.source.FilterBean;
|
||||
import org.redkale.util.Utility;
|
||||
@@ -15,17 +16,19 @@ import org.redkale.util.Utility;
|
||||
public class SncpTestBean implements FilterBean {
|
||||
|
||||
@Id
|
||||
@ConvertColumn(index = 1)
|
||||
private long id;
|
||||
|
||||
@ConvertColumn(index = 2)
|
||||
private String content;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String json = "{\"content\":\"数据: 01\",\"id\":1}";
|
||||
SncpTestBean bean = JsonConvert.root().convertFrom(SncpTestBean.class, json);
|
||||
System.out.println(bean);
|
||||
byte[] bs = BsonFactory.root().getConvert().convertTo(bean);
|
||||
byte[] bs = ProtobufFactory.root().getConvert().convertTo(bean);
|
||||
Utility.println("---------", bs);
|
||||
System.out.println(BsonFactory.root()
|
||||
System.out.println(ProtobufFactory.root()
|
||||
.getConvert()
|
||||
.convertFrom(SncpTestBean.class, bs)
|
||||
.toString());
|
||||
|
||||
Reference in New Issue
Block a user