Created
December 1, 2011 02:10
-
-
Save smanek/1412814 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class ImmutableJson { | |
private static final ObjectMapper MAPPER = new ObjectMapper(); | |
public static void main(String[] args) throws IOException { | |
// create a student | |
final Student student = new Student("John Doe", new Date(), true, | |
new Course("Intro to Typing", Department.COMPUTER_SCIENCE, 101), | |
new Course("Postmodern Deconstructionism", Department.ENGLISH, 201), | |
new Course("Remedial Galois Field Arithmetic", Department.MATH, 51)); | |
System.out.printf("The original student object is: '%s'%n", student); | |
// serialize the student to a JSON object | |
final String serialized = MAPPER.writeValueAsString(student); | |
System.out.printf("The serialized student is: '%s'%n", serialized); | |
// parse the JSON back into a Java object | |
final Student deserializedStudent = MAPPER.readValue(serialized, Student.class); | |
System.out.printf("The deserialized student object is: '%s'%n", deserializedStudent); | |
} | |
public static enum Department { | |
COMPUTER_SCIENCE("CS"), | |
ENGLISH("ENG"), | |
MATH("MATH"); | |
private final String shortName; | |
private Department(String shortName) { | |
this.shortName = shortName; | |
} | |
public String getShortName() { | |
return shortName; | |
} | |
} | |
public static class Course { | |
private final Department department; | |
private final String vanityName; | |
private final long courseNumber; | |
@JsonCreator | |
public Course(@JsonProperty("vanityName") String vanityName, | |
@JsonProperty("department") Department department, | |
@JsonProperty("courseNumber") long courseNumber) { | |
this.vanityName = vanityName; | |
this.department = department; | |
this.courseNumber = courseNumber; | |
} | |
public String getVanityName() { | |
return vanityName; | |
} | |
public long getCourseNumber() { | |
return courseNumber; | |
} | |
public Department getDepartment() { | |
return department; | |
} | |
} | |
public static class Student { | |
private final String name; | |
private final Date dateOfBirth; | |
private final boolean enrolled; | |
private final List<Course> courses; | |
@JsonCreator | |
public Student(@JsonProperty("name") String name, | |
@JsonProperty("dateOfBirth") Date dateOfBirth, | |
@JsonProperty("enrolled") boolean enrolled, | |
@JsonProperty("courses") Course ... courses) { | |
this.name = name; | |
this.dateOfBirth = dateOfBirth; | |
this.enrolled = enrolled; | |
this.courses = Collections.unmodifiableList(Arrays.asList(courses)); | |
} | |
public String getName() { | |
return name; | |
} | |
public Date getDateOfBirth() { | |
return dateOfBirth; | |
} | |
public boolean isEnrolled() { | |
return enrolled; | |
} | |
public List<Course> getCourses() { | |
return courses; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment