123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /*
- * ********************************************************************************************************************
- *
- * iFHS7.
- * ;BBMBMBMc rZMBMBR BMB
- * MBEr:;PBM, 7MBMMEOBB: BBB RBW
- * XK: BO SB. :SZ MBM. c;; ir BBM :FFr :SSF: ;xBMB:r iuGXv. i:. iF2;
- * DBBM0r. :D S7 ;XMBMB GMBMu. MBM: BMB MBMBBBMBMS WMBMBMBBK MBMBMBM BMBRBMBW .MBMBMBMBB
- * :JMRMMD .. , 1MMRM1; ;MBMBBR: MBM ;MB: BMB: MBM. RMBr sBMH BM0 UMB, BMB. KMBv
- * ;. XOW B1; :uM: 1RE, i .2BMBs rMB. MBO MBO JMB; MBB MBM BBS 7MBMBOBM: MBW :BMc
- * OBRJ.SEE MRDOWOR, 3DE:7OBM . ;BMB RMR7BM BMB MBB. BMB ,BMR .BBZ MMB rMB, BMM rMB7
- * :FBRO0D0 RKXSXPR. JOKOOMPi BMBSSWBMB; BMBB: MBMB0ZMBMS .BMBOXRBMB MBMDE RBM2;SMBM; MBB xBM2
- * iZGE O0SHSPO. uGZ7. sBMBMBDL :BMO OZu:BMBK, rRBMB0; ,EBMB xBMBr:ER. RDU :OO;
- * ,BZ, 1D0 RPSFHXR. xWZ .SMr . .BBB
- * :0BMRDG RESSSKR. 2WOMBW; BMBMR
- * i0BM: SWKHKGO MBDv
- * .UB OOGDM. MK, Copyright (c) 2015-2019. 斯伯坦机器人
- * , XMW ..
- * r All rights reserved.
- *
- * ********************************************************************************************************************
- */
- package com.persagy.base.utils
- import com.alibaba.fastjson.JSON
- import com.alibaba.fastjson.PropertyNamingStrategy
- import com.alibaba.fastjson.serializer.SerializeConfig
- import com.alibaba.fastjson.serializer.SerializerFeature
- import java.io.*
- import java.lang.reflect.Type
- /**
- * Json工具类
- *
- * @author 庞利祥 <sybotan@126.com>
- */
- object SJsonUtil {
- private val TAG = SJsonUtil::class.java.name
- /** 序列化配置 */
- val serializeConfig = SerializeConfig()
- /**
- * 转换对象到 JSON 格式字符串
- *
- * @param obj 被转换对象
- * @param namingStrategy 命名规则
- * @return JSON 格式字符串
- */
- fun toJson(obj: Any, namingStrategy: PropertyNamingStrategy? = null): String {
- return if (null == namingStrategy) {
- JSON.toJSONString(obj, serializeConfig, SerializerFeature.DisableCircularReferenceDetect)
- } else {
- val config = SerializeConfig()
- config.propertyNamingStrategy = namingStrategy
- JSON.toJSONString(obj, config, SerializerFeature.DisableCircularReferenceDetect)
- }
- } // Fun toJson()
- /**
- * 转换对象到 JSON 格式字符串,包含空字符串
- *
- * @param obj 被转换对象
- * @param namingStrategy 首字母是否大写
- * @return json格式字符串
- */
- fun toJsonAll(obj: Any, namingStrategy: PropertyNamingStrategy? = null): String {
- return if (null == namingStrategy) {
- JSON.toJSONString(obj,
- serializeConfig,
- SerializerFeature.WriteMapNullValue,
- SerializerFeature.WriteNullListAsEmpty,
- SerializerFeature.DisableCircularReferenceDetect)
- } else {
- val config = SerializeConfig()
- config.propertyNamingStrategy = namingStrategy
- JSON.toJSONString(obj,
- config,
- SerializerFeature.WriteMapNullValue,
- SerializerFeature.WriteNullListAsEmpty,
- SerializerFeature.DisableCircularReferenceDetect)
- }
- } // Fun toJsonAll()
- /**
- * 将 JSON 数据解析成相应的映射对象
- *
- * @param jsonData 要解析的JSON数据
- * @param type Java类型
- * @return 解析结果
- */
- @Throws(Exception::class)
- fun <T> fromJson(jsonData: String, type: Class<T>): T? {
- return JSON.parseObject(jsonData, type)
- } // Fun fromJson()
- /**
- * 将Json数据解析成相应的映射对象
- *
- * @param jsonData 要解析的JSON数据
- * @param type Java类型
- * @return 解析结果
- */
- @Throws(Exception::class)
- fun fromJson(jsonData: String, type: Type): Any? {
- return JSON.parseObject(jsonData, type)
- } // Fun fromJson()
- /**
- * 将Json数据解析成相应的映射对象
- *
- * @param input 要解析的JSON数据输入流
- * @param type Java类型
- * @param namingStrategy 命名规则
- * @return 解析结果
- */
- @Throws(Exception::class)
- fun <T> fromJson(input: InputStream, type: Class<T>): T? {
- val count = input.available()
- val buf = ByteArray(count)
- var readCount = 0
- while (readCount < count) {
- readCount += input.read(buf, readCount, count - readCount)
- }
- return fromJson(String(buf), type)
- } // Fun fromJson()
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /**
- * 将Json数据解析成相应的映射对象
- *
- * @param jsonData 要解析的JSON数据
- * @param namingStrategy 命名规则
- * @return 解析结果
- */
- @Throws(Exception::class)
- inline fun <reified T> fromJson(jsonData: String, namingStrategy: PropertyNamingStrategy? = null): T {
- return JSON.parseObject(jsonData, T::class.java)
- } // Fun parseJson()
- /**
- * 将Json数据解析成相应的映射对象
- *
- * @param input 要解析的JSON数据输入流
- * @param namingStrategy 命名规则
- * @return 解析结果
- */
- @Throws(Exception::class)
- inline fun <reified T> fromJson(input: InputStream, namingStrategy: PropertyNamingStrategy? = null): T {
- val count = input.available()
- val buf = ByteArray(count)
- var readCount = 0
- while (readCount < count) {
- readCount += input.read(buf, readCount, count - readCount)
- }
- return fromJson(String(buf), namingStrategy)
- } // Fun fromJson()
- } // Class SJsonUtil
|