package com.yeejoin.amos.boot.module.hygf.api.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

import static com.alibaba.fastjson.JSON.parseArray;


public class JsonUtils {
    //将json文件转化为Map<list<Map<>>>
    public static Map getResourceJson(Resource resource) {
        String json = null;
        try {
            json = IOUtils.toString(resource.getInputStream(), String.valueOf(StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new RuntimeException(resource + "json文件转化失败");
        }
        return JSONObject.parseObject(json, Map.class);
    }

    //将json文件转化为List<Map>
    public static  List<Map> getResourceList(Resource resource) {
        String json = null;
        try {
            json = IOUtils.toString(resource.getInputStream(), String.valueOf(StandardCharsets.UTF_8));
        } catch (IOException e) {
            throw new RuntimeException(resource + "json文件转化失败");
        }
        List<Map> list = parseArray(json, Map.class);
        return list;
    }


}
