Created
April 12, 2012 21:40
-
-
Save tony-kerz/2371184 to your computer and use it in GitHub Desktop.
json serialization compatible with net.sf.json.JSONObject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.x.y.model; | |
import java.io.IOException; | |
import java.util.Date; | |
import junit.framework.Assert; | |
import net.sf.json.JSONObject; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import com.x.y.model.demographics.Demographics; | |
import com.x.y.quickfind.model.demographics.Gender; | |
import com.x.y.quickfind.model.demographics.State; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration | |
public class MarshallingTest | |
{ | |
@Test | |
public void basic() throws IOException | |
{ | |
Demographics d = new Demographics(); | |
d.setFirstName("fred"); | |
d.setBirthDate(new Date(2012, 6, 6, 6, 6, 6)); | |
d.setGender(Gender.FEMALE); | |
d.setState(State.getState("PA")); | |
JSONObject jo = JSONObject.fromObject(d); | |
String json = jo.toString(); | |
// want date as: | |
// {"date":20,"day":3,"hours":11,"minutes":51,"month":4,"seconds":22,"time":-871546117259,"timezoneOffset":240,"year":42} | |
System.out.println(json); | |
JSONObject objDemo = JSONObject.fromObject(json); | |
Demographics demo = (Demographics) JSONObject.toBean(objDemo, Demographics.class); | |
Assert.assertEquals(d.getFirstName(), demo.getFirstName()); | |
System.out.println("expected=" + d.getBirthDate() + ", actual=" + demo.getBirthDate()); | |
Assert.assertEquals(d.getBirthDate(), demo.getBirthDate()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment