Java操作JSON的便捷工具类(Gson)

来源:互联网 发布:苏宁 算法研究员 面试 编辑:程序博客网 时间:2024/05/01 07:35

对于JSON数据格式的处理,自开发Java以来,已用过多种JSON的开源工具,用得最好,也用得最High的恐怕要属Google的Gson了。

特别为它写了一个工具类,放入常备工具中,方便使用。下面是为GSON 1.5版本重写的工具类。

依赖包:

slf4j-api-1.6.0.jar

slf4j-log4j12-1.6.0.jar

log4j-1.2.15.jar

gson-1.5.jar

[java] view plaincopyprint?
  1. /** 
  2.  * Copyright 2010 Fuchun. 
  3.  *  
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  *     http://www.apache.org/licenses/LICENSE-2.0 
  8.  *  
  9.  * Unless required by applicable law or agreed to in writing, software 
  10.  * distributed under the License is distributed on an "AS IS" BASIS, 
  11.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  12.  * See the License for the specific language governing permissions and 
  13.  * limitations under the License. 
  14.  */  
  15.   
  16. package my.tools;  
  17. import java.lang.reflect.Type;  
  18. import java.util.Collection;  
  19. import java.util.Enumeration;  
  20. import java.util.Iterator;  
  21. import org.slf4j.Logger;  
  22. import org.slf4j.LoggerFactory;  
  23. import com.google.gson.Gson;  
  24. import com.google.gson.GsonBuilder;  
  25. import com.google.gson.reflect.TypeToken;  
  26. import org.apache.commons.lang.StringUtils;  
  27.   
  28. /** 
  29.  * 包含操作 {@code JSON} 数据的常用方法的工具类。 
  30.  * <p /> 
  31.  * 该工具类使用的 {@code JSON} 转换引擎是 <a href="http://code.google.com/p/google-gson/" mce_href="http://code.google.com/p/google-gson/" target="_blank"> 
  32.  * {@code Google Gson}</a>。 下面是工具类的使用案例: 
  33.  *  
  34.  * <pre> 
  35.  * public class User {  
  36.  *     @SerializedName("pwd") 
  37.  *     private String password;  
  38.  *     @Expose 
  39.  *     @SerializedName("uname") 
  40.  *     private String username;  
  41.  *     @Expose 
  42.  *     @Since(1.1) 
  43.  *     private String gender;  
  44.  *     @Expose 
  45.  *     @Since(1.0) 
  46.  *     private String sex;  
  47.  *       
  48.  *     public User() {}  
  49.  *     public User(String username, String password, String gender) {  
  50.  *         // user constructor code... ... ...  
  51.  *     }  
  52.  *       
  53.  *     public String getUsername()  
  54.  *     ... ... ...  
  55.  * } 
  56.  * List<User> userList = new LinkedList<User>();  
  57.  * User jack = new User("Jack", "123456", "Male");  
  58.  * User marry = new User("Marry", "888888", "Female");  
  59.  * userList.add(jack);  
  60.  * userList.add(marry);  
  61.  * Type targetType = new TypeToken<List<User>>(){}.getType();  
  62.  * String sUserList1 = JSONUtils.toJson(userList, targetType);  
  63.  * sUserList1 ----> [{"uname":"jack","gender":"Male","sex":"Male"},{"uname":"marry","gender":"Female","sex":"Female"}]  
  64.  * String sUserList2 = JSONUtils.toJson(userList, targetType, false);  
  65.  * sUserList2 ----> [{"uname":"jack","pwd":"123456","gender":"Male","sex":"Male"},{"uname":"marry","pwd":"888888","gender":"Female","sex":"Female"}]  
  66.  * String sUserList3 = JSONUtils.toJson(userList, targetType, 1.0d, true);  
  67.  * sUserList3 ----> [{"uname":"jack","sex":"Male"},{"uname":"marry","sex":"Female"}] 
  68.  * </pre> 
  69.  *  
  70.  * @author Fuchun 
  71.  * @since ay-commons-lang 1.0 
  72.  * @version 1.1.0 
  73.  */  
  74. public class JSONUtils {  
  75.     private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtils.class);  
  76.   
  77.     /** 空的 {@code JSON} 数据 - <code>"{}"</code>。 */  
  78.     public static final String EMPTY_JSON = "{}";  
  79.   
  80.     /** 空的 {@code JSON} 数组(集合)数据 - {@code "[]"}。 */  
  81.     public static final String EMPTY_JSON_ARRAY = "[]";  
  82.   
  83.     /** 默认的 {@code JSON} 日期/时间字段的格式化模式。 */  
  84.     public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss SSS";  
  85.   
  86.     /** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本号常量 - {@code 1.0}。 */  
  87.     public static final double SINCE_VERSION_10 = 1.0d;  
  88.   
  89.     /** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本号常量 - {@code 1.1}。 */  
  90.     public static final double SINCE_VERSION_11 = 1.1d;  
  91.   
  92.     /** {@code Google Gson} 的 <code>@Since</code> 注解常用的版本号常量 - {@code 1.2}。 */  
  93.     public static final double SINCE_VERSION_12 = 1.2d;  
  94.   
  95.     /** {@code Google Gson} 的 <code>@Until</code> 注解常用的版本号常量 - {@code 1.0}。 */  
  96.     public static final double UNTIL_VERSION_10 = SINCE_VERSION_10;  
  97.   
  98.     /** {@code Google Gson} 的 <code>@Until</code> 注解常用的版本号常量 - {@code 1.1}。 */  
  99.     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 == nullreturn 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, nullfalsenullnulltrue);  
  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, nullfalsenull, 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, nullfalse, version, nulltrue);  
  193.     }  
  194.   
  195.     /** 
  196.      * 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean} 对象。</strong> 
  197.      * <ul> 
  198.      * <li>该方法不会转换 {@code null} 值字段;</li> 
  199.      * <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li> 
  200.      * <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li> 
  201.      * </ul> 
  202.      *  
  203.      * @param target 要转换成 {@code JSON} 的目标对象。 
  204.      * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。 
  205.      * @return 目标对象的 {@code JSON} 格式的字符串。 
  206.      * @since 1.0 
  207.      */  
  208.     public static String toJson(Object target, boolean excludesFieldsWithoutExpose) {  
  209.         return toJson(target, nullfalsenullnull, excludesFieldsWithoutExpose);  
  210.     }  
  211.   
  212.     /** 
  213.      * 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法只用来转换普通的 {@code JavaBean} 对象。</strong> 
  214.      * <ul> 
  215.      * <li>该方法不会转换 {@code null} 值字段;</li> 
  216.      * <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li> 
  217.      * </ul> 
  218.      *  
  219.      * @param target 要转换成 {@code JSON} 的目标对象。 
  220.      * @param version 字段的版本号注解({@literal @Since})。 
  221.      * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。 
  222.      * @return 目标对象的 {@code JSON} 格式的字符串。 
  223.      * @since 1.0 
  224.      */  
  225.     public static String toJson(Object target, Double version, boolean excludesFieldsWithoutExpose) {  
  226.         return toJson(target, nullfalse, version, null, excludesFieldsWithoutExpose);  
  227.     }  
  228.   
  229.     /** 
  230.      * 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法通常用来转换使用泛型的对象。</strong> 
  231.      * <ul> 
  232.      * <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li> 
  233.      * <li>该方法不会转换 {@code null} 值字段;</li> 
  234.      * <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li> 
  235.      * <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSSS};</li> 
  236.      * </ul> 
  237.      *  
  238.      * @param target 要转换成 {@code JSON} 的目标对象。 
  239.      * @param targetType 目标对象的类型。 
  240.      * @return 目标对象的 {@code JSON} 格式的字符串。 
  241.      * @since 1.0 
  242.      */  
  243.     public static String toJson(Object target, Type targetType) {  
  244.         return toJson(target, targetType, falsenullnulltrue);  
  245.     }  
  246.   
  247.     /** 
  248.      * 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法通常用来转换使用泛型的对象。</strong> 
  249.      * <ul> 
  250.      * <li>该方法只会转换标有 {@literal @Expose} 注解的字段;</li> 
  251.      * <li>该方法不会转换 {@code null} 值字段;</li> 
  252.      * <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSSS};</li> 
  253.      * </ul> 
  254.      *  
  255.      * @param target 要转换成 {@code JSON} 的目标对象。 
  256.      * @param targetType 目标对象的类型。 
  257.      * @param version 字段的版本号注解({@literal @Since})。 
  258.      * @return 目标对象的 {@code JSON} 格式的字符串。 
  259.      * @since 1.0 
  260.      */  
  261.     public static String toJson(Object target, Type targetType, Double version) {  
  262.         return toJson(target, targetType, false, version, nulltrue);  
  263.     }  
  264.   
  265.     /** 
  266.      * 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法通常用来转换使用泛型的对象。</strong> 
  267.      * <ul> 
  268.      * <li>该方法不会转换 {@code null} 值字段;</li> 
  269.      * <li>该方法会转换所有未标注或已标注 {@literal @Since} 的字段;</li> 
  270.      * <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li> 
  271.      * </ul> 
  272.      *  
  273.      * @param target 要转换成 {@code JSON} 的目标对象。 
  274.      * @param targetType 目标对象的类型。 
  275.      * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。 
  276.      * @return 目标对象的 {@code JSON} 格式的字符串。 
  277.      * @since 1.0 
  278.      */  
  279.     public static String toJson(Object target, Type targetType, boolean excludesFieldsWithoutExpose) {  
  280.         return toJson(target, targetType, falsenullnull, excludesFieldsWithoutExpose);  
  281.     }  
  282.   
  283.     /** 
  284.      * 将给定的目标对象转换成 {@code JSON} 格式的字符串。<strong>此方法通常用来转换使用泛型的对象。</strong> 
  285.      * <ul> 
  286.      * <li>该方法不会转换 {@code null} 值字段;</li> 
  287.      * <li>该方法转换时使用默认的 日期/时间 格式化模式 - {@code yyyy-MM-dd HH:mm:ss SSS};</li> 
  288.      * </ul> 
  289.      *  
  290.      * @param target 要转换成 {@code JSON} 的目标对象。 
  291.      * @param targetType 目标对象的类型。 
  292.      * @param version 字段的版本号注解({@literal @Since})。 
  293.      * @param excludesFieldsWithoutExpose 是否排除未标注 {@literal @Expose} 注解的字段。 
  294.      * @return 目标对象的 {@code JSON} 格式的字符串。 
  295.      * @since 1.0 
  296.      */  
  297.     public static String toJson(Object target, Type targetType, Double version, boolean excludesFieldsWithoutExpose) {  
  298.         return toJson(target, targetType, false, version, null, excludesFieldsWithoutExpose);  
  299.     }  
  300.   
  301.     /** 
  302.      * 将给定的 {@code JSON} 字符串转换成指定的类型对象。 
  303.      *  
  304.      * @param <T> 要转换的目标类型。 
  305.      * @param json 给定的 {@code JSON} 字符串。 
  306.      * @param token {@code com.google.gson.reflect.TypeToken} 的类型指示类对象。 
  307.      * @param datePattern 日期格式模式。 
  308.      * @return 给定的 {@code JSON} 字符串表示的指定的类型对象。 
  309.      * @since 1.0 
  310.      */  
  311.     public static <T> T fromJson(String json, TypeToken<T> token, String datePattern) {  
  312.         if (StringUtils.isBlank(json)) {  
  313.             return null;  
  314.         }  
  315.         GsonBuilder builder = new GsonBuilder();  
  316.         if (StringUtils.isBlank(datePattern)) {  
  317.             datePattern = DEFAULT_DATE_PATTERN;  
  318.         }  
  319.         Gson gson = builder.create();  
  320.         try {  
  321.             return gson.fromJson(json, token.getType());  
  322.         } catch (Exception ex) {  
  323.             LOGGER.error(json + " 无法转换为 " + token.getRawType().getName() + " 对象!", ex);  
  324.             return null;  
  325.         }  
  326.     }  
  327.   
  328.     /** 
  329.      * 将给定的 {@code JSON} 字符串转换成指定的类型对象。 
  330.      *  
  331.      * @param <T> 要转换的目标类型。 
  332.      * @param json 给定的 {@code JSON} 字符串。 
  333.      * @param token {@code com.google.gson.reflect.TypeToken} 的类型指示类对象。 
  334.      * @return 给定的 {@code JSON} 字符串表示的指定的类型对象。 
  335.      * @since 1.0 
  336.      */  
  337.     public static <T> T fromJson(String json, TypeToken<T> token) {  
  338.         return fromJson(json, token, null);  
  339.     }  
  340.   
  341.     /** 
  342.      * 将给定的 {@code JSON} 字符串转换成指定的类型对象。<strong>此方法通常用来转换普通的 {@code JavaBean} 对象。</strong> 
  343.      *  
  344.      * @param <T> 要转换的目标类型。 
  345.      * @param json 给定的 {@code JSON} 字符串。 
  346.      * @param clazz 要转换的目标类。 
  347.      * @param datePattern 日期格式模式。 
  348.      * @return 给定的 {@code JSON} 字符串表示的指定的类型对象。 
  349.      * @since 1.0 
  350.      */  
  351.     public static <T> T fromJson(String json, Class<T> clazz, String datePattern) {  
  352.         if (StringUtils.isBlank(json)) {  
  353.             return null;  
  354.         }  
  355.         GsonBuilder builder = new GsonBuilder();  
  356.         if (StringUtils.isBlank(datePattern)) {  
  357.             datePattern = DEFAULT_DATE_PATTERN;  
  358.         }  
  359.         Gson gson = builder.create();  
  360.         try {  
  361.             return gson.fromJson(json, clazz);  
  362.         } catch (Exception ex) {  
  363.             LOGGER.error(json + " 无法转换为 " + clazz.getName() + " 对象!", ex);  
  364.             return null;  
  365.         }  
  366.     }  
  367.   
  368.     /** 
  369.      * 将给定的 {@code JSON} 字符串转换成指定的类型对象。<strong>此方法通常用来转换普通的 {@code JavaBean} 对象。</strong> 
  370.      *  
  371.      * @param <T> 要转换的目标类型。 
  372.      * @param json 给定的 {@code JSON} 字符串。 
  373.      * @param clazz 要转换的目标类。 
  374.      * @return 给定的 {@code JSON} 字符串表示的指定的类型对象。 
  375.      * @since 1.0 
  376.      */  
  377.     public static <T> T fromJson(String json, Class<T> clazz) {  
  378.         return fromJson(json, clazz, null);  
  379.     }  
  380.   
  381.     /** 
  382.      * 将给定的目标对象根据{@code GsonBuilder} 所指定的条件参数转换成 {@code JSON} 格式的字符串。 
  383.      * <p /> 
  384.      * 该方法转换发生错误时,不会抛出任何异常。若发生错误时,{@code JavaBean} 对象返回 <code>"{}"</code>; 集合或数组对象返回 
  385.      * <code>"[]"</code>。 其本基本类型,返回相应的基本值。 
  386.      *  
  387.      * @param target 目标对象。 
  388.      * @param targetType 目标对象的类型。 
  389.      * @param builder 可定制的{@code Gson} 构建器。 
  390.      * @return 目标对象的 {@code JSON} 格式的字符串。 
  391.      * @since 1.1 
  392.      */  
  393.     public static String toJson(Object target, Type targetType, GsonBuilder builder) {  
  394.         if (target == nullreturn EMPTY_JSON;  
  395.         Gson gson = null;  
  396.         if (builder == null) {  
  397.             gson = new Gson();  
  398.         } else {  
  399.             gson = builder.create();  
  400.         }  
  401.         String result = EMPTY_JSON;  
  402.         try {  
  403.             if (targetType == null) {  
  404.                 result = gson.toJson(target);  
  405.             } else {  
  406.                 result = gson.toJson(target, targetType);  
  407.             }  
  408.         } catch (Exception ex) {  
  409.             LOGGER.warn("目标对象 " + target.getClass().getName() + " 转换 JSON 字符串时,发生异常!", ex);  
  410.             if (target instanceof Collection<?> || target instanceof Iterator<?> || target instanceof Enumeration<?>  
  411.                     || target.getClass().isArray()) {  
  412.                 result = EMPTY_JSON_ARRAY;  
  413.             }  
  414.         }  
  415.         return result;  
  416.     }  
  417. }  
0 0
原创粉丝点击