Skip to content

Instantly share code, notes, and snippets.

@madhephaestus
Last active November 12, 2024 14:58
Show Gist options
  • Save madhephaestus/aa464b39c1208c357b8d4ae7fe210bbb to your computer and use it in GitHub Desktop.
Save madhephaestus/aa464b39c1208c357b8d4ae7fe210bbb to your computer and use it in GitHub Desktop.
gsonExample.groovy
/.project
/.classpath
/.cproject
/cache/
/*.class
{
"DataSetOne": {
"keyThree": "Data in secion 3",
"keyTwo": 2.0,
"keyOne": 1.0
},
"NewDataset": {
"myOtherKey": 37.42,
"myNewKey": "MyValue"
},
"DataSetTwo": {
"keyThree": "Data in secion 3",
"keyTwo": 2.0,
"keyOne": 1.0
}
}
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine
import java.lang.reflect.Type;
String myURL = "https://gist.github.com/aa464b39c1208c357b8d4ae7fe210bbb.git"
String filename = "data.json"
//GSON parts
Type TT_mapStringString = new TypeToken<HashMap<String, HashMap<String, Object>>>() {}.getType();
Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
// load daata from a Git json
HashMap<String, HashMap<String, Object>> database = ScriptingEngine.gitScriptRun(
myURL,
filename,null)
//Alternate method to load from a string
//File cachejson = ScriptingEngine.fileFromGit(myURL, filename)
//String jsonString = null;
//InputStream inPut = null;
//inPut = FileUtils.openInputStream(cachejson);
//jsonString = IOUtils.toString(inPut);
//HashMap<String, HashMap<String, Object>> database = gson.fromJson(jsonString, TT_mapStringString);
// Reading Data
for(String key:database.keySet()) {
HashMap<String, Object> data = database.get(key)
println "Loading Data from set: "+key
for(String keyLevel2:data.keySet()){
println "\tContent at: "+keyLevel2+" is "+data.get(keyLevel2)
}
}
// Writing data
HashMap<String, Object> newData = new HashMap<>()
newData.put("myNewKey","MyValue")
newData.put("myOtherKey",37.42)
database.put("NewDataset",newData)
String writeOut = gson.toJson(database, TT_mapStringString);
println "New database JSON content = \n\n"+writeOut
ScriptingEngine.pushCodeToGit(myURL, null, filename, writeOut, "saving CSG database", false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment