Polishing

This commit is contained in:
Juergen Hoeller 2014-05-27 18:29:51 +02:00
parent 22a38d4547
commit 8220832c4e
3 changed files with 40 additions and 47 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,13 +25,14 @@ import java.lang.annotation.Target;
* Declares that a field should be formatted as a number.
* Supports formatting by style or custom pattern string.
* Can be applied to any JDK {@code java.lang.Number} type.
* <p>
* For style-based formatting, set the {@link #style()} attribute to be the desired {@link Style}.
*
* <p>For style-based formatting, set the {@link #style()} attribute to be the desired {@link Style}.
* For custom formatting, set the {@link #pattern()} attribute to be the number pattern, such as {@code #, ###.##}.
* <p>
* Each attribute is mutually exclusive, so only set one attribute per annotation instance (the one most convenient one for your formatting needs).
* When the pattern attribute is specified, it takes precedence over the style attribute.
* When no annotation attributes are specified, the default format applied is style-based with a style of {@link Style#NUMBER}.
*
* <p>Each attribute is mutually exclusive, so only set one attribute per annotation instance
* (the one most convenient one for your formatting needs). When the pattern attribute is specified,
* it takes precedence over the style attribute. When no annotation attributes are specified,
* the default format applied is style-based with a style of {@link Style#NUMBER}.
*
* @author Keith Donald
* @since 3.0
@ -43,23 +44,23 @@ public @interface NumberFormat {
/**
* The style pattern to use to format the field.
* Defaults to {@link Style#NUMBER} for general-purpose number formatter.
* Set this attribute when you wish to format your field in accordance with a common style other than the default style.
* <p>Defaults to {@link Style#NUMBER} for general-purpose number formatter.
* Set this attribute when you wish to format your field in accordance with a
* common style other than the default style.
*/
Style style() default Style.NUMBER;
/**
* The custom pattern to use to format the field.
* Defaults to empty String, indicating no custom pattern String has been specified.
* Set this attribute when you wish to format your field in accordance with a custom number pattern not represented by a style.
* <p>Defaults to empty String, indicating no custom pattern String has been specified.
* Set this attribute when you wish to format your field in accordance with a
* custom number pattern not represented by a style.
*/
String pattern() default "";
/**
* Common number format styles.
* @author Keith Donald
* @since 3.0
*/
public enum Style {

View File

@ -19,32 +19,30 @@ package org.springframework.http.converter.json;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.TypeAdapter;
import org.apache.commons.codec.binary.Base64;
/**
* Custom Gson {@link TypeAdapter} for serialization or deserialization of
* {@code byte[]}. By default Gson converts byte arrays to JSON arrays instead
* of a Base64 encoded string. Use this type adapter with
* {@link org.springframework.http.converter.json.GsonHttpMessageConverter
* GsonHttpMessageConverter} to read and write Base64 encoded byte arrays.
* Custom Gson {@link TypeAdapter} for serialization and deserialization
* of {@code byte[]} values to/from Base64-encoded Strings.
*
* <p>By default, Gson converts byte arrays to JSON arrays. This type adapter
* needs to be specifically registered to read/write Base64-encoded byte arrays.
*
* @author Roy Clarkson
* @since 4.1
* @see GsonBuilder#registerTypeHierarchyAdapter(Class, Object)
*/
final class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer<byte[]>, JsonDeserializer<byte[]> {
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private final Base64 base64 = new Base64();
@ -56,7 +54,7 @@ final class GsonBase64ByteArrayJsonTypeAdapter implements JsonSerializer<byte[]>
}
@Override
public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) throws JsonParseException {
public byte[] deserialize(JsonElement json, Type type, JsonDeserializationContext cxt) {
return this.base64.decode(json.getAsString().getBytes(DEFAULT_CHARSET));
}

View File

@ -23,6 +23,11 @@ import java.io.Reader;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
@ -33,28 +38,21 @@ import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.Assert;
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
/**
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter
* HttpMessageConverter} that can read and write JSON using the <a
* href="https://code.google.com/p/google-gson/">Google Gson</a> library's {@link Gson}
* class.
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter}
* that can read and write JSON using the
* <a href="https://code.google.com/p/google-gson/">Google Gson</a> library's
* {@link Gson} class.
*
* <p>This converter can be used to bind to typed beans or untyped
* {@link java.util.HashMap HashMap} instances.
*
* <p>By default this converter supports {@code application/json} and
* {@code application/*+json} but {@link #setSupportedMediaTypes
* supportedMediaTypes} can be used to change that.
* <p>This converter can be used to bind to typed beans or untyped {@code HashMap}s.
* By default, it supports {@code application/json} and {@code application/*+json}.
*
* <p>Tested against Gson 2.2; compatible with Gson 2.0 and higher.
*
* @author Roy Clarkson
* @since 4.1
* @see #setGson
* @see #setSupportedMediaTypes
*/
public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Object>
implements GenericHttpMessageConverter<Object> {
@ -75,10 +73,10 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
new MediaType("application", "*+json", DEFAULT_CHARSET));
}
/**
* Set the {@code Gson} instance to use.
* If not set, a default {@link Gson#Gson() Gson} instance is used.
*
* <p>Setting a custom-configured {@code Gson} is one way to take further
* control of the JSON serialization process.
*/
@ -96,7 +94,6 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
/**
* Specify a custom prefix to use for JSON output. Default is none.
*
* @see #setPrefixJson
*/
public void setJsonPrefix(String jsonPrefix) {
@ -106,13 +103,11 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
/**
* Indicate whether the JSON output by this view should be prefixed with "{} &&".
* Default is {@code false}.
*
* <p>Prefixing the JSON string in this manner is used to help prevent JSON
* Hijacking. The prefix renders the string syntactically invalid as a script
* so that it cannot be hijacked. This prefix does not affect the evaluation
* of JSON, but if JSON validation is performed on the string, the prefix
* would need to be ignored.
*
* @see #setJsonPrefix
*/
public void setPrefixJson(boolean prefixJson) {
@ -159,16 +154,15 @@ public class GsonHttpMessageConverter extends AbstractHttpMessageConverter<Objec
/**
* Return the Gson {@link TypeToken} for the specified type.
*
* <p>The default implementation returns {@code TypeToken.get(type)}, but
* this can be overridden in subclasses to allow for custom generic
* collection handling. For instance:
* <pre class="code">
* protected TypeToken<?> getTypeToken(Type type) {
* if (type instanceof Class && List.class.isAssignableFrom((Class<?>) type)) {
* return new TypeToken<ArrayList<MyBean>>() {
* };
* } else {
* return new TypeToken<ArrayList<MyBean>>() {};
* }
* else {
* return super.getTypeToken(type);
* }
* }