1818
1919import java .text .SimpleDateFormat ;
2020
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+
2424import org .springframework .beans .factory .FactoryBean ;
2525import org .springframework .beans .factory .InitializingBean ;
2626import org .springframework .util .ClassUtils ;
2727
28- import com .google .gson .Gson ;
29- import com .google .gson .GsonBuilder ;
30-
3128
3229/**
3330 * A {@link FactoryBean} for creating a Google Gson 2.x {@link Gson} instance.
3431 *
3532 * @author Roy Clarkson
33+ * @author Juergen Hoeller
3634 * @since 4.1
3735 */
38- public class GsonFactoryBean implements FactoryBean <Gson >, BeanClassLoaderAware , InitializingBean {
36+ public class GsonFactoryBean implements FactoryBean <Gson >, InitializingBean {
3937
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 (
4140 "org.apache.commons.codec.binary.Base64" , GsonFactoryBean .class .getClassLoader ());
4241
43- private final Log logger = LogFactory .getLog (getClass ());
44-
45-
46- private Gson gson ;
4742
4843 private GsonBuilder gsonBuilder ;
4944
50- private Boolean prettyPrint ;
45+ private boolean serializeNulls ;
5146
52- private Boolean serializeNulls ;
47+ private boolean prettyPrinting ;
5348
54- private Boolean disableHtmlEscaping ;
49+ private boolean disableHtmlEscaping ;
5550
56- private SimpleDateFormat dateFormat ;
51+ private String dateFormatPattern ;
5752
58- private Boolean base64EncodeByteArrays ;
53+ private boolean base64EncodeByteArrays ;
5954
60- private ClassLoader beanClassLoader ;
55+ private Gson gson ;
6156
6257
6358 /**
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.
6661 */
6762 public void setGsonBuilder (GsonBuilder gsonBuilder ) {
6863 this .gsonBuilder = gsonBuilder ;
6964 }
7065
7166 /**
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
8168 * JSON. This is a shortcut for setting up a {@code Gson} as follows:
82- *
8369 * <pre class="code">
84- * new GsonBuilder().setPrettyPrinting ().create();
70+ * new GsonBuilder().serializeNulls ().create();
8571 * </pre>
8672 */
87- public void setPrettyPrint (boolean prettyPrint ) {
88- this .prettyPrint = prettyPrint ;
73+ public void setSerializeNulls (boolean serializeNulls ) {
74+ this .serializeNulls = serializeNulls ;
8975 }
9076
9177 /**
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:
9680 * <pre class="code">
97- * new GsonBuilder().serializeNulls ().create();
81+ * new GsonBuilder().setPrettyPrinting ().create();
9882 * </pre>
9983 */
100- public void setSerializeNulls (boolean serializeNulls ) {
101- this .serializeNulls = serializeNulls ;
84+ public void setPrettyPrinting (boolean prettyPrinting ) {
85+ this .prettyPrinting = prettyPrinting ;
10286 }
10387
10488 /**
10589 * Whether to use the {@link GsonBuilder#disableHtmlEscaping()} when writing
10690 * JSON. Set to {@code true} to disable HTML escaping in JSON. This is a
10791 * shortcut for setting up a {@code Gson} as follows:
108- *
10992 * <pre class="code">
11093 * new GsonBuilder().disableHtmlEscaping().create();
11194 * </pre>
@@ -115,92 +98,67 @@ public void setDisableHtmlEscaping(boolean disableHtmlEscaping) {
11598 }
11699
117100 /**
118- * Define the format for date/time with the given {@link SimpleDateFormat}.
101+ * Define the date/time format with a {@link SimpleDateFormat}-style pattern .
119102 * This is a shortcut for setting up a {@code Gson} as follows:
120- *
121103 * <pre class="code">
122104 * new GsonBuilder().setDateFormat(dateFormatPattern).create();
123105 * </pre>
124- *
125- * @see #setSimpleDateFormat(String)
126106 */
127- public void setSimpleDateFormat ( SimpleDateFormat dateFormat ) {
128- this .dateFormat = dateFormat ;
107+ public void setDateFormatPattern ( String dateFormatPattern ) {
108+ this .dateFormatPattern = dateFormatPattern ;
129109 }
130110
131111 /**
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
147113 * 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
159121 */
160122 public void setBase64EncodeByteArrays (boolean base64EncodeByteArrays ) {
161123 this .base64EncodeByteArrays = base64EncodeByteArrays ;
162124 }
163125
164- @ Override
165- public void setBeanClassLoader (ClassLoader beanClassLoader ) {
166- this .beanClassLoader = beanClassLoader ;
167- }
168-
169126
170127 @ Override
171- public void afterPropertiesSet () throws Exception {
128+ public void afterPropertiesSet () {
172129 if (this .gsonBuilder == null ) {
173130 this .gsonBuilder = new GsonBuilder ();
174131 }
175- if (this .prettyPrint != null && this . prettyPrint ) {
176- this .gsonBuilder = this . gsonBuilder . setPrettyPrinting ();
132+ if (this .serializeNulls ) {
133+ this .gsonBuilder . serializeNulls ();
177134 }
178- if (this .serializeNulls != null && this . serializeNulls ) {
179- this .gsonBuilder = this . gsonBuilder . serializeNulls ();
135+ if (this .prettyPrinting ) {
136+ this .gsonBuilder . setPrettyPrinting ();
180137 }
181- if (this .disableHtmlEscaping != null && this . disableHtmlEscaping ) {
182- this .gsonBuilder = this . gsonBuilder .disableHtmlEscaping ();
138+ if (this .disableHtmlEscaping ) {
139+ this .gsonBuilder .disableHtmlEscaping ();
183140 }
184- if (this .dateFormat != null ) {
185- this .gsonBuilder .setDateFormat (this .dateFormat . toPattern () );
141+ if (this .dateFormatPattern != null ) {
142+ this .gsonBuilder .setDateFormat (this .dateFormatPattern );
186143 }
187- if (base64Present ) {
188- if (this . base64EncodeByteArrays != null && this . base64EncodeByteArrays ) {
144+ if (this . base64EncodeByteArrays ) {
145+ if (commonsCodecPresent ) {
189146 this .gsonBuilder .registerTypeHierarchyAdapter (byte [].class , new GsonBase64ByteArrayJsonTypeAdapter ());
190147 }
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+ }
195152 }
196153 this .gson = this .gsonBuilder .create ();
197154 }
198155
156+
199157 /**
200158 * Return the created Gson instance.
201159 */
202160 @ Override
203- public Gson getObject () throws Exception {
161+ public Gson getObject () {
204162 return this .gson ;
205163 }
206164
0 commit comments