SJsonUtil.kt 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * ********************************************************************************************************************
  3. *
  4. * iFHS7.
  5. * ;BBMBMBMc rZMBMBR BMB
  6. * MBEr:;PBM, 7MBMMEOBB: BBB RBW
  7. * XK: BO SB. :SZ MBM. c;; ir BBM :FFr :SSF: ;xBMB:r iuGXv. i:. iF2;
  8. * DBBM0r. :D S7 ;XMBMB GMBMu. MBM: BMB MBMBBBMBMS WMBMBMBBK MBMBMBM BMBRBMBW .MBMBMBMBB
  9. * :JMRMMD .. , 1MMRM1; ;MBMBBR: MBM ;MB: BMB: MBM. RMBr sBMH BM0 UMB, BMB. KMBv
  10. * ;. XOW B1; :uM: 1RE, i .2BMBs rMB. MBO MBO JMB; MBB MBM BBS 7MBMBOBM: MBW :BMc
  11. * OBRJ.SEE MRDOWOR, 3DE:7OBM . ;BMB RMR7BM BMB MBB. BMB ,BMR .BBZ MMB rMB, BMM rMB7
  12. * :FBRO0D0 RKXSXPR. JOKOOMPi BMBSSWBMB; BMBB: MBMB0ZMBMS .BMBOXRBMB MBMDE RBM2;SMBM; MBB xBM2
  13. * iZGE O0SHSPO. uGZ7. sBMBMBDL :BMO OZu:BMBK, rRBMB0; ,EBMB xBMBr:ER. RDU :OO;
  14. * ,BZ, 1D0 RPSFHXR. xWZ .SMr . .BBB
  15. * :0BMRDG RESSSKR. 2WOMBW; BMBMR
  16. * i0BM: SWKHKGO MBDv
  17. * .UB OOGDM. MK, Copyright (c) 2015-2019. 斯伯坦机器人
  18. * , XMW ..
  19. * r All rights reserved.
  20. *
  21. * ********************************************************************************************************************
  22. */
  23. package com.persagy.base.utils
  24. import com.alibaba.fastjson.JSON
  25. import com.alibaba.fastjson.PropertyNamingStrategy
  26. import com.alibaba.fastjson.serializer.SerializeConfig
  27. import com.alibaba.fastjson.serializer.SerializerFeature
  28. import java.io.*
  29. import java.lang.reflect.Type
  30. /**
  31. * Json工具类
  32. *
  33. * @author 庞利祥 <sybotan@126.com>
  34. */
  35. object SJsonUtil {
  36. private val TAG = SJsonUtil::class.java.name
  37. /** 序列化配置 */
  38. val serializeConfig = SerializeConfig()
  39. /**
  40. * 转换对象到 JSON 格式字符串
  41. *
  42. * @param obj 被转换对象
  43. * @param namingStrategy 命名规则
  44. * @return JSON 格式字符串
  45. */
  46. fun toJson(obj: Any, namingStrategy: PropertyNamingStrategy? = null): String {
  47. return if (null == namingStrategy) {
  48. JSON.toJSONString(obj, serializeConfig, SerializerFeature.DisableCircularReferenceDetect)
  49. } else {
  50. val config = SerializeConfig()
  51. config.propertyNamingStrategy = namingStrategy
  52. JSON.toJSONString(obj, config, SerializerFeature.DisableCircularReferenceDetect)
  53. }
  54. } // Fun toJson()
  55. /**
  56. * 转换对象到 JSON 格式字符串,包含空字符串
  57. *
  58. * @param obj 被转换对象
  59. * @param namingStrategy 首字母是否大写
  60. * @return json格式字符串
  61. */
  62. fun toJsonAll(obj: Any, namingStrategy: PropertyNamingStrategy? = null): String {
  63. return if (null == namingStrategy) {
  64. JSON.toJSONString(obj,
  65. serializeConfig,
  66. SerializerFeature.WriteMapNullValue,
  67. SerializerFeature.WriteNullListAsEmpty,
  68. SerializerFeature.DisableCircularReferenceDetect)
  69. } else {
  70. val config = SerializeConfig()
  71. config.propertyNamingStrategy = namingStrategy
  72. JSON.toJSONString(obj,
  73. config,
  74. SerializerFeature.WriteMapNullValue,
  75. SerializerFeature.WriteNullListAsEmpty,
  76. SerializerFeature.DisableCircularReferenceDetect)
  77. }
  78. } // Fun toJsonAll()
  79. /**
  80. * 将 JSON 数据解析成相应的映射对象
  81. *
  82. * @param jsonData 要解析的JSON数据
  83. * @param type Java类型
  84. * @return 解析结果
  85. */
  86. @Throws(Exception::class)
  87. fun <T> fromJson(jsonData: String, type: Class<T>): T? {
  88. return JSON.parseObject(jsonData, type)
  89. } // Fun fromJson()
  90. /**
  91. * 将Json数据解析成相应的映射对象
  92. *
  93. * @param jsonData 要解析的JSON数据
  94. * @param type Java类型
  95. * @return 解析结果
  96. */
  97. @Throws(Exception::class)
  98. fun fromJson(jsonData: String, type: Type): Any? {
  99. return JSON.parseObject(jsonData, type)
  100. } // Fun fromJson()
  101. /**
  102. * 将Json数据解析成相应的映射对象
  103. *
  104. * @param input 要解析的JSON数据输入流
  105. * @param type Java类型
  106. * @param namingStrategy 命名规则
  107. * @return 解析结果
  108. */
  109. @Throws(Exception::class)
  110. fun <T> fromJson(input: InputStream, type: Class<T>): T? {
  111. val count = input.available()
  112. val buf = ByteArray(count)
  113. var readCount = 0
  114. while (readCount < count) {
  115. readCount += input.read(buf, readCount, count - readCount)
  116. }
  117. return fromJson(String(buf), type)
  118. } // Fun fromJson()
  119. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  120. /**
  121. * 将Json数据解析成相应的映射对象
  122. *
  123. * @param jsonData 要解析的JSON数据
  124. * @param namingStrategy 命名规则
  125. * @return 解析结果
  126. */
  127. @Throws(Exception::class)
  128. inline fun <reified T> fromJson(jsonData: String, namingStrategy: PropertyNamingStrategy? = null): T {
  129. return JSON.parseObject(jsonData, T::class.java)
  130. } // Fun parseJson()
  131. /**
  132. * 将Json数据解析成相应的映射对象
  133. *
  134. * @param input 要解析的JSON数据输入流
  135. * @param namingStrategy 命名规则
  136. * @return 解析结果
  137. */
  138. @Throws(Exception::class)
  139. inline fun <reified T> fromJson(input: InputStream, namingStrategy: PropertyNamingStrategy? = null): T {
  140. val count = input.available()
  141. val buf = ByteArray(count)
  142. var readCount = 0
  143. while (readCount < count) {
  144. readCount += input.read(buf, readCount, count - readCount)
  145. }
  146. return fromJson(String(buf), namingStrategy)
  147. } // Fun fromJson()
  148. } // Class SJsonUtil