This commit is contained in:
RedKale
2016-02-22 16:35:11 +08:00
parent 850fd04eb7
commit 23445de9c4
5 changed files with 612 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
/*
* 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.*;
/**
*
* @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 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;
}
}

View File

@@ -0,0 +1,117 @@
/*
* 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.media;
/**
*
* @author redkale
*/
public class Image implements java.io.Serializable {
private static final long serialVersionUID = 1L;
public enum Size {
SMALL, LARGE
}
private String uri;
private String title; // Can be null
private int width;
private int height;
private Size size;
public Image() {
}
public Image(String uri, String title, int width, int height, Size size) {
this.height = height;
this.title = title;
this.uri = uri;
this.width = width;
this.size = size;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Image image = (Image) o;
if (height != image.height) return false;
if (width != image.width) return false;
if (size != image.size) return false;
if (title != null ? !title.equals(image.title) : image.title != null) return false;
return !(uri != null ? !uri.equals(image.uri) : image.uri != null);
}
@Override
public int hashCode() {
int result = uri != null ? uri.hashCode() : 0;
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + width;
result = 31 * result + height;
result = 31 * result + (size != null ? size.hashCode() : 0);
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[Image ");
sb.append("uri=").append((uri));
sb.append(", title=").append((title));
sb.append(", width=").append(width);
sb.append(", height=").append(height);
sb.append(", size=").append(size);
sb.append("]");
return sb.toString();
}
public void setUri(String uri) {
this.uri = uri;
}
public void setTitle(String title) {
this.title = title;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setSize(Size size) {
this.size = size;
}
public String getUri() {
return uri;
}
public String getTitle() {
return title;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Size getSize() {
return size;
}
}

View File

@@ -0,0 +1,78 @@
/*
* 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.media;
import com.alibaba.fastjson.*;
import com.google.gson.*;
import org.redkale.convert.json.*;
/**
*
* @author redkale
*/
public class JsonTestMain {
public static void main(String[] args) throws Exception {
final MediaContent entry = MediaContent.createDefault();
final JsonConvert convert = JsonFactory.root().getConvert();
final String entryString = convert.convertTo(entry);
convert.convertFrom(MediaContent.class, entryString);
System.out.println("redkale-convert: " + convert.convertTo(entry));
JSON.parseObject(entryString, MediaContent.class);
System.out.println("fastjson 1.2.7: " + JSON.toJSONString(entry));
final Gson gson = new Gson();
gson.fromJson(entryString, MediaContent.class);
System.out.println("google-gson 2.4: " + gson.toJson(entry));
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);
s = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
convert.convertFrom(MediaContent.class, entryString);
}
e = System.currentTimeMillis() - s;
System.out.println("\t " + e);
//----------------------------------------------------------------------------
s = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
JSON.toJSONString(entry);
}
e = System.currentTimeMillis() - s;
System.out.print("fastjson 1.2.7 " + e);
s = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
JSON.parseObject(entryString, MediaContent.class);
}
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);
s = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
gson.fromJson(entryString, MediaContent.class);
}
e = System.currentTimeMillis() - s;
System.out.println("\t " + e);
//----------------------------------------------------------------------------
}
}

View File

@@ -0,0 +1,203 @@
/*
* 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.media;
import java.util.*;
/**
*
* @author redkale
*/
public class Media implements java.io.Serializable {
public enum Player {
JAVA, FLASH;
}
private String uri;
private String title; // Can be unset.
private int width;
private int height;
private String format;
private long duration;
private long size;
private int bitrate; // Can be unset.
private List<String> persons;
private Player player;
private String copyright; // Can be unset.
public Media() {
}
public Media(String uri, String title, int width, int height, String format, long duration, long size,
int bitrate, boolean hasBitrate, List<String> persons, Player player, String copyright) {
this.uri = uri;
this.title = title;
this.width = width;
this.height = height;
this.format = format;
this.duration = duration;
this.size = size;
this.bitrate = bitrate;
this.persons = persons;
this.player = player;
this.copyright = copyright;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Media media = (Media) o;
if (bitrate != media.bitrate) return false;
if (duration != media.duration) return false;
if (height != media.height) return false;
if (size != media.size) return false;
if (width != media.width) return false;
if (copyright != null ? !copyright.equals(media.copyright) : media.copyright != null) return false;
if (format != null ? !format.equals(media.format) : media.format != null) return false;
if (persons != null ? !persons.equals(media.persons) : media.persons != null) return false;
if (player != media.player) return false;
if (title != null ? !title.equals(media.title) : media.title != null) return false;
if (uri != null ? !uri.equals(media.uri) : media.uri != null) return false;
return true;
}
@Override
public int hashCode() {
int result = uri != null ? uri.hashCode() : 0;
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + width;
result = 31 * result + height;
result = 31 * result + (format != null ? format.hashCode() : 0);
result = 31 * result + (int) (duration ^ (duration >>> 32));
result = 31 * result + (int) (size ^ (size >>> 32));
result = 31 * result + bitrate;
result = 31 * result + (persons != null ? persons.hashCode() : 0);
result = 31 * result + (player != null ? player.hashCode() : 0);
result = 31 * result + (copyright != null ? copyright.hashCode() : 0);
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[Media ");
sb.append("uri=").append((uri));
sb.append(", title=").append((title));
sb.append(", width=").append(width);
sb.append(", height=").append(height);
sb.append(", format=").append((format));
sb.append(", duration=").append(duration);
sb.append(", size=").append(size);
sb.append(", bitrate=").append(String.valueOf(bitrate));
sb.append(", persons=").append((persons));
sb.append(", player=").append(player);
sb.append(", copyright=").append((copyright));
sb.append("]");
return sb.toString();
}
public void setUri(String uri) {
this.uri = uri;
}
public void setTitle(String title) {
this.title = title;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void setFormat(String format) {
this.format = format;
}
public void setDuration(long duration) {
this.duration = duration;
}
public void setSize(long size) {
this.size = size;
}
public void setBitrate(int bitrate) {
this.bitrate = bitrate;
}
public void setPersons(List<String> persons) {
this.persons = persons;
}
public void setPlayer(Player player) {
this.player = player;
}
public void setCopyright(String copyright) {
this.copyright = copyright;
}
public String getUri() {
return uri;
}
public String getTitle() {
return title;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public String getFormat() {
return format;
}
public long getDuration() {
return duration;
}
public long getSize() {
return size;
}
public int getBitrate() {
return bitrate;
}
public List<String> getPersons() {
return persons;
}
public Player getPlayer() {
return player;
}
public String getCopyright() {
return copyright;
}
}

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.media;
import java.util.*;
import org.redkale.convert.json.*;
/**
*
* @author redkale
*/
public class MediaContent implements java.io.Serializable {
private Media media;
private List<Image> images;
public MediaContent() {
}
public MediaContent(Media media, List<Image> images) {
this.media = media;
this.images = images;
}
public static void main(String[] args) throws Exception {
JsonConvert convert = JsonFactory.root().getConvert();
MediaContent content = MediaContent.createDefault();
System.out.println(content);
}
public static MediaContent createDefault() {
String str = "{"
+ " media : {"
+ " uri : \"http://javaone.com/keynote.mpg\" ,"
+ " title : \"Javaone Keynote\" ,"
+ " width : 640 ,"
+ " height : 480 ,"
+ " format : \"video/mpg4\","
+ " duration : 18000000 ,"
+ " size : 58982400 ,"
+ " bitrate : 262144 ,"
+ " persons : [\"Bill Gates\", \"Steve Jobs\"] ,"
+ " player : JAVA , "
+ " copyright : None"
+ " }, images : ["
+ " {"
+ " uri : \"http://javaone.com/keynote_large.jpg\","
+ " title : \"Javaone Keynote\","
+ " width : 1024,"
+ " height : 768,"
+ " size : LARGE"
+ " }, {"
+ " uri : \"http://javaone.com/keynote_small.jpg\", "
+ " title : \"Javaone Keynote\" , "
+ " width : 320 , "
+ " height : 240 , "
+ " size : SMALL"
+ " }"
+ " ]"
+ "}";
return JsonFactory.root().getConvert().convertFrom(MediaContent.class, str);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MediaContent that = (MediaContent) o;
if (images != null ? !images.equals(that.images) : that.images != null) return false;
return !(media != null ? !media.equals(that.media) : that.media != null);
}
@Override
public int hashCode() {
int result = media != null ? media.hashCode() : 0;
result = 31 * result + (images != null ? images.hashCode() : 0);
return result;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[MediaContent: ");
sb.append("media=").append(media);
sb.append(", images=").append(images);
sb.append("]");
return sb.toString();
}
public void setMedia(Media media) {
this.media = media;
}
public void setImages(List<Image> images) {
this.images = images;
}
public Media getMedia() {
return media;
}
public List<Image> getImages() {
return images;
}
}