ExtJsonUtils.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package com.jsh.erp.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson.parser.ParserConfig;
  5. import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
  6. import com.alibaba.fastjson.parser.deserializer.FieldDeserializer;
  7. import com.alibaba.fastjson.serializer.*;
  8. import java.io.IOException;
  9. import java.lang.reflect.Type;
  10. import java.util.HashMap;
  11. import java.util.HashSet;
  12. import java.util.Map;
  13. import java.util.Set;
  14. /**
  15. * @author longyong qq752718920 2018-10-7 15:26:27
  16. */
  17. public class ExtJsonUtils {
  18. private static class NPFloatCodec extends FloatCodec {
  19. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  20. SerializeWriter out = serializer.getWriter();
  21. if (object == null) {
  22. if (serializer.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
  23. out.write('0');
  24. } else {
  25. out.writeNull();
  26. }
  27. return;
  28. }
  29. float floatValue = (Float) object;
  30. if (Float.isNaN(floatValue)) {
  31. out.writeNull();
  32. } else if (Float.isInfinite(floatValue)) {
  33. out.writeNull();
  34. } else {
  35. String floatText = Float.toString(floatValue);
  36. out.write(floatText);
  37. if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
  38. out.write('F');
  39. }
  40. }
  41. }
  42. }
  43. private static class NPDoubleSerializer extends DoubleSerializer {
  44. public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType) throws IOException {
  45. SerializeWriter out = serializer.getWriter();
  46. if (object == null) {
  47. if (!serializer.isEnabled(SerializerFeature.WriteNullNumberAsZero)) {
  48. out.writeNull();
  49. } else {
  50. out.write('0');
  51. }
  52. return;
  53. }
  54. double doubleValue = (Double) object;
  55. if (Double.isNaN(doubleValue)) {
  56. out.writeNull();
  57. } else if (Double.isInfinite(doubleValue)) {
  58. out.writeNull();
  59. } else {
  60. String doubleText;
  61. doubleText = Double.toString(doubleValue);
  62. out.append(doubleText);
  63. if (serializer.isEnabled(SerializerFeature.WriteClassName)) {
  64. out.write('D');
  65. }
  66. }
  67. }
  68. }
  69. private static final String EXT_NAME = "ext";
  70. static class ExtFilter extends AfterFilter implements PropertyFilter {
  71. static {
  72. SerializeConfig.getGlobalInstance().put(Float.class, new NPFloatCodec());
  73. SerializeConfig.getGlobalInstance().put(float.class, new NPFloatCodec());
  74. SerializeConfig.getGlobalInstance().put(Double.class, new NPDoubleSerializer());
  75. SerializeConfig.getGlobalInstance().put(double.class, new NPDoubleSerializer());
  76. }
  77. private Map<Object, JSONObject> map = new HashMap<>();
  78. private Map<Object, Set<String>> ignoredKey = new HashMap<>();
  79. @Override
  80. public boolean apply(Object object, String name, Object value) {
  81. if (name.equals(EXT_NAME) && value instanceof String) {
  82. map.put(object, JSON.parseObject((String) value));
  83. return false;
  84. }
  85. if (!map.containsKey(object)) {
  86. ignoredKey.put(object, new HashSet<String>());
  87. }
  88. ignoredKey.get(object).add(name);
  89. // if (value instanceof Float || value instanceof Double) {
  90. // if (!floatMap.containsKey(object)) {
  91. // floatMap.put(object, new HashMap<String, Object>());
  92. // }
  93. // floatMap.get(object).put(name, value);
  94. // return false;
  95. // }
  96. return true;
  97. }
  98. @Override
  99. public void writeAfter(Object object) {
  100. if (map.containsKey(object)) {
  101. Set<String> ignoredKeys;
  102. if (ignoredKey.containsKey(object)) {
  103. ignoredKeys = ignoredKey.get(object);
  104. } else {
  105. ignoredKeys = new HashSet<>();
  106. }
  107. for (Map.Entry<String, Object> entry : map.get(object).entrySet()) {
  108. if (!ignoredKeys.contains(entry.getKey())) {
  109. writeKeyValue(entry.getKey(), entry.getValue());
  110. }
  111. }
  112. }
  113. }
  114. }
  115. public static String toJSONString(Object object) {
  116. return JSON.toJSONString(object, new ExtFilter());
  117. }
  118. public interface ExtExtractor {
  119. String getExt(Object bean);
  120. }
  121. }