All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.litongjava.tio.utils.json.FastJson2Utils Maven / Gradle / Ivy

package com.litongjava.tio.utils.json;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.TypeReference;

/**
 * @author Tong Li
 */
public class FastJson2Utils {

  public static String toJson(Object object) {
    return JSON.toJSONString(object);
  }

  public static byte[] toJSONBytes(Object input) {
    return JSON.toJSONBytes(input);
  }
  /**
   * 支持传入更多 SerializerFeature
   * 

* 例如: * SerializerFeature.WriteMapNullValue 支持对 null 值字段的转换 */ public static String toJson(Object input, JSONWriter.Feature... features) { return JSON.toJSONString(input, features); } public static byte[] toJSONBytes(Object input, JSONWriter.Feature... features) { return JSON.toJSONBytes(input, features); } public static T parse(String jsonString, Class type) { return JSON.parseObject(jsonString, type); } public static T parse(byte[] input, Class type) { return JSON.parseObject(input, type); } public static JSONObject parseObject(String bodyString) { return JSON.parseObject(bodyString); } public static JSONObject parseObject(byte[] bytes) { return JSON.parseObject(bytes); } public static JSONArray parseArray(String jsonString) { return JSON.parseArray(jsonString); } public static JSONArray parseArray(byte[] input) { return JSON.parseArray(input); } public static List parseArray(String jsonString, Class clazz) { return JSON.parseArray(jsonString, clazz); } public static List parseArray(byte[] input, Class clazz) { return JSON.parseArray(input, clazz); } public static List> parseToListMap(String stringValue, Class kType, Class vType) { TypeReference> typeReference = new TypeReference>() { }; JSONArray jsonArray = JSON.parseArray(stringValue); List> listMap = new ArrayList<>(); for (int i = 0; i < jsonArray.size(); i++) { Map map = jsonArray.getJSONObject(i).to(typeReference); listMap.add(map); } return listMap; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy