JsonUtils.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.hcks.cmfds.commons.util;
  2. import java.text.SimpleDateFormat;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.codehaus.jackson.map.ObjectMapper;
  7. import org.codehaus.jackson.map.SerializationConfig;
  8. import com.hcks.cmfds.commons.lang.PaginationList;
  9. /**
  10. *
  11. * @Author 刘厦(liusha.information@gmail.com)
  12. * @Date 创建时间:May 16, 2011 13:08:19 PM
  13. * @Version 0.0.0
  14. */
  15. public class JsonUtils {
  16. private static ObjectMapper mapper = new ObjectMapper();
  17. public static final String SIMPLE_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
  18. public static final SimpleDateFormat df = new SimpleDateFormat(SIMPLE_DATETIME_FORMAT);
  19. static {
  20. mapper.configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
  21. mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
  22. mapper.getSerializationConfig().setDateFormat(df);
  23. }
  24. public static ObjectMapper getMapperBinder(){
  25. return mapper;
  26. }
  27. /**
  28. * 将各种形式转换为json格式
  29. * @param object
  30. * @return
  31. */
  32. public static String toJson(Object object) {
  33. try {
  34. return mapper.writeValueAsString(object);
  35. } catch (Exception e) {
  36. throw new RuntimeException(e);
  37. }
  38. }
  39. /**
  40. * 将json串 转换为已知对象
  41. * @param json
  42. * @param clazz
  43. * @return
  44. */
  45. public static Object fromJson(String json, Class<?> clazz) {
  46. try {
  47. return mapper.readValue(json, clazz);
  48. } catch (Exception e) {
  49. throw new RuntimeException(e);
  50. }
  51. }
  52. /**
  53. * 转换为grid数据
  54. * @param data
  55. * @param page
  56. * @return
  57. */
  58. @SuppressWarnings("unchecked")
  59. public static String wrapListForGrid(List data, int page) {
  60. if (page <= 0) page = 1;
  61. Map<String, Object> map = new HashMap<String, Object>(3);
  62. map.put("page", page);
  63. map.put("rows", data);
  64. if (data instanceof PaginationList) {
  65. map.put("total", ((PaginationList)data).getTotal());
  66. } else {
  67. map.put("total", data.size());
  68. }
  69. return toJson(map);
  70. }
  71. public static String getJsons(Object object) {
  72. ObjectMapper mappers = new ObjectMapper();
  73. String SIMPLE_DATETIME_FORMAT = "yyyy-MM-dd";
  74. SimpleDateFormat df = new SimpleDateFormat(SIMPLE_DATETIME_FORMAT);
  75. mappers.configure(org.codehaus.jackson.map.SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
  76. mappers.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
  77. mappers.getSerializationConfig().setDateFormat(df);
  78. try {
  79. return mappers.writeValueAsString(object);
  80. } catch (Exception e) {
  81. throw new RuntimeException(e);
  82. }
  83. }
  84. /**
  85. * 转换为grid数据
  86. * @param data
  87. * @param page
  88. * @return
  89. */
  90. @SuppressWarnings("unchecked")
  91. public static String wrapListForGrid(List data, int page,List vos) {
  92. if (page <= 0) page = 1;
  93. Map<String, Object> map = new HashMap<String, Object>(3);
  94. map.put("page", page);
  95. map.put("rows", vos);
  96. if (data instanceof PaginationList) {
  97. map.put("total", ((PaginationList)data).getTotal());
  98. } else {
  99. map.put("total", data.size());
  100. }
  101. return toJson(map);
  102. }
  103. }