惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - Jeason

Falling slowly 暴雪还不赶快?劳拉与光之守护者PC平台登陆 欢迎来看美女Nikita 近期的几个参考备注 什么是效率和气场:保卫人民币 Krypton Suite 4.3 Released JAVA默认构造函数和可变参数构造函数之间的区别 JQuery 备注 Eclipse 几点备要 所谓心如磐石,初见面即见分晓? 失败啊:心如磐石,无可奈何 A short introduction of Feed4JUnit 输入英文的尴尬 SQLReport 项目重新激活 百度这个疯子 无题无感慨 设置XP的自动登录 新域名上线 www.jeasonzhao.com What does a Scrum Master do?
POJO模型类XML和JSON互相转换
Jeason · 2010-09-15 · via 博客园 - Jeason

已知缺陷

  • 不支持嵌套的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"

}