001 |
/** |
002 |
* Copyright 2010 Fuchun.
|
003 |
*
|
004 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
005 |
* you may not use this file except in compliance with the License.
|
006 |
* You may obtain a copy of the License at
|
007 |
* http://www.apache.org/licenses/LICENSE-2.0
|
008 |
*
|
009 |
* Unless required by applicable law or agreed to in writing, software
|
010 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
011 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
012 |
* See the License for the specific language governing permissions and
|
013 |
* limitations under the License.
|
014 |
*/
|
015 |
016 |
package my.tools;
|
017 |
import java.lang.reflect.Type;
|
018 |
import java.util.Collection;
|
019 |
import java.util.Enumeration;
|
020 |
import java.util.Iterator;
|
021 |
import org.slf4j.Logger;
|
022 |
import org.slf4j.LoggerFactory;
|
023 |
import com.google.gson.Gson;
|
024 |
import com.google.gson.GsonBuilder;
|
025 |
import com.google.gson.reflect.TypeToken;
|
026 |
import org.apache.commons.lang.StringUtils;
|
027 |
028 |
/** |
029 |
* 包含操作 {@code JSON} 数据的常用方法的工具类。
|
030 |
* <p />
|
031 |
* 该工具类使用的 {@code JSON} 转换引擎是 <a href="http://code.google.com/p/google-gson/" mce_href="http://code.google.com/p/google-gson/" target="_blank">
|
032 |
* {@code Google Gson}</a>。 下面是工具类的使用案例:
|
033 |
*
|
034 |
* <pre>
|
035 |
* public class User {
|
036 |
* @SerializedName("pwd")
|
037 |
* private String password;
|
038 |
* @Expose
|
039 |
* @SerializedName("uname")
|
040 |
* private String username;
|
041 |
* @Expose
|
042 |
* @Since(1.1)
|
043 |
* private String gender;
|
044 |
* @Expose
|
045 |
* @Since(1.0)
|
046 |
* private String sex;
|
047 |
*
|
048 |
* public User() {}
|
049 |
* public User(String username, String password, String gender) {
|
050 |
* // user constructor code... ... ...
|
051 |
* }
|
052 |
*
|
053 |
* public String getUsername()
|
054 |
* ... ... ...
|
055 |
* }
|
056 |
* List<User> userList = new LinkedList<User>();
|
057 |
* User jack = new User("Jack", "123456", "Male");
|
058 |
* User marry = new User("Marry", "888888", "Female");
|
059 |
* userList.add(jack);
|
060 |
* userList.add(marry);
|
061 |
* Type targetType = new TypeToken<List<User>>(){}.getType();
|
062 |
* String sUserList1 = JSONUtils.toJson(userList, targetType);
|
063 |
* sUserList1 ----> [{"uname":"jack","gender":"Male","sex":"Male"},{"uname":"marry","gender":"Female","sex":"Female"}]
|
064 |
* String sUserList2 = JSONUtils.toJson(userList, targetType, false);
|
065 |
* sUserList2 ----> [{"uname":"jack","pwd":"123456","gender":"Male","sex":"Male"},{"uname":"marry","pwd":"888888","gender":"Female","sex":"Female"}]
|
066 |
* String sUserList3 = JSONUtils.toJson(userList, targetType, 1.0d, true);
|
067 |
* sUserList3 ----> [{"uname":"jack","sex":"Male"},{"uname":"marry","sex":"Female"}]
|
068 |
* </pre>
|
069 |
*
|
070 |
* @author Fuchun
|
071 |
* @since ay-commons-lang 1.0
|
072 |
* @version 1.1.0
|
073 |
*/
|
074 |
public class JSONUtils {
|
075 |
private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtils.class);
|
076 |
077 |
/** 空的 {@code JSON} 数据 - <code>"{}"</code>。 */
|
078 |
public static final String EMPTY_JSON = "{}";
|
079 |
080 |
/** 空的 {@code JSON} 数组(集合)数据 - {@code "[]"}。 */
|
081 |
public static final String EMPTY_JSON_ARRAY = "[]";
|
082 |
083 |
/** 默认的 {@code JSON} 日期/时间字段的格式化模式。 */
|
084 |
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss SSS";
|
085 |
086 |
/** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本号常量 - {@code 1.0}。 */
|
087 |
public static final double SINCE_VERSION_10 = 1.0d;
|
088 |
089 |
/** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本号常量 - {@code 1.1}。 */
|
090 |
public static final double SINCE_VERSION_11 = 1.1d;
|
091 |
092 |
/** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本号常量 - {@code 1.2}。 */
|
093 |
public static final double SINCE_VERSION_12 = 1.2d;
|
094 |
095 |
/** {@code Google Gson} 的 <code>@Until</code> 注解常用的版本号常量 - {@code 1.0}。 */
|
096 |
public static final double UNTIL_VERSION_10 = SINCE_VERSION_10;
|
097 |
098 |
/** {@code Google Gson} 的 <code>@Until</code> 注解常用的版本号常量 - {@code 1.1}。 */
|
099 |
public static final double UNTIL_VERSION_11 = SINCE_VERSION_11;
|
100 |
101 |
/** {@code Google Gson} 的 <code>@Until</code> 注解常用的版本号常量 - {@code 1.2}。 */
|
102 |
public static final double UNTIL_VERSION_12 = SINCE_VERSION_12;
|
103 |
104 |
/**
|
105 |
* <p>
|
106 |
* <code>JSONUtils</code> instances should NOT be constructed in standard programming. Instead,
|
107 |
* the class should be used as <code>JSONUtils.fromJson("foo");</code>.
|
108 |
* </p>
|
109 |
* <p>
|
110 |
* This constructor is public to permit tools that require a JavaBean instance to operate.
|
111 |
* </p>
|
112 |
*/
|
113 |
public JSONUtils() {
|
114 |
super();
|
115 |
}
|
116 |
117 |
/**
|
118 |
* 将给定的目标对象根据指定的条件参数转换成 {@code JSON} 格式的字符串。
|
119 |
* <p />
|
120 |
* <strong>该方法转换发生错误时,不会抛出任何异常。若发生错误时,曾通对象返回 <code>"{}"</code>; 集合或数组对象返回 <code>"[]"</code>
|
121 |
* </strong>
|
122 |
*
|
123 |
* @param target 目标对象。
|
124 |
* @param targetType 目标对象的类型。
|
125 |
* @param isSerializeNulls 是否序列化 {@code null} 值字段。
|
126 |
* @param version 字段的版本号注解。
|
127 |
* @param datePattern 日期字段的格式化模式。
|
128 |
* @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。
|
129 |
* @return 目标对象的 {@code JSON} 格式的字符串。
|
130 |
* @since 1.0
|
131 |
*/
|
132 |
public static String toJson(Object target, Type targetType, boolean isSerializeNulls, Double version,
|
133 |
String datePattern, boolean excludesFieldsWithoutExpose) {
|
134 |
if (target == null) return EMPTY_JSON;
|
135 |
GsonBuilder builder = new GsonBuilder();
|
136 |
if (isSerializeNulls) builder.serializeNulls();
|
137 |
if (version != null) builder.setVersion(version.doubleValue());
|
138 |
if (StringUtils.isBlank(datePattern)) datePattern = DEFAULT_DATE_PATTERN;
|
139 |
builder.setDateFormat(datePattern);
|
140 |
if (excludesFieldsWithoutExpose) builder.excludeFieldsWithoutExposeAnnotation();
|
141 |
return toJson(target, targetType, builder);
|
142 |
}
|
143 |
144 |
/**
|
145 |
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean} 对象。</strong>
|
146 |
* <ul>
|
147 |
* <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li>
|
148 |
* <li>该方法不会转换 {@code null} 值字段;</li>
|
149 |
* <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li>
|
150 |
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
|
151 |
* </ul>
|
152 |
*
|
153 |
* @param target 要转换成 {@code JSON} 的目标对象。
|
154 |
* @return 目标对象的 {@code JSON} 格式的字符串。
|
155 |
* @since 1.0
|
156 |
*/
|
157 |
public static String toJson(Object target) {
|
158 |
return toJson(target, null, false, null, null, true);
|
159 |
}
|
160 |
161 |
/**
|
162 |
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean} 对象。</strong>
|
163 |
* <ul>
|
164 |
* <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li>
|
165 |
* <li>该方法不会转换 {@code null} 值字段;</li>
|
166 |
* <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li>
|
167 |
* </ul>
|
168 |
*
|
169 |
* @param target 要转换成 {@code JSON} 的目标对象。
|
170 |
* @param datePattern 日期字段的格式化模式。
|
171 |
* @return 目标对象的 {@code JSON} 格式的字符串。
|
172 |
* @since 1.0
|
173 |
*/
|
174 |
public static String toJson(Object target, String datePattern) {
|
175 |
return toJson(target, null, false, null, datePattern, true);
|
176 |
}
|
177 |
178 |
/**
|
179 |
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean} 对象。</strong>
|
180 |
* <ul>
|
181 |
* <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li>
|
182 |
* <li>该方法不会转换 {@code null} 值字段;</li>
|
183 |
* <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li>
|
184 |
* </ul>
|
185 |
*
|
186 |
* @param target 要转换成 {@code JSON} 的目标对象。
|
187 |
* @param version 字段的版本号注解({@literal @Since})。
|
188 |
* @return 目标对象的 {@code JSON} 格式的字符串。
|
189 |
* @since 1.0
|
190 |
*/
|
191 |
public static String toJson(Object target, Double version) {
|
192 |
return toJson(target, null, false, version, null, true);
|
193 |
}
|
194 |
195 |
/**
|
196 |
* 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean} 对象。</strong>
|
|