11package fi .hsl .common .hfp ;
22
3- import com .dslplatform .json .*;
3+ import com .fasterxml .jackson .annotation .JsonAlias ;
4+ import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
5+ import com .fasterxml .jackson .annotation .JsonProperty ;
6+ import com .fasterxml .jackson .core .JsonGenerator ;
7+ import com .fasterxml .jackson .core .JsonParser ;
8+ import com .fasterxml .jackson .core .JsonProcessingException ;
9+ import com .fasterxml .jackson .databind .DeserializationContext ;
10+ import com .fasterxml .jackson .databind .JsonDeserializer ;
11+ import com .fasterxml .jackson .databind .JsonSerializer ;
12+ import com .fasterxml .jackson .databind .SerializerProvider ;
13+ import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
14+ import com .fasterxml .jackson .databind .annotation .JsonSerialize ;
415
516import java .io .IOException ;
617
7- // ignore unknown properties (default for objects).
8- // to disallow unknown properties in JSON set it to FAIL which will result in exception instead
9- @ CompiledJson (onUnknown = CompiledJson .Behavior .IGNORE )
18+ @ JsonIgnoreProperties (ignoreUnknown = true )
1019public class HfpJson {
1120 //Specification: https://digitransit.fi/en/developers/apis/4-realtime-api/vehicle-positions/
1221 //Example payload:
1322 // {"VP":{"desi":"81","dir":"2","oper":22,"veh":792,"tst":"2018-04-05T17:38:36Z","tsi":1522949916,"spd":0.16,"hdg":225,"lat":60.194481,"long":25.03095,"acc":0,"dl":-25,"odo":2819,"drst":0,"oday":"2018-04-05","jrn":636,"line":112,"start":"20:25"}}
1423
15- @ JsonAttribute (nullable = false , name = "VP" , alternativeNames = {"DUE" , "ARR" , "DEP" , "ARS" , "PDE" , "PAS" , "WAIT" ,
16- "DOO" , "DOC" , "TLR" , "TLA" , "DA" , "DOUT" , "BA" , "BOUT" , "VJA" , "VJOUT" })
24+ @ JsonProperty ("VP" )
25+ @ JsonAlias ({"DUE" , "ARR" , "DEP" , "ARS" , "PDE" , "PAS" , "WAIT" ,
26+ "DOO" , "DOC" , "TLR" , "TLA" , "DA" , "DOUT" , "BA" , "BOUT" ,
27+ "VJA" , "VJOUT" })
1728 public Payload payload ;
1829
19- @ CompiledJson ( onUnknown = CompiledJson . Behavior . IGNORE )
30+ @ JsonIgnoreProperties ( ignoreUnknown = true )
2031 public static class Payload {
2132
2233 public String desi ;
@@ -27,10 +38,10 @@ public static class Payload {
2738
2839 public Integer veh ;
2940
30- @ JsonAttribute ( nullable = false )
41+ @ JsonProperty ( required = true )
3142 public String tst ;
3243
33- @ JsonAttribute ( nullable = false )
44+ @ JsonProperty ( required = true )
3445 public long tsi ;
3546
3647 public Double spd ;
@@ -39,14 +50,15 @@ public static class Payload {
3950
4051 public Double lat ;
4152
42- @ JsonAttribute ( name = "long" ) //use alternative name in JSON
53+ @ JsonProperty ( "long" ) //use alternative name in JSON
4354 public Double longitude ;
4455
4556 public Double acc ;
4657
4758 public Integer dl ;
4859
49- @ JsonAttribute (converter = Odo .class )
60+ @ JsonDeserialize (using = OdoDeserializer .class )
61+ @ JsonSerialize (using = OdoSerializer .class )
5062 public Double odo ;
5163
5264 public Integer drst ;
@@ -73,65 +85,70 @@ public static class Payload {
7385
7486 public String ttdep ;
7587
76- @ JsonAttribute ( name = "dr-type" ) //use alternative name in JSON
88+ @ JsonProperty ( "dr-type" ) //use alternative name in JSON
7789 public Integer dr_type ;
7890
79- @ JsonAttribute ( name = "tlp-requestid" ) //use alternative name in JSON
91+ @ JsonProperty ( "tlp-requestid" )
8092 public Integer tlp_requestid ;
8193
82- @ JsonAttribute ( name = "tlp-requesttype" ) //use alternative name in JSON
94+ @ JsonProperty ( "tlp-requesttype" )
8395 public String tlp_requesttype ;
8496
85- @ JsonAttribute ( name = "tlp-prioritylevel" ) //use alternative name in JSON
97+ @ JsonProperty ( "tlp-prioritylevel" )
8698 public String tlp_prioritylevel ;
8799
88- @ JsonAttribute ( name = "tlp-reason" ) //use alternative name in JSON
100+ @ JsonProperty ( "tlp-reason" )
89101 public String tlp_reason ;
90102
91- @ JsonAttribute ( name = "tlp-att-seq" ) //use alternative name in JSON
103+ @ JsonProperty ( "tlp-att-seq" )
92104 public Integer tlp_att_seq ;
93105
94- @ JsonAttribute ( name = "tlp-decision" ) //use alternative name in JSON
106+ @ JsonProperty ( "tlp-decision" )
95107 public String tlp_decision ;
96108
97109 public Integer sid ;
98110
99- @ JsonAttribute ( name = "signal-groupid" ) //use alternative name in JSON
111+ @ JsonProperty ( "signal-groupid" )
100112 public Integer signal_groupid ;
101113
102- @ JsonAttribute ( name = "tlp-signalgroupnbr" ) //use alternative name in JSON
114+ @ JsonProperty ( "tlp-signalgroupnbr" )
103115 public Integer tlp_signalgroupnbr ;
104116
105- @ JsonAttribute ( name = "tlp-line-configid" ) //use alternative name in JSON
117+ @ JsonProperty ( "tlp-line-configid" )
106118 public Integer tlp_line_configid ;
107119
108- @ JsonAttribute ( name = "tlp-point-configid" ) //use alternative name in JSON
120+ @ JsonProperty ( "tlp-point-configid" )
109121 public Integer tlp_point_configid ;
110122
111- @ JsonAttribute ( name = "tlp-frequency" ) //use alternative name in JSON
123+ @ JsonProperty ( "tlp-frequency" )
112124 public Integer tlp_frequency ;
113125
114- @ JsonAttribute ( name = "tlp-protocol" ) //use alternative name in JSON
126+ @ JsonProperty ( "tlp-protocol" )
115127 public String tlp_protocol ;
116128
117129 public String label ;
118130 }
119131
120- public static abstract class Odo {
121- public static final JsonReader .ReadObject <Double > JSON_READER = new JsonReader .ReadObject <Double >() {
122- public Double read (JsonReader reader ) throws IOException {
123- return reader .wasNull () ? null : NumberConverter .deserializeDouble (reader );
132+ public static class OdoDeserializer extends JsonDeserializer <Double > {
133+ @ Override
134+ public Double deserialize (JsonParser p , DeserializationContext ctxt )
135+ throws IOException , JsonProcessingException {
136+ if (p .currentToken ().isNumeric ()) {
137+ return p .getDoubleValue ();
124138 }
125- };
126-
127- public static final JsonWriter .WriteObject <Double > JSON_WRITER = new JsonWriter .WriteObject <Double >() {
128- public void write (JsonWriter writer , Double value ) {
129- if (value == null )
130- writer .writeNull ();
131- else
132- NumberConverter .serializeNullable (value .intValue (), writer );
133- }
134- };
139+ return null ;
140+ }
135141 }
136142
143+ public static class OdoSerializer extends JsonSerializer <Double > {
144+ @ Override
145+ public void serialize (Double value , JsonGenerator gen , SerializerProvider serializers )
146+ throws IOException {
147+ if (value == null ) {
148+ gen .writeNull ();
149+ } else {
150+ gen .writeNumber (value .intValue ());
151+ }
152+ }
153+ }
137154}
0 commit comments