This commit is contained in:
wentch
2015-12-18 16:01:23 +08:00
parent 743955e1bd
commit b1799c5829
11 changed files with 210 additions and 756 deletions

View File

@@ -19,31 +19,32 @@ import java.util.Arrays;
* @author zhangjx
*/
public class BsonTestMain {
public static void main(String[] args) throws Exception {
Serializable[] sers = new Serializable[]{"aaa",4};
Serializable[] sers = new Serializable[]{"aaa", 4};
final BsonConvert convert = BsonFactory.root().getConvert();
byte[] bytes = convert.convertTo(sers);
Serializable[] a = convert.convertFrom(Serializable[].class, bytes);
byte[] bytes = convert.convertTo(sers);
Serializable[] a = convert.convertFrom(Serializable[].class, bytes);
System.out.println(Arrays.toString(a));
main2(args);
}
public static void main2(String[] args) throws Exception {
final BsonConvert convert = BsonFactory.root().getConvert();
TestEntry2 entry = TestEntry2.create();
byte[] bytes = convert.convertTo(TestEntry.class, entry);
Utility.println(null,bytes);
System.out.println(JsonFactory.root().getConvert().convertTo(entry));
TestEntry rs = convert.convertFrom(TestEntry.class, bytes);
SimpleChildEntity entry = SimpleChildEntity.create();
byte[] bytes = convert.convertTo(SimpleEntity.class, entry);
Utility.println(null, bytes);
System.out.println(JsonFactory.root().getConvert().convertTo(entry));
SimpleEntity rs = convert.convertFrom(SimpleEntity.class, bytes);
System.out.println(rs.toString());
System.out.println(JsonFactory.root().getConvert().convertTo(rs));
TestComplextBean bean = new TestComplextBean();
System.out.println(JsonFactory.root().getConvert().convertTo(rs));
ComplextEntity bean = new ComplextEntity();
byte[] bytes2 = convert.convertTo(Object.class, bean);
final int len = bytes2.length;
BsonByteBufferWriter writer = convert.pollBsonWriter(()-> ByteBuffer.allocate(len/2));
BsonByteBufferWriter writer = convert.pollBsonWriter(() -> ByteBuffer.allocate(len / 2));
convert.convertTo(writer, bean);
bytes2 = writer.toArray();
System.out.println(convert.convertFrom(TestComplextBean.class, bytes2).toString());
System.out.println(convert.convertFrom(ComplextEntity.class, bytes2).toString());
}
}

View File

@@ -0,0 +1,71 @@
/*
* 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.util.List;
import javax.persistence.*;
/**
*
* @author zhangjx
*/
public class ComplextEntity extends BasedEntity {
@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;
}
}

View File

@@ -10,10 +10,11 @@ import org.redkale.convert.bson.*;
import org.redkale.convert.json.*;
/**
* 测试不存在无参数的构造函数的bean类解析
*
* @author zhangjx
*/
public class NotEmptyConstructorParamsBean {
public class ConstructorArgsEntity {
private final int userid;
@@ -22,7 +23,7 @@ public class NotEmptyConstructorParamsBean {
private long createtime;
@ConstructorProperties({"userid", "name"})
public NotEmptyConstructorParamsBean(int userid, String name) {
public ConstructorArgsEntity(int userid, String name) {
this.userid = userid;
this.name = name;
}
@@ -30,13 +31,13 @@ public class NotEmptyConstructorParamsBean {
public static void main(String[] args) throws Exception {
final JsonConvert jsonConvert = JsonFactory.root().getConvert();
final BsonConvert bsonConvert = BsonFactory.root().getConvert();
NotEmptyConstructorParamsBean bean = new NotEmptyConstructorParamsBean(12345678, "哈哈");
ConstructorArgsEntity bean = new ConstructorArgsEntity(12345678, "哈哈");
bean.setCreatetime(System.currentTimeMillis());
String json = jsonConvert.convertTo(bean);
System.out.println(json);
System.out.println(jsonConvert.convertFrom(NotEmptyConstructorParamsBean.class, json).toString());
System.out.println(jsonConvert.convertFrom(ConstructorArgsEntity.class, json).toString());
byte[] bytes = bsonConvert.convertTo(bean);
System.out.println(bsonConvert.convertFrom(NotEmptyConstructorParamsBean.class, bytes).toString());
System.out.println(bsonConvert.convertFrom(ConstructorArgsEntity.class, bytes).toString());
}
public int getUserid() {

View File

@@ -0,0 +1,109 @@
/*
* 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.util.TypeToken;
import org.redkale.convert.json.JsonFactory;
import java.lang.reflect.*;
import java.util.*;
/**
* 支持泛型的
*
* @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().setTiny(true);
String json = JsonFactory.root().getConvert().convertTo(bean);
System.out.println(json);
System.out.println(JsonFactory.root().getConvert().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 JsonFactory.root().getConvert().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;
}
}
}

View File

@@ -7,8 +7,6 @@ package org.redkale.test.convert;
import org.redkale.convert.json.JsonConvert;
import org.redkale.convert.json.JsonFactory;
import org.redkale.util.TypeToken;
import java.lang.reflect.*;
import java.nio.*;
import java.util.*;
@@ -18,15 +16,12 @@ import java.util.*;
*/
public class JsonTestMain {
private static final Type MAPTYPE = new TypeToken<Map<String, String>>() {
}.getType();
public static void main(String[] args) throws Exception {
JsonFactory factory = JsonFactory.root();
factory.setTiny(true);
final JsonConvert convert = JsonFactory.root().getConvert();
String json = "{\"access_token\":\"vVX2bIjN5P9TMOphDkStM96eNWapAehTuWAlVDO74aFaYxLwj2b-9-T9p_W2mfr9\",\"expires_in\":7200, \"aa\":\"\"}";
Map<String, String> map = convert.convertFrom(MAPTYPE, json);
Map<String, String> map = convert.convertFrom(JsonConvert.TYPE_MAP_STRING_STRING, json);
System.out.println(map);
System.out.println(convert.convertTo(map));
ByteBuffer[] buffers = convert.convertTo(() -> ByteBuffer.allocate(1024), map);

View File

@@ -13,12 +13,12 @@ import java.util.*;
* @author zhangjx
*/
@ConvertEntity("myname")
public class TestEntry2 extends TestEntry {
public class SimpleChildEntity extends SimpleEntity {
private String extend;
public static TestEntry2 create() {
TestEntry2 v = new TestEntry2();
public static SimpleChildEntity create() {
SimpleChildEntity v = new SimpleChildEntity();
v.setName("this is name\n \"test");
v.setId(1000000001);
v.setAddrs(new int[]{22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999});

View File

@@ -13,7 +13,7 @@ import java.util.*;
*
* @author zhangjx
*/
public class TestEntry {
public class SimpleEntity {
private String name;
@@ -29,8 +29,8 @@ public class TestEntry {
private Map<String, Integer> map;
public static TestEntry create() {
TestEntry v = new TestEntry();
public static SimpleEntity create() {
SimpleEntity v = new SimpleEntity();
v.setName("this is name\n \"test");
v.setId(1000000001);
v.setAddrs(new int[]{22222, 33333, 44444, 55555, 66666, 77777, 88888, 99999});
@@ -50,8 +50,8 @@ public class TestEntry {
public static void main(String[] args) throws Exception {
System.out.println(JsonFactory.root().getConvert().convertTo(create()));
Creator<TestEntry> creator = Creator.create(TestEntry.class); //Creator.create(10, TestEntry.class);
TestEntry entry = creator.create();
Creator<SimpleEntity> creator = Creator.create(SimpleEntity.class); //Creator.create(10, SimpleEntity.class);
SimpleEntity entry = creator.create();
System.out.println(entry);
for(int i =0; i < 10000000; i++){
creator.create();

View File

@@ -1,391 +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.util.List;
import javax.persistence.*;
/**
*
* @author zhangjx
*/
public class TestComplextBean extends BasedEntity{
@Id
private int userid;
private String chname = "";
private int organid;
private String photos = "";
private String introvideourl = "";
private String introduction = "";
private String linkemail = "";
private String telephone = "";
private String skype = "";
private String weixin = "";
private String jego = "";
private String city = ""; //'导师所在城市',
private String states = ""; //导师所在洲名
private String country = ""; //导师所在国家
private int zones;// 导师所在时区
private int lac;// 1东北部2西部4中西部8南部
private short hyproficient;
private long createtime;
private long updatetime;
private int edutype;
private int major;
private short iecalevel; //IECA会员或者认证1、IECA会员一级 2、IECA会员二级 3、IECA会员三级 4、IECA认证一级5、IECA认证二级6、IECA认证三级7、非会员非认证导师'
private int workyear;
private int rateservice;//'服务态度评分(总分)',
private int ratemajor;//'咨询专业评分(总分)',
private int ratenum;//'导师评分人次数(总评价人次)',
private int successnum;//'服务过多少名申请学生',
private int successrate; //'申请的成功率: 9500,表示成功率为95%',
private int successhotrate; //'常青藤学校比例: 6000表示比例为60%',
private long hots;
private int starlevel;
private String degreestr = "";
private String searchflag="";
private String searchkey = "";
@Transient
private boolean baseinfoAll = true;//基本信息是否完整
@Transient
private List<UserMentorPrize> prizes;
@Transient
private UserInfo user;
public List<UserMentorPrize> getPrizes() {
return prizes;
}
public void setPrizes(List<UserMentorPrize> prizes) {
this.prizes = prizes;
}
public boolean isBaseinfoAll() {
return baseinfoAll;
}
public void setBaseinfoAll(boolean baseinfoAll) {
this.baseinfoAll = baseinfoAll;
}
public UserInfo getUser() {
return user;
}
public void setUser(UserInfo user) {
this.user = 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 int getOrganid() {
return organid;
}
public void setOrganid(int organid) {
this.organid = organid;
}
public String getPhotos() {
return photos;
}
public void setPhotos(String photos) {
this.photos = photos;
}
public String getIntrovideourl() {
return introvideourl;
}
public void setIntrovideourl(String introvideourl) {
this.introvideourl = introvideourl;
}
public String getIntroduction() {
return introduction;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
public String getLinkemail() {
return linkemail;
}
public void setLinkemail(String linkemail) {
this.linkemail = linkemail;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getSkype() {
return skype;
}
public void setSkype(String skype) {
this.skype = skype;
}
public String getWeixin() {
return weixin;
}
public void setWeixin(String weixin) {
this.weixin = weixin;
}
public String getJego() {
return jego;
}
public void setJego(String jego) {
this.jego = jego;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStates() {
return states;
}
public void setStates(String states) {
this.states = states;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getZones() {
return zones;
}
public void setZones(int zones) {
this.zones = zones;
}
public int getLac() {
return lac;
}
public void setLac(int lac) {
this.lac = lac;
}
public short getHyproficient() {
return hyproficient;
}
public void setHyproficient(short hyproficient) {
this.hyproficient = hyproficient;
}
public long getCreatetime() {
return createtime;
}
public void setCreatetime(long createtime) {
this.createtime = createtime;
}
public long getUpdatetime() {
return updatetime;
}
public void setUpdatetime(long updatetime) {
this.updatetime = updatetime;
}
public int getEdutype() {
return edutype;
}
public void setEdutype(int edutype) {
this.edutype = edutype;
}
public int getMajor() {
return major;
}
public void setMajor(int major) {
this.major = major;
}
public short getIecalevel() {
return iecalevel;
}
public void setIecalevel(short iecalevel) {
this.iecalevel = iecalevel;
}
public int getWorkyear() {
return workyear;
}
public void setWorkyear(int workyear) {
this.workyear = workyear;
}
public int getRateservice() {
return rateservice;
}
public void setRateservice(int rateservice) {
this.rateservice = rateservice;
}
public int getRatemajor() {
return ratemajor;
}
public void setRatemajor(int ratemajor) {
this.ratemajor = ratemajor;
}
public int getRatenum() {
return ratenum;
}
public void setRatenum(int ratenum) {
this.ratenum = ratenum;
}
public int getSuccessnum() {
return successnum;
}
public void setSuccessnum(int successnum) {
this.successnum = successnum;
}
public int getSuccessrate() {
return successrate;
}
public void setSuccessrate(int successrate) {
this.successrate = successrate;
}
public int getSuccesshotrate() {
return successhotrate;
}
public void setSuccesshotrate(int successhotrate) {
this.successhotrate = successhotrate;
}
public long getHots() {
return hots;
}
public void setHots(long hots) {
this.hots = hots;
}
public int getStarlevel() {
return starlevel;
}
public void setStarlevel(int starlevel) {
this.starlevel = starlevel;
}
public String getDegreestr() {
return degreestr;
}
public void setDegreestr(String degreestr) {
this.degreestr = degreestr;
}
public String getSearchflag() {
return searchflag;
}
public void setSearchflag(String searchflag) {
this.searchflag = searchflag;
}
public String getSearchkey() {
return searchkey;
}
public void setSearchkey(String searchkey) {
this.searchkey = searchkey;
}
}

View File

@@ -1,129 +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.util.Sheet;
import org.redkale.util.TypeToken;
import org.redkale.convert.bson.BsonFactory;
import org.redkale.convert.json.JsonFactory;
import java.lang.reflect.*;
import java.util.*;
/**
*
* @author zhangjx
* @param <T>
* @param <K>
* @param <V>
*/
public class TestConvertBean<T, K, V> {
public static class Entry {
private String id;
private String remark;
public Entry(){
}
public Entry(String id, String remark) {
this.id = id;
this.remark = remark;
}
@Override
public String toString() {
return "Entry{" + "id=" + id + ", remark=" + remark + '}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
private T name;
private List<? extends K> list;
private Map<K, V> map;
public static void main(String[] args) throws Throwable {
TestConvertBean<Long, String, Entry> bean = new TestConvertBean<>();
bean.setName(1234567890L);
List<String> list = new ArrayList<>();
list.add("你好");
bean.setList(list);
Map<String, Entry> map = new HashMap<>();
map.put("myvalue", new Entry("myid", ""));
bean.setMap(map);
final Type type = new TypeToken<TestConvertBean<Long, String, Entry>>() {
}.getType();
JsonFactory.root().setTiny(true);
String json = JsonFactory.root().getConvert().convertTo(type, bean);
System.out.println(json);
System.out.println( JsonFactory.root().getConvert().convertFrom(type, json).toString());
// JsonFactory child = JsonFactory.root().createChild();
// System.out.println(child.register(TestConvertBean.class, "name", new ConvertColumnEntry("name", true)));
// child.register(TestConvertBean.class, child.createEncoder(type));
// System.out.println(child.getConvert().convertTo(type, bean));
if(true) return;
Sheet<Entry> sheet = new Sheet<>();
sheet.setTotal(20);
List<Entry> list2 = new ArrayList<>();
list2.add(new Entry("myid", "描述"));
sheet.setRows(list2);
final Type type2 = new TypeToken<Sheet<Entry>>() {
}.getType();
System.out.println(JsonFactory.root().getConvert().convertTo(type2, sheet));
sheet = BsonFactory.root().getConvert().convertFrom(type2, BsonFactory.root().getConvert().convertTo(type2, sheet));
System.out.println(JsonFactory.root().getConvert().convertTo(type2, sheet));
}
@Override
public String toString() {
return "TestConvertBean{" + "name=" + name + ", list=" + list + ", map=" + map + '}';
}
public T getName() {
return name;
}
public void setName(T name) {
this.name = name;
}
public List<? extends K> getList() {
return list;
}
public void setList(List<? extends K> list) {
this.list = list;
}
public Map<K, V> getMap() {
return map;
}
public void setMap(Map<K, V> map) {
this.map = map;
}
}

View File

@@ -1,114 +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;
/**
*
* @author zhangjx
*/
public class UserInfo extends BasedEntity {
private int userid;
private String namekey;
private String nickname;
private String chname;
private String password;
private String mobile;
private String email;
private short type;
private short status;
private int gmt = 8;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getNamekey() {
return namekey;
}
public void setNamekey(String namekey) {
this.namekey = namekey;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getChname() {
return chname;
}
public void setChname(String chname) {
this.chname = chname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public short getType() {
return type;
}
public void setType(short type) {
this.type = type;
}
public short getStatus() {
return status;
}
public void setStatus(short status) {
this.status = status;
}
public int getGmt() {
return gmt;
}
public void setGmt(int gmt) {
this.gmt = gmt;
}
}

View File

@@ -1,89 +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.source.DistributeGenerator;
import javax.persistence.*;
/**
*
* @author zhangjx
*/
public class UserMentorPrize extends BasedEntity implements Comparable<UserMentorPrize> {
private static final long serialVersionUID = 1L;
@Id
@DistributeGenerator(initialValue = 10001)
private long mentorprizeid;
private int userid;
private String prizename;
private int happenday;
@Column(updatable = false)
private long createtime = System.currentTimeMillis();
private long updatetime;
public UserMentorPrize() {
}
public long getMentorprizeid() {
return mentorprizeid;
}
public void setMentorprizeid(long mentorprizeid) {
this.mentorprizeid = mentorprizeid;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getPrizename() {
return prizename;
}
public void setPrizename(String prizename) {
this.prizename = prizename;
}
public int getHappenday() {
return happenday;
}
public void setHappenday(int happenday) {
this.happenday = happenday;
}
public long getCreatetime() {
return createtime;
}
public void setCreatetime(long createtime) {
this.createtime = createtime;
}
public long getUpdatetime() {
return updatetime;
}
public void setUpdatetime(long updatetime) {
this.updatetime = updatetime;
}
@Override
public int compareTo(UserMentorPrize o) {
return this.happenday - o.happenday;
}
}