package com.wacai.sdk.jtr;

import com.alibaba.fastjson.JSONReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class JsonModel implements Serializable {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final File jsonDir;

    public JsonModel(File jsonDir) { this.jsonDir = jsonDir;}

    @SuppressWarnings("unchecked")
    public Map<String, Object> load(String path, String queryString) throws FileNotFoundException {
        String name = get(queryString, "m", RequestPath.base(path));
        final File file = new File(jsonDir, name + ".json");
        if (!file.exists()) {
            logger.warn("Missing model file {}", file);
            return new HashMap<>();
        }

        try (JSONReader reader = new JSONReader(new FileReader(file))) {
            final Map<String, Object> model = (Map<String, Object>) reader.readObject(new HashMap<String, Object>());
            logger.info("Inject model {}", model);
            return model;
        }
    }

    static String get(String queryString, String key, String defaultValue) {
        if (queryString == null) return defaultValue;
        final int i = queryString.indexOf(key);
        if (i < 0) return defaultValue;
        final int j = queryString.indexOf('&', i);
        return queryString.substring(i + key.length() + 1, j < 0 ? queryString.length() : j);
    }
}
@@ -21,16 +21,56 @@
 k.jtr;%0A%0A
+import com.alibaba.fastjson.JSONObject;%0A
 import c
@@ -1032,56 +1032,39 @@
 nal 
-Map%3CString, Object%3E model = (Map%3CString, 
+JSONObject object = (JSON
 Object
-%3E
 ) re
@@ -1083,37 +1083,8 @@
 ect(
-new HashMap%3CString, Object%3E()
 );%0A 
@@ -1125,21 +1125,22 @@
 el %7B%7D%22, 
-model
+object
 );%0A     
@@ -1153,21 +1153,22 @@
  return 
-model
+object
 ;%0A      

Clean code.