Created
June 9, 2016 12:00
-
-
Save SriramKeerthi/37fab427c8d94a9929a23976ff6506cd to your computer and use it in GitHub Desktop.
Typed JSON with Jackson example
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.caffinc.learning.json; | |
import com.fasterxml.jackson.annotation.JsonSubTypes; | |
import com.fasterxml.jackson.annotation.JsonTypeInfo; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Test { | |
static class ScheduleList { | |
public List<Schedule> schedules; | |
} | |
public static void main(String[] args) throws Exception { | |
ScheduleList schedules = new ScheduleList(); | |
schedules.schedules = new ArrayList<Schedule>(); | |
schedules.schedules.add(new HourlySchedule()); | |
schedules.schedules.add(new DailySchedule()); | |
ObjectMapper objectMapper = new ObjectMapper(); | |
System.out.println(objectMapper.writeValueAsString(schedules)); | |
String dummySchedule = "{\n" + | |
" \"schedules\": [\n" + | |
" {\n" + | |
" \"type\": \"hourly\",\n" + | |
" \"lastRunTime\": \"DUMMYTIME\",\n" + | |
" \"nextRunTime\": \"MOREDUMMYTIME\",\n" + | |
" \"hour\": \"12\"\n" + | |
" },\n" + | |
" {\n" + | |
" \"type\": \"daily\",\n" + | |
" \"lastRunTime\": \"DUMMYTIME\",\n" + | |
" \"nextRunTime\": \"MOREDUMMYTIME\",\n" + | |
" \"day\": \"1\"\n" + | |
" }\n" + | |
" ]\n" + | |
"}"; | |
ScheduleList parsedSchedule = objectMapper.readValue(dummySchedule, ScheduleList.class); | |
System.out.println(parsedSchedule.schedules.get(0).getClass().toString()); | |
System.out.println(parsedSchedule.schedules.get(1).getClass().toString()); | |
} | |
} | |
@JsonTypeInfo( | |
use = JsonTypeInfo.Id.NAME, | |
include = JsonTypeInfo.As.PROPERTY, | |
property = "type") | |
@JsonSubTypes({ | |
@JsonSubTypes.Type(value = DailySchedule.class, name = "daily"), | |
@JsonSubTypes.Type(value = HourlySchedule.class, name = "hourly") | |
}) | |
abstract class Schedule { | |
private String lastRunTime; | |
private String nextRunTime; | |
public String getLastRunTime() { | |
return lastRunTime; | |
} | |
public void setLastRunTime(String lastRunTime) { | |
this.lastRunTime = lastRunTime; | |
} | |
public String getNextRunTime() { | |
return nextRunTime; | |
} | |
public void setNextRunTime(String nextRunTime) { | |
this.nextRunTime = nextRunTime; | |
} | |
} | |
class HourlySchedule extends Schedule { | |
private String hour; | |
public String getHour() { | |
return hour; | |
} | |
public void setHour(String hour) { | |
this.hour = hour; | |
} | |
@Override | |
public String toString() { | |
return "HourlySchedule{" + | |
"lastRunTime='" + getLastRunTime() + "', " + | |
"nextRunTime='" + getNextRunTime() + "', " + | |
"hour='" + hour + '\'' + | |
'}'; | |
} | |
} | |
class DailySchedule extends Schedule { | |
private String day; | |
public String getDay() { | |
return day; | |
} | |
public void setDay(String day) { | |
this.day = day; | |
} | |
@Override | |
public String toString() { | |
return "DailySchedule{" + | |
"lastRunTime='" + getLastRunTime() + "', " + | |
"nextRunTime='" + getNextRunTime() + "', " + | |
"day='" + day + '\'' + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment