BowlerKernel
JsonRunner.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.scripting;
2 
3 import java.io.File;
4 import java.io.InputStream;
5 import java.lang.reflect.Type;
6 import java.util.ArrayList;
7 import java.util.Arrays;
8 import java.util.HashMap;
9 
10 import org.apache.commons.io.FileUtils;
11 import org.apache.commons.io.IOUtils;
12 
13 import com.google.gson.Gson;
14 import com.google.gson.GsonBuilder;
15 import com.google.gson.reflect.TypeToken;
16 
17 public class JsonRunner implements IScriptingLanguage {
18 
19  // Create the type, this tells GSON what datatypes to instantiate when parsing
20  // and saving the json
21  private static Type TT_mapStringString = new TypeToken<HashMap<String, HashMap<String, Object>>>() {
22  }.getType();
23  // chreat the gson object, this is the parsing factory
24  private static Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
25 
26  @Override
27  public Object inlineScriptRun(File code, ArrayList<Object> args) throws Exception {
28  String jsonString = null;
29  InputStream inPut = null;
30  inPut = FileUtils.openInputStream(code);
31  jsonString = IOUtils.toString(inPut);
32  return inlineScriptRun(jsonString, args);
33  }
34 
35  @Override
36  public Object inlineScriptRun(String code, ArrayList<Object> args) throws Exception {
37 
38  // perfoem the GSON parse
39  HashMap<String, HashMap<String, Object>> database = gson.fromJson(code, TT_mapStringString);
40  return database;
41  }
42 
43  @Override
44  public String getShellType() {
45  return "JSON";
46  }
47 
48  @Override
49  public boolean getIsTextFile() {
50  return true;
51  }
52 
58  public String getDefaultContents() {
59  return "{}";
60  }
61 
62  @Override
63  public ArrayList<String> getFileExtenetion() {
64  // TODO Auto-generated method stub
65  return new ArrayList<>(Arrays.asList("json"));
66  }
67 }
Object inlineScriptRun(String code, ArrayList< Object > args)
Definition: JsonRunner.java:36
Object inlineScriptRun(File code, ArrayList< Object > args)
Definition: JsonRunner.java:27