1. package com.hq.schoolcj.util;
    2. import com.fasterxml.jackson.annotation.JsonInclude;
    3. import com.fasterxml.jackson.core.JsonFactory;
    4. import com.fasterxml.jackson.core.JsonGenerator;
    5. import com.fasterxml.jackson.core.JsonParser;
    6. import com.fasterxml.jackson.core.JsonParser.Feature;
    7. import com.fasterxml.jackson.databind.DeserializationFeature;
    8. import com.fasterxml.jackson.databind.ObjectMapper;
    9. import com.google.gson.Gson;
    10. import com.google.gson.GsonBuilder;
    11. import java.io.IOException;
    12. import java.io.StringWriter;
    13. import java.util.HashMap;
    14. import java.util.Iterator;
    15. import java.util.Map;
    16. import java.util.Map.Entry;
    17. /**
    18. * json javabean 之间的转换工具;
    19. * servlet 输出工具
    20. *
    21. * @author yangyang.cong
    22. */
    23. public abstract class JSONUtil {
    24. private static final ObjectMapper MAPPER = new ObjectMapper();
    25. private static final JsonFactory JSONFACTORY =new JsonFactory();
    26. private static final Gson GSON = new GsonBuilder().create();
    27. public static<T> String objToGson(T t) {
    28. return GSON.toJson(t);
    29. }
    30. public static<T> T gsonToObj(String gson, Class<T> type) {
    31. return GSON.fromJson(gson,type);
    32. }
    33. /**
    34. * 转换Java Bean 为 json
    35. */
    36. public static String beanToJson(Object o) {
    37. StringWriter sw = new StringWriter(300);
    38. JsonGenerator gen = null;
    39. try {
    40. gen = JSONFACTORY.createGenerator(sw);
    41. MAPPER.writeValue(gen, o);
    42. return sw.toString();
    43. } catch (Exception e) {
    44. throw new RuntimeException("JSON转换失败", e);
    45. } finally {
    46. if (gen != null) try {
    47. gen.close();
    48. } catch (IOException ignored) {
    49. }
    50. }
    51. }
    52. public static String beanToJson(ToJson o) {
    53. return o.toJsonString();
    54. }
    55. /**
    56. * 转换Java Bean 为 HashMap
    57. */
    58. public static Map<String, Object> beanToMap(Object o) {
    59. try {
    60. return (Map) MAPPER.readValue(beanToJson(o), HashMap.class);
    61. } catch (IOException e) {
    62. throw new RuntimeException("转换失败", e);
    63. }
    64. }
    65. /**
    66. * 转换Json String 为 HashMap
    67. */
    68. public static Map<String, Object> jsonToMap(String json) {
    69. try {
    70. //配置转义字符
    71. MAPPER.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    72. return (Map) MAPPER.readValue(json, HashMap.class);
    73. } catch (IOException e) {
    74. System.out.println(e.toString());
    75. throw new RuntimeException("转换失败", e);
    76. }
    77. }
    78. public static String hashMapToJson(Map<String, String> params) {
    79. String dot = "\"";
    80. String string = "{";
    81. for (Iterator it = params.entrySet().iterator(); it.hasNext();) {
    82. Entry e = (Entry) it.next();
    83. string += dot + e.getKey() + dot+":";
    84. string += dot + e.getValue() + dot+",";
    85. }
    86. string = string.substring(0, string.lastIndexOf(","));
    87. string += "}";
    88. return string;
    89. }
    90. /**
    91. * 转换Json String 为 JavaBean
    92. */
    93. public static <T> T jsonToBean(String json, Class<T> type) {
    94. try {
    95. return MAPPER.readValue(json, type);
    96. } catch (IOException e) {
    97. throw new RuntimeException(e);
    98. }
    99. }
    100. static {
    101. MAPPER.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    102. MAPPER.configure(Feature.ALLOW_SINGLE_QUOTES, true);
    103. MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
    104. //MAPPER.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);
    105. }
    106. public static void validateJSON(String json) throws IOException {
    107. try(JsonParser parser = JSONFACTORY.createParser(json)){
    108. while (parser.nextToken() != null) {
    109. }
    110. }
    111. }
    112. public interface ToJson{
    113. String toJsonString();
    114. }
    115. }