Created
December 1, 2011 07:13
-
-
Save smanek/1414547 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
@JsonSerialize(using = CourseSerializer.class) | |
@JsonDeserialize(using = CourseDeserializer.class) | |
public static class Course { | |
private final Department department; | |
private final long courseNumber; | |
public Course(Department department, long courseNumber) { | |
assert department != null; | |
this.department = department; | |
this.courseNumber = courseNumber; | |
} | |
public long getCourseNumber() { | |
return courseNumber; | |
} | |
public Department getDepartment() { | |
return department; | |
} | |
} | |
public static class CourseSerializer extends JsonSerializer<Course> { | |
@Override | |
public void serialize(Course course, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) | |
throws IOException, JsonProcessingException { | |
jsonGenerator.writeString(course.getDepartment().getShortName() + "-" + course.getCourseNumber()); | |
} | |
} | |
public static class CourseDeserializer extends JsonDeserializer<Course> { | |
@Override | |
public Course deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) | |
throws IOException, JsonProcessingException { | |
final String courseParts[] = jsonParser.getText().split("-", 2); | |
assert courseParts.length == 2; | |
final Department department = Department.fromShortName(courseParts[0]); | |
assert department != null; | |
final long courseNumber = Long.parseLong(courseParts[1]); | |
return new Course(department, courseNumber); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment