JSON嵌套支持

Oryx采用JSON用来描述模型。虽然JSON简单,但是动不动几千行的代码实在看的眼花缭乱。更何况有相当多的重复描述,改起来非常不便,一改要改好几个地方。顺手写了一个可以处理嵌套JSON的JAVA类,如在属性中有”$name.json”,表示将name.json文件包含进来,name.json可以是一个JSONObject也可以是一个JSONArray。多重嵌套也支持。

代码如下:

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
 * JSON is a nice format. But it does not support nest format. The size of JSON
 * file blows very often. This class is used for nest JSON file. All nest JSON
 * must be a string with format "$.*json".
 */
public class NestJSON {
  public static void main(String[] args) throws IOException, JSONException {
    String mainFile = "main.json";
    String outputFile = "WebContent/stencilsets/myflow/myflow.json";
    String dir = "WebContent/stencilsets/myflow/";
    NestJSON nest = new NestJSON();

    String outputData = nest.expandJSONObject(dir, mainFile);
    File output = new File(outputFile);
    FileUtils.writeStringToFile(output, outputData);
    
    System.out.println("expend flyflow JSON module successfully");
  }

  /**
   * expend the JSON object specified in file @mainFile.
   * 
   * referenced files can use three formats.
   * All referenced file use absolute path
   * All referenced file in current path
   * All referenced file in directory @dir
   * @return expended JSON object string
   * @throws IOException
   *             if any resource does not exist
   * @throws JSONException
   *             if any resource is not JSON object and array
   */
  public String expandJSONObject(String dir, String mainFile)
      throws IOException, JSONException {
    String data = FileUtils.readFileToString(new File(dir + mainFile));
    JSONObject json = new JSONObject(data);

    walk(dir, json);
    return json.toString(3);
  }

  /**
   * expend the JSON array specified in file @mainFile.
   * 
   * referenced files can use three formats.
   * All referenced file use absolute path
   * All referenced file in current path
   * All referenced file in directory @dir
   * 
   * @return expended JSON object string
   * @throws IOException
   *             if any resource does not exist
   * @throws JSONException
   *             if any resource is not JSON object and array
   */
  public String expandJSONArray(String dir, String mainJSON)
      throws IOException, JSONException {
    String data = FileUtils.readFileToString(new File(dir + mainJSON));
    JSONArray json = new JSONArray(data);

    walk(dir, json);
    return json.toString(3);
  }

  /**
   * try to get JSON object and walk through it.
   * 
   * @return JSON string. null if the @s is not a nest JSON file
   * @throws IOException
   *             if any resource does not exist
   * @throws JSONException
   *             if any resource is not JSON object and array
   */
  private Object getJSON(String dir, String s) throws IOException,
      JSONException {
    Object obj = null;
    if (s.startsWith("$") && s.endsWith("json")) {
      String data = FileUtils.readFileToString(new File(dir, s
          .substring(1)));
      try {
        obj = new JSONObject(data);
        walk(dir, (JSONObject) obj);
      } catch (JSONException e) {
        obj = new JSONArray(data);
        walk(dir, (JSONArray) obj);
      }
    }
    return obj;
  }

  /**
   * walk through the @json object. expend it when meet any reference
   * 
   * @throws IOException
   *             if any resource does not exist
   * @throws JSONException
   *             if any resource is not JSON object and array
   */
  @SuppressWarnings("unchecked")
  private void walk(String dir, JSONObject json) throws JSONException,
      IOException {
    for (Iterator it = json.keys(); it.hasNext();) {
      String key = it.next();
      Object obj = json.get(key);
      if (obj instanceof String) {
        String s = (String) obj;
        Object value = getJSON(dir, s);
        if (value != null) {
          json.put(key, value);
        }
      } else if (obj instanceof JSONObject) {
        walk(dir, (JSONObject) obj);
      } else if (obj instanceof JSONArray) {
        walk(dir, (JSONArray) obj);
      }
    }
  }

  /**
   * walk through the @json object. expend it when meet any reference
   * 
   * @throws IOException
   *             if any resource does not exist
   * @throws JSONException
   *             if any resource is not JSON object and array
   */
  private void walk(String dir, JSONArray json) throws JSONException,
      IOException {
    for (int i = 0; i < json.length(); ++i) {
      Object obj = json.get(i);
      if (obj instanceof String) {
        String s = (String) obj;
        Object value = getJSON(dir, s);
        if (value != null) {
          json.put(i, value);
        }

      } else if (obj instanceof JSONObject) {
        walk(dir, (JSONObject) obj);
      }
    }
  }
}

Your email will never published nor shared. Required fields are marked *...

Type your comment out: