18
18
19
19
import java .text .SimpleDateFormat ;
20
20
21
- import org . apache . commons . logging . Log ;
22
- import org . apache . commons . logging . LogFactory ;
23
- import org . springframework . beans . factory . BeanClassLoaderAware ;
21
+ import com . google . gson . Gson ;
22
+ import com . google . gson . GsonBuilder ;
23
+
24
24
import org .springframework .beans .factory .FactoryBean ;
25
25
import org .springframework .beans .factory .InitializingBean ;
26
26
import org .springframework .util .ClassUtils ;
27
27
28
- import com .google .gson .Gson ;
29
- import com .google .gson .GsonBuilder ;
30
-
31
28
32
29
/**
33
30
* A {@link FactoryBean} for creating a Google Gson 2.x {@link Gson} instance.
34
31
*
35
32
* @author Roy Clarkson
33
+ * @author Juergen Hoeller
36
34
* @since 4.1
37
35
*/
38
- public class GsonFactoryBean implements FactoryBean <Gson >, BeanClassLoaderAware , InitializingBean {
36
+ public class GsonFactoryBean implements FactoryBean <Gson >, InitializingBean {
39
37
40
- private static final boolean base64Present = ClassUtils .isPresent (
38
+ /** Apache Commons Codec present on the classpath, for Base64 encoding? */
39
+ private static final boolean commonsCodecPresent = ClassUtils .isPresent (
41
40
"org.apache.commons.codec.binary.Base64" , GsonFactoryBean .class .getClassLoader ());
42
41
43
- private final Log logger = LogFactory .getLog (getClass ());
44
-
45
-
46
- private Gson gson ;
47
42
48
43
private GsonBuilder gsonBuilder ;
49
44
50
- private Boolean prettyPrint ;
45
+ private boolean serializeNulls ;
51
46
52
- private Boolean serializeNulls ;
47
+ private boolean prettyPrinting ;
53
48
54
- private Boolean disableHtmlEscaping ;
49
+ private boolean disableHtmlEscaping ;
55
50
56
- private SimpleDateFormat dateFormat ;
51
+ private String dateFormatPattern ;
57
52
58
- private Boolean base64EncodeByteArrays ;
53
+ private boolean base64EncodeByteArrays ;
59
54
60
- private ClassLoader beanClassLoader ;
55
+ private Gson gson ;
61
56
62
57
63
58
/**
64
- * Set the GsonBuilder instance to use. If not set, the GsonBuilder will be
65
- * created using its default constructor.
59
+ * Set the GsonBuilder instance to use.
60
+ * If not set, the GsonBuilder will be created using its default constructor.
66
61
*/
67
62
public void setGsonBuilder (GsonBuilder gsonBuilder ) {
68
63
this .gsonBuilder = gsonBuilder ;
69
64
}
70
65
71
66
/**
72
- * Return the configured GsonBuilder instance to use, if any.
73
- * @return the GsonBuilder instance
74
- */
75
- public GsonBuilder getGsonBuilder () {
76
- return this .gsonBuilder ;
77
- }
78
-
79
- /**
80
- * Whether to use the {@link GsonBuilder#setPrettyPrinting()} when writing
67
+ * Whether to use the {@link GsonBuilder#serializeNulls()} option when writing
81
68
* JSON. This is a shortcut for setting up a {@code Gson} as follows:
82
- *
83
69
* <pre class="code">
84
- * new GsonBuilder().setPrettyPrinting ().create();
70
+ * new GsonBuilder().serializeNulls ().create();
85
71
* </pre>
86
72
*/
87
- public void setPrettyPrint (boolean prettyPrint ) {
88
- this .prettyPrint = prettyPrint ;
73
+ public void setSerializeNulls (boolean serializeNulls ) {
74
+ this .serializeNulls = serializeNulls ;
89
75
}
90
76
91
77
/**
92
- * Whether to use the {@link GsonBuilder#serializeNulls()} option when
93
- * writing JSON. This is a shortcut for setting up a {@code Gson} as
94
- * follows:
95
- *
78
+ * Whether to use the {@link GsonBuilder#setPrettyPrinting()} when writing
79
+ * JSON. This is a shortcut for setting up a {@code Gson} as follows:
96
80
* <pre class="code">
97
- * new GsonBuilder().serializeNulls ().create();
81
+ * new GsonBuilder().setPrettyPrinting ().create();
98
82
* </pre>
99
83
*/
100
- public void setSerializeNulls (boolean serializeNulls ) {
101
- this .serializeNulls = serializeNulls ;
84
+ public void setPrettyPrinting (boolean prettyPrinting ) {
85
+ this .prettyPrinting = prettyPrinting ;
102
86
}
103
87
104
88
/**
105
89
* Whether to use the {@link GsonBuilder#disableHtmlEscaping()} when writing
106
90
* JSON. Set to {@code true} to disable HTML escaping in JSON. This is a
107
91
* shortcut for setting up a {@code Gson} as follows:
108
- *
109
92
* <pre class="code">
110
93
* new GsonBuilder().disableHtmlEscaping().create();
111
94
* </pre>
@@ -115,92 +98,67 @@ public void setDisableHtmlEscaping(boolean disableHtmlEscaping) {
115
98
}
116
99
117
100
/**
118
- * Define the format for date/time with the given {@link SimpleDateFormat}.
101
+ * Define the date/time format with a {@link SimpleDateFormat}-style pattern .
119
102
* This is a shortcut for setting up a {@code Gson} as follows:
120
- *
121
103
* <pre class="code">
122
104
* new GsonBuilder().setDateFormat(dateFormatPattern).create();
123
105
* </pre>
124
- *
125
- * @see #setSimpleDateFormat(String)
126
106
*/
127
- public void setSimpleDateFormat ( SimpleDateFormat dateFormat ) {
128
- this .dateFormat = dateFormat ;
107
+ public void setDateFormatPattern ( String dateFormatPattern ) {
108
+ this .dateFormatPattern = dateFormatPattern ;
129
109
}
130
110
131
111
/**
132
- * Define the date/time format with a {@link SimpleDateFormat}.
133
- * This is a shortcut for setting up a {@code Gson} as follows:
134
- *
135
- * <pre class="code">
136
- * new GsonBuilder().setDateFormat(dateFormatPattern).create();
137
- * </pre>
138
- *
139
- * @see #setSimpleDateFormat(SimpleDateFormat)
140
- */
141
- public void setSimpleDateFormat (String format ) {
142
- this .dateFormat = new SimpleDateFormat (format );
143
- }
144
-
145
- /**
146
- * Whether to Base64 encode {@code byte[]} properties when reading and
112
+ * Whether to Base64-encode {@code byte[]} properties when reading and
147
113
* writing JSON.
148
- *
149
- * <p>When set to {@code true} a custom {@link com.google.gson.TypeAdapter}
150
- * is registered via
151
- * {@link GsonBuilder#registerTypeHierarchyAdapter(Class, Object)}
152
- * that serializes a {@code byte[]} property to and from a Base64 encoded
153
- * string instead of a JSON array.
154
- *
155
- * <p><strong>NOTE:</strong> Use of this option requires the presence of
156
- * Apache commons-codec on the classpath. Otherwise it is ignored.
157
- *
158
- * @see org.springframework.http.converter.json.GsonBase64ByteArrayJsonTypeAdapter
114
+ * <p>When set to {@code true} a custom {@link com.google.gson.TypeAdapter} is
115
+ * registered via {@link GsonBuilder#registerTypeHierarchyAdapter(Class, Object)}
116
+ * that serializes a {@code byte[]} property to and from a Base64-encoded String
117
+ * instead of a JSON array.
118
+ * <p><strong>NOTE:</strong> Use of this option requires the presence of the
119
+ * Apache Commons Codec library on the classpath.
120
+ * @see GsonBase64ByteArrayJsonTypeAdapter
159
121
*/
160
122
public void setBase64EncodeByteArrays (boolean base64EncodeByteArrays ) {
161
123
this .base64EncodeByteArrays = base64EncodeByteArrays ;
162
124
}
163
125
164
- @ Override
165
- public void setBeanClassLoader (ClassLoader beanClassLoader ) {
166
- this .beanClassLoader = beanClassLoader ;
167
- }
168
-
169
126
170
127
@ Override
171
- public void afterPropertiesSet () throws Exception {
128
+ public void afterPropertiesSet () {
172
129
if (this .gsonBuilder == null ) {
173
130
this .gsonBuilder = new GsonBuilder ();
174
131
}
175
- if (this .prettyPrint != null && this . prettyPrint ) {
176
- this .gsonBuilder = this . gsonBuilder . setPrettyPrinting ();
132
+ if (this .serializeNulls ) {
133
+ this .gsonBuilder . serializeNulls ();
177
134
}
178
- if (this .serializeNulls != null && this . serializeNulls ) {
179
- this .gsonBuilder = this . gsonBuilder . serializeNulls ();
135
+ if (this .prettyPrinting ) {
136
+ this .gsonBuilder . setPrettyPrinting ();
180
137
}
181
- if (this .disableHtmlEscaping != null && this . disableHtmlEscaping ) {
182
- this .gsonBuilder = this . gsonBuilder .disableHtmlEscaping ();
138
+ if (this .disableHtmlEscaping ) {
139
+ this .gsonBuilder .disableHtmlEscaping ();
183
140
}
184
- if (this .dateFormat != null ) {
185
- this .gsonBuilder .setDateFormat (this .dateFormat . toPattern () );
141
+ if (this .dateFormatPattern != null ) {
142
+ this .gsonBuilder .setDateFormat (this .dateFormatPattern );
186
143
}
187
- if (base64Present ) {
188
- if (this . base64EncodeByteArrays != null && this . base64EncodeByteArrays ) {
144
+ if (this . base64EncodeByteArrays ) {
145
+ if (commonsCodecPresent ) {
189
146
this .gsonBuilder .registerTypeHierarchyAdapter (byte [].class , new GsonBase64ByteArrayJsonTypeAdapter ());
190
147
}
191
- }
192
- else if ( logger . isDebugEnabled ()) {
193
- logger . debug ( "org.apache.commons.codec.binary.Base64 is not " +
194
- "available on the class path. Gson Base64 encoding is disabled." );
148
+ else {
149
+ throw new IllegalStateException (
150
+ "Apache Commons Codec is not available on the classpath - cannot enable Gson Base64 encoding" );
151
+ }
195
152
}
196
153
this .gson = this .gsonBuilder .create ();
197
154
}
198
155
156
+
199
157
/**
200
158
* Return the created Gson instance.
201
159
*/
202
160
@ Override
203
- public Gson getObject () throws Exception {
161
+ public Gson getObject () {
204
162
return this .gson ;
205
163
}
206
164
0 commit comments