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

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

There is a newer version: 3.7.3.v202400213-RELEASE
Show newest version
package com.litongjava.tio.utils.json;

import java.lang.reflect.Type;
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.JSONWriter;
import com.alibaba.fastjson2.TypeReference;

/**
 * Json 转换 fastjson 实现.
 */
public class FastJson2 extends Json {

  public static FastJson2 getJson() {
    return new FastJson2();
  }

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

  @Override
  public byte[] toJsonBytes(Object object) {
    return JSON.toJSONBytes(object);
  }

  /**
   * 支持传入更多 SerializerFeature
   * 

* 例如: SerializerFeature.WriteMapNullValue 支持对 null 值字段的转换 */ public String toJson(Object object, JSONWriter.Feature... features) { return JSON.toJSONString(object, features); } public T parse(String jsonString, Class type) { return JSON.parseObject(jsonString, type); } @Override public Map parseToMap(String json) { return JSON.parseObject(json, Map.class); } @Override public Map parseToMap(String json, Class kType, Class vType) { TypeReference> typeReference = new TypeReference>() { }; Map map = JSON.parseObject(json, typeReference); return map; } @Override public Object parseObject(String jsonString) { return JSON.parseObject(jsonString); } @Override public Object parseArray(String jsonString) { return JSON.parseArray(jsonString); } @Override public 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; } @Override public Object parse(String stringValue) { return JSON.parse(stringValue); } @Override public T parse(String body, Type type) { return JSON.parseObject(body, type); } @Override public T parse(byte[] body, Type type) { return JSON.parseObject(body, type); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy