11package com .fasterxml .jackson .datatype .guava .deser ;
22
3+ import static java .util .Arrays .asList ;
4+
35import java .io .IOException ;
4- import java .util .Arrays ;
56
67import com .google .common .base .Preconditions ;
78import com .google .common .collect .BoundType ;
1516import com .fasterxml .jackson .databind .type .TypeFactory ;
1617
1718import com .fasterxml .jackson .datatype .guava .deser .util .RangeFactory ;
19+ import java .util .HashMap ;
20+ import java .util .Map ;
1821
1922/**
2023 * Jackson deserializer for a Guava {@link Range}.
@@ -33,6 +36,8 @@ public class RangeDeserializer
3336
3437 protected final JsonDeserializer <Object > _endpointDeserializer ;
3538
39+ private final Map <String , String > _fieldNames ;
40+
3641 private BoundType _defaultBoundType ;
3742
3843 /*
@@ -48,27 +53,35 @@ public class RangeDeserializer
4853 public RangeDeserializer (JavaType rangeType ) {
4954 this (null , rangeType );
5055 }
51-
56+
5257 public RangeDeserializer (BoundType defaultBoundType , JavaType rangeType ) {
53- this (rangeType , null );
58+ this (rangeType , null , new HashMap <>() );
5459 _defaultBoundType = defaultBoundType ;
5560 }
5661
5762 @ SuppressWarnings ("unchecked" )
58- public RangeDeserializer (JavaType rangeType , JsonDeserializer <?> endpointDeser )
63+ public RangeDeserializer (JavaType rangeType , JsonDeserializer <?> endpointDeser , Map < String , String > fieldNames )
5964 {
6065 super (rangeType );
6166 _rangeType = rangeType ;
6267 _endpointDeserializer = (JsonDeserializer <Object >) endpointDeser ;
68+ _fieldNames = fieldNames ;
6369 }
6470
6571 @ SuppressWarnings ("unchecked" )
6672 public RangeDeserializer (JavaType rangeType , JsonDeserializer <?> endpointDeser , BoundType defaultBoundType )
73+ {
74+ this (rangeType , endpointDeser , defaultBoundType , new HashMap <>());
75+ }
76+
77+ @ SuppressWarnings ("unchecked" )
78+ public RangeDeserializer (JavaType rangeType , JsonDeserializer <?> endpointDeser , BoundType defaultBoundType , Map <String , String > fieldNames )
6779 {
6880 super (rangeType );
6981 _rangeType = rangeType ;
7082 _endpointDeserializer = (JsonDeserializer <Object >) endpointDeser ;
7183 _defaultBoundType = defaultBoundType ;
84+ _fieldNames = fieldNames ;
7285 }
7386
7487 @ Override
@@ -78,23 +91,40 @@ public RangeDeserializer(JavaType rangeType, JsonDeserializer<?> endpointDeser,
7891 public JsonDeserializer <?> createContextual (DeserializationContext ctxt ,
7992 BeanProperty property ) throws JsonMappingException
8093 {
94+ PropertyNamingStrategy propertyNamingStrategy = ctxt .getConfig ().getPropertyNamingStrategy ();
8195 if (_endpointDeserializer == null ) {
8296 JavaType endpointType = _rangeType .containedType (0 );
8397 if (endpointType == null ) { // should this ever occur?
8498 endpointType = TypeFactory .unknownType ();
8599 }
86100 JsonDeserializer <Object > deser = ctxt .findContextualValueDeserializer (endpointType , property );
87- return new RangeDeserializer (_rangeType , deser , _defaultBoundType );
101+ if (propertyNamingStrategy != null ) {
102+ return new RangeDeserializer (_rangeType , deser , _defaultBoundType , getPropertyNames (ctxt , propertyNamingStrategy ));
103+ } else {
104+ return new RangeDeserializer (_rangeType , deser , _defaultBoundType );
105+ }
106+ }
107+ else if (propertyNamingStrategy != null ) {
108+ return new RangeDeserializer (_rangeType , _endpointDeserializer , _defaultBoundType , getPropertyNames (ctxt , propertyNamingStrategy ));
88109 }
89110 return this ;
90111 }
91112
113+ private Map <String , String > getPropertyNames (DeserializationContext ctxt , PropertyNamingStrategy propertyNamingStrategy ) {
114+ DeserializationConfig config = ctxt .getConfig ();
115+ HashMap <String , String > fieldNames = new HashMap <>();
116+ for (String field : asList ("lowerEndpoint" , "upperEndpoint" , "lowerBoundType" , "upperBoundType" )) {
117+ fieldNames .put (field , propertyNamingStrategy .nameForField (config , null , field ));
118+ }
119+ return fieldNames ;
120+ }
121+
92122 /*
93123 /**********************************************************
94124 /* Actual deserialization
95125 /**********************************************************
96126 */
97-
127+
98128 @ Override
99129 public Object deserializeWithType (JsonParser jp , DeserializationContext ctxt ,
100130 TypeDeserializer typeDeserializer )
@@ -122,16 +152,16 @@ public Range<?> deserialize(JsonParser p, DeserializationContext context)
122152 expect (context , JsonToken .FIELD_NAME , t );
123153 String fieldName = p .currentName ();
124154 try {
125- if (fieldName .equals ("lowerEndpoint" )) {
155+ if (fieldName .equals (getFieldName ( "lowerEndpoint" ) )) {
126156 p .nextToken ();
127157 lowerEndpoint = deserializeEndpoint (context , p );
128- } else if (fieldName .equals ("upperEndpoint" )) {
158+ } else if (fieldName .equals (getFieldName ( "upperEndpoint" ) )) {
129159 p .nextToken ();
130160 upperEndpoint = deserializeEndpoint (context , p );
131- } else if (fieldName .equals ("lowerBoundType" )) {
161+ } else if (fieldName .equals (getFieldName ( "lowerBoundType" ) )) {
132162 p .nextToken ();
133163 lowerBoundType = deserializeBoundType (context , p );
134- } else if (fieldName .equals ("upperBoundType" )) {
164+ } else if (fieldName .equals (getFieldName ( "upperBoundType" ) )) {
135165 p .nextToken ();
136166 upperBoundType = deserializeBoundType (context , p );
137167 } else {
@@ -148,19 +178,32 @@ public Range<?> deserialize(JsonParser p, DeserializationContext context)
148178 try {
149179 if ((lowerEndpoint != null ) && (upperEndpoint != null )) {
150180 Preconditions .checkState (lowerEndpoint .getClass () == upperEndpoint .getClass (),
151- "Endpoint types are not the same - 'lowerEndpoint' deserialized to [%s], and 'upperEndpoint' deserialized to [%s]." ,
181+ "Endpoint types are not the same - '%s' deserialized to [%s], and '%s' deserialized to [%s]." ,
182+ getFieldName ("lowerEndpoint" ),
152183 lowerEndpoint .getClass ().getName (),
184+ getFieldName ("upperEndpoint" ),
153185 upperEndpoint .getClass ().getName ());
154- Preconditions .checkState (lowerBoundType != null , "'lowerEndpoint' field found, but not 'lowerBoundType'" );
155- Preconditions .checkState (upperBoundType != null , "'upperEndpoint' field found, but not 'upperBoundType'" );
186+ Preconditions .checkState (lowerBoundType != null ,
187+ "'%s' field found, but not '%s'" ,
188+ getFieldName ("lowerEndpoint" ),
189+ getFieldName ("lowerBoundType" ));
190+ Preconditions .checkState (upperBoundType != null ,
191+ "'%s' field found, but not '%s'" ,
192+ getFieldName ("upperEndpoint" ),
193+ getFieldName ("upperBoundType" ));
156194 return RangeFactory .range (lowerEndpoint , lowerBoundType , upperEndpoint , upperBoundType );
157195 }
158196 if (lowerEndpoint != null ) {
159- Preconditions .checkState (lowerBoundType != null , "'lowerEndpoint' field found, but not 'lowerBoundType'" );
197+ Preconditions .checkState (lowerBoundType != null ,
198+ "'%s' field found, but not '%s'" ,
199+ getFieldName ("lowerEndpoint" ),
200+ getFieldName ("lowerBoundType" ));
160201 return RangeFactory .downTo (lowerEndpoint , lowerBoundType );
161202 }
162203 if (upperEndpoint != null ) {
163- Preconditions .checkState (upperBoundType != null , "'upperEndpoint' field found, but not 'upperBoundType'" );
204+ Preconditions .checkState (upperBoundType != null ,
205+ "'%s' field found, but not '%s'" ,
206+ getFieldName ("lowerEndpoint" ));
164207 return RangeFactory .upTo (upperEndpoint , upperBoundType );
165208 }
166209 return RangeFactory .all ();
@@ -169,6 +212,11 @@ public Range<?> deserialize(JsonParser p, DeserializationContext context)
169212 }
170213 }
171214
215+ private String getFieldName (String fieldName ) {
216+ String name = _fieldNames .get (fieldName );
217+ return name == null ? fieldName : name ;
218+ }
219+
172220 private BoundType deserializeBoundType (DeserializationContext context , JsonParser p ) throws IOException
173221 {
174222 expect (context , JsonToken .VALUE_STRING , p .currentToken ());
@@ -178,7 +226,7 @@ private BoundType deserializeBoundType(DeserializationContext context, JsonParse
178226 } catch (IllegalArgumentException e ) {
179227 return (BoundType ) context .handleWeirdStringValue (BoundType .class , name ,
180228 "not a valid BoundType name (should be one oF: %s)" ,
181- Arrays . asList (BoundType .values ()));
229+ asList (BoundType .values ()));
182230 }
183231 }
184232
0 commit comments