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

com.litongjava.utils.projectvariable.JsonSerilizable Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.litongjava.utils.projectvariable;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class JsonSerilizable {

  /* 将链表序列化为字符串存入json文件中 */
  public static String serilizableForList(Object objList, String OutfilePathName) throws IOException {
    String listString = JSON.toJSONString(objList, true);// (maps,CityEntity.class);
    FileUtil.writeFile(OutfilePathName, listString);
    return listString;
  }

  /* 将json文件中的内容读取出来,反序列化为链表 */
  public static  List deserilizableForListFromFile(String InputfilePathName, Class clazz) throws IOException {
    String listString2 = FileUtil.readFile(InputfilePathName);
    List list2 = JSON.parseArray(listString2, clazz);
    return list2;
  }

  /* 将HashMap序列化为字符串存入json文件中 */
  public static String serilizableForMap(Object objMap, String OutfilePathName) throws IOException {
    String listString = JSON.toJSONString(objMap, true);// (maps,CityEntity.class);
    FileUtil.writeFile(OutfilePathName, listString);
    return listString;
  }

  /* 将json文件中的内容读取出来,反序列化为HashMap */
  public static  Map deserilizableForMapFromFile(String jsonStringFilePath, Class k, Class v)
      throws IOException {
    String jsonString = FileUtil.readFile(jsonStringFilePath);
    TypeReference> typeReference = new TypeReference>(k, v) {
    };
    Map map = JSON.parseObject(jsonString, typeReference);
    return map;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy