POJO模型类XML和JSON互相转换
已知缺陷
- 不支持嵌套的POJO类
- 不支持列表和数组类型
- 缺陷在新的一个版本中实现。
com.jeasonzhao.model.ModelHelper可以完成全部的工作
测试类
public class TestSimplePojo
{
private String strValue="aaa\"";
private int intValue=111;
private Date dateValue=new Date();
private double doubleValue=3.4333;
private long longValue=55555;
private float floatValue=999.911321f;
private short shortValue=12;
public TestSimplePojo()
{
super();
}
//省略POJO方法
}
public static class Item
{
private String id = null;
private String name = null;
public Item()
{
}
public Item(String i,String n)
{
this.id = i;
this.name = n;
}
//省略POJO方法
}
测试用例
public void testXML()
throws ModelException
{
Item a = new Item("A","NAME OF A");
String xml = ModelHelper.getInstance().toXML(a,false).toXMLString();
Assert.assertNotNull(xml);
System.out.println("----------------------------Original XML----------------------------");
System.out.println(xml);
Item a2 = ModelHelper.getInstance().fromXML(Item.class,xml);
Assert.assertNotNull(a2);
Assert.assertEquals(a.getId(),a2.getId());
Assert.assertEquals(a.getName(),a2.getName());
TestSimplePojo sp = new TestSimplePojo();
xml = ModelHelper.getInstance().toXML(sp,false).toXMLString();
Assert.assertNotNull(xml);
System.out.println("----------------------------Original XML----------------------------");
System.out.println(xml);
TestSimplePojo sp2 = ModelHelper.getInstance().fromXML(TestSimplePojo.class,xml);
Assert.assertNotNull(sp2);
Assert.assertEquals(Algorithms.toString(sp2.getDateValue()),Algorithms.toString(sp.getDateValue()));
Assert.assertEquals(sp2.getIntValue(),sp.getIntValue());
Assert.assertEquals(sp2.getDoubleValue(),sp.getDoubleValue());
Assert.assertEquals(sp2.getLongValue(),sp.getLongValue());
Assert.assertEquals(sp2.getShortValue(),sp.getShortValue());
Assert.assertEquals(sp2.getStrValue(),sp.getStrValue());
Assert.assertEquals(sp2.getFloatValue(),sp.getFloatValue());
}
public void testJSON()
throws ModelException
{
Item a = new Item("A","NAME OF A");
String json = ModelHelper.getInstance().toJSON(a,false).toJSON();
Assert.assertNotNull(json);
System.out.println("----------------------------Original JSON----------------------------");
System.out.println(json);
Item a2 = ModelHelper.getInstance().fromJSON(Item.class,json);
Assert.assertNotNull(a2);
Assert.assertEquals(a.getId(),a2.getId());
Assert.assertEquals(a.getName(),a2.getName());
TestSimplePojo sp = new TestSimplePojo();
json = ModelHelper.getInstance().toJSON(sp,false).toJSON();
Assert.assertNotNull(json);
System.out.println("----------------------------Original JSON----------------------------");
System.out.println(json);
TestSimplePojo sp2 = ModelHelper.getInstance().fromJSON(TestSimplePojo.class,json);
Assert.assertNotNull(sp2);
Assert.assertEquals(Algorithms.toString(sp2.getDateValue()),Algorithms.toString(sp.getDateValue()));
Assert.assertEquals(sp2.getIntValue(),sp.getIntValue());
Assert.assertEquals(sp2.getDoubleValue(),sp.getDoubleValue());
Assert.assertEquals(sp2.getLongValue(),sp.getLongValue());
Assert.assertEquals(sp2.getShortValue(),sp.getShortValue());
Assert.assertEquals(sp2.getStrValue(),sp.getStrValue());
Assert.assertEquals(sp2.getFloatValue(),sp.getFloatValue());
}
测试输出
----------------------------Original XML----------------------------
<Item>
<id>A</id>
<name>NAME OF A</name>
</Item>
----------------------------Original XML----------------------------
<TestSimplePojo>
<dateValue>2010-09-15 00:57:59</dateValue>
<doubleValue>3.4333</doubleValue>
<floatValue>999.9113159179688</floatValue>
<intValue>111</intValue>
<longValue>55555</longValue>
<shortValue>12</shortValue>
<strValue>aaa"</strValue>
</TestSimplePojo>
----------------------------Original JSON----------------------------
{
name:"NAME OF A"
,id:"A"
}
----------------------------Original JSON----------------------------
{
shortValue:"12"
,dateValue:"2010-09-15 00:57:59"
,intValue:"111"
,strValue:"aaa\""
,longValue:"55555"
,floatValue:"999.9113159179688"
,doubleValue:"3.4333"
}