1414 */
1515package com .jayway .jsonpath .spi .json ;
1616
17+ import java .io .InputStream ;
18+ import java .io .InputStreamReader ;
19+ import java .io .UnsupportedEncodingException ;
20+
21+ import java .math .BigDecimal ;
22+
23+ import java .util .ArrayList ;
24+ import java .util .Collection ;
25+ import java .util .List ;
26+ import java .util .Map ;
27+
1728import com .google .gson .Gson ;
18- import com .google .gson .GsonBuilder ;
1929import com .google .gson .JsonArray ;
2030import com .google .gson .JsonElement ;
2131import com .google .gson .JsonObject ;
2232import com .google .gson .JsonParser ;
2333import com .google .gson .JsonPrimitive ;
2434import com .google .gson .internal .LazilyParsedNumber ;
35+
2536import com .jayway .jsonpath .InvalidJsonException ;
2637import com .jayway .jsonpath .JsonPathException ;
2738
28- import java .io .InputStream ;
29- import java .io .InputStreamReader ;
30- import java .io .UnsupportedEncodingException ;
31- import java .math .BigDecimal ;
32- import java .util .ArrayList ;
33- import java .util .Collection ;
34- import java .util .List ;
35- import java .util .Map ;
36-
3739public class GsonJsonProvider extends AbstractJsonProvider {
3840
39- private static final JsonParser parser = new JsonParser ();
40- private static final Gson gson = new GsonBuilder (). create () ;
41+ private static final JsonParser PARSER = new JsonParser ();
42+ private final Gson gson ;
4143
44+ /**
45+ * Initializes the {@code GsonJsonProvider} using the default {@link Gson} object.
46+ */
47+ public GsonJsonProvider () {
48+ this (new Gson ());
49+ }
50+
51+ /**
52+ * Initializes the {@code GsonJsonProvider} using a customized {@link Gson} object.
53+ *
54+ * @param gson the customized Gson object.
55+ */
56+ public GsonJsonProvider (final Gson gson ) {
57+ this .gson = gson ;
58+ }
4259
43- public Object unwrap (Object o ) {
60+ public Object unwrap (final Object o ) {
4461
4562 if (o == null ) {
4663 return null ;
4764 }
65+
4866 if (!(o instanceof JsonElement )) {
4967 return o ;
5068 }
@@ -64,10 +82,11 @@ public Object unwrap(Object o) {
6482 return unwrapNumber (p .getAsNumber ());
6583 }
6684 }
85+
6786 return o ;
6887 }
6988
70- private static Number unwrapNumber (Number n ) {
89+ private static Number unwrapNumber (final Number n ) {
7190 Number unwrapped ;
7291
7392 if (n instanceof LazilyParsedNumber ) {
@@ -85,27 +104,28 @@ private static Number unwrapNumber(Number n) {
85104 } else {
86105 unwrapped = n ;
87106 }
107+
88108 return unwrapped ;
89109 }
90110
91111 @ Override
92- public Object parse (String json ) throws InvalidJsonException {
93- return parser .parse (json );
112+ public Object parse (final String json ) throws InvalidJsonException {
113+ return PARSER .parse (json );
94114 }
95115
96116 @ Override
97- public Object parse (InputStream jsonStream , String charset ) throws InvalidJsonException {
117+ public Object parse (final InputStream jsonStream , final String charset ) throws InvalidJsonException {
98118
99119 try {
100- return parser .parse (new InputStreamReader (jsonStream , charset ));
120+ return PARSER .parse (new InputStreamReader (jsonStream , charset ));
101121 } catch (UnsupportedEncodingException e ) {
102122 throw new JsonPathException (e );
103123 }
104124 }
105125
106126 @ Override
107- public String toJson (Object obj ) {
108- return obj . toString ( );
127+ public String toJson (final Object obj ) {
128+ return gson . toJson ( obj );
109129 }
110130
111131 @ Override
@@ -119,31 +139,31 @@ public Object createMap() {
119139 }
120140
121141 @ Override
122- public boolean isArray (Object obj ) {
142+ public boolean isArray (final Object obj ) {
123143 return (obj instanceof JsonArray || obj instanceof List );
124144 }
125145
126146 @ Override
127- public Object getArrayIndex (Object obj , int idx ) {
147+ public Object getArrayIndex (final Object obj , final int idx ) {
128148 return toJsonArray (obj ).get (idx );
129149 }
130150
131151 @ Override
132- public void setArrayIndex (Object array , int index , Object newValue ) {
152+ public void setArrayIndex (final Object array , final int index , final Object newValue ) {
133153 if (!isArray (array )) {
134154 throw new UnsupportedOperationException ();
135155 } else {
136156 JsonArray arr = toJsonArray (array );
137- if (index == arr .size ()){
157+ if (index == arr .size ()) {
138158 arr .add (createJsonElement (newValue ));
139- }else {
159+ } else {
140160 arr .set (index , createJsonElement (newValue ));
141161 }
142162 }
143163 }
144164
145165 @ Override
146- public Object getMapValue (Object obj , String key ) {
166+ public Object getMapValue (final Object obj , final String key ) {
147167 JsonObject jsonObject = toJsonObject (obj );
148168 Object o = jsonObject .get (key );
149169 if (!jsonObject .has (key )) {
@@ -154,17 +174,18 @@ public Object getMapValue(Object obj, String key) {
154174 }
155175
156176 @ Override
157- public void setProperty (Object obj , Object key , Object value ) {
158- if (isMap (obj ))
177+ public void setProperty (final Object obj , final Object key , final Object value ) {
178+ if (isMap (obj )) {
159179 toJsonObject (obj ).add (key .toString (), createJsonElement (value ));
160- else {
180+ } else {
161181 JsonArray array = toJsonArray (obj );
162182 int index ;
163183 if (key != null ) {
164184 index = key instanceof Integer ? (Integer ) key : Integer .parseInt (key .toString ());
165185 } else {
166186 index = array .size ();
167187 }
188+
168189 if (index == array .size ()) {
169190 array .add (createJsonElement (value ));
170191 } else {
@@ -173,36 +194,36 @@ public void setProperty(Object obj, Object key, Object value) {
173194 }
174195 }
175196
176-
177-
178197 @ SuppressWarnings ("unchecked" )
179- public void removeProperty (Object obj , Object key ) {
180- if (isMap (obj ))
198+ public void removeProperty (final Object obj , final Object key ) {
199+ if (isMap (obj )) {
181200 toJsonObject (obj ).remove (key .toString ());
182- else {
201+ } else {
183202 JsonArray array = toJsonArray (obj );
184203 int index = key instanceof Integer ? (Integer ) key : Integer .parseInt (key .toString ());
185204 array .remove (index );
186205 }
187206 }
188207
189208 @ Override
190- public boolean isMap (Object obj ) {
191- //return (obj instanceof JsonObject || obj instanceof Map);
209+ public boolean isMap (final Object obj ) {
210+
211+ // return (obj instanceof JsonObject || obj instanceof Map);
192212 return (obj instanceof JsonObject );
193213 }
194214
195215 @ Override
196- public Collection <String > getPropertyKeys (Object obj ) {
216+ public Collection <String > getPropertyKeys (final Object obj ) {
197217 List <String > keys = new ArrayList <String >();
198218 for (Map .Entry <String , JsonElement > entry : toJsonObject (obj ).entrySet ()) {
199219 keys .add (entry .getKey ());
200220 }
221+
201222 return keys ;
202223 }
203224
204225 @ Override
205- public int length (Object obj ) {
226+ public int length (final Object obj ) {
206227 if (isArray (obj )) {
207228 return toJsonArray (obj ).size ();
208229 } else if (isMap (obj )) {
@@ -215,32 +236,35 @@ public int length(Object obj) {
215236 }
216237 }
217238 }
218- throw new JsonPathException ("length operation can not applied to " + obj != null ? obj .getClass ().getName () : "null" );
239+
240+ throw new JsonPathException ("length operation can not applied to " + obj != null ? obj .getClass ().getName ()
241+ : "null" );
219242 }
220243
221244 @ Override
222- public Iterable <?> toIterable (Object obj ) {
245+ public Iterable <?> toIterable (final Object obj ) {
223246 JsonArray arr = toJsonArray (obj );
224247 List <Object > values = new ArrayList <Object >(arr .size ());
225248 for (Object o : arr ) {
226249 values .add (unwrap (o ));
227250 }
251+
228252 return values ;
229253 }
230254
231- private JsonElement createJsonElement (Object o ) {
255+ private JsonElement createJsonElement (final Object o ) {
232256 return gson .toJsonTree (o );
233257 }
234258
235- private JsonArray toJsonArray (Object o ) {
259+ private JsonArray toJsonArray (final Object o ) {
236260 return (JsonArray ) o ;
237261 }
238262
239- private JsonObject toJsonObject (Object o ) {
263+ private JsonObject toJsonObject (final Object o ) {
240264 return (JsonObject ) o ;
241265 }
242266
243- private JsonElement toJsonElement (Object o ) {
267+ private JsonElement toJsonElement (final Object o ) {
244268 return (JsonElement ) o ;
245269 }
246270}
0 commit comments