Sometimes I want to use JSON in a GhidraScript, but Ghidra only uses json-simple at build time...it is not available at runtime. I think it can be included in Framework Generic.
There is the JSONParser although it isn't that fun to use and is not intuitive. This is an example of using it
@SuppressWarnings("unchecked")
public static Map<String, Object> parseJson(String jsonData) throws Exception {
JSONParser parser = new JSONParser();
LinkedList<JSONToken> tokens = new LinkedList<>();
switch (parser.parse(jsonData.toCharArray(), tokens)) {
case JSMN_ERROR_INVAL:
throw new Exception("JSON contains invalid character");
case JSMN_ERROR_NOMEM:
throw new Exception("Not enough tokens");
case JSMN_ERROR_PART:
throw new Exception("Malformed or missing JSON data");
case JSMN_SUCCESS:
break;
}
return (Map<String, Object>) parser.convert(jsonData.toCharArray(), tokens);
}
The reason for the unchecked conversion to String keys is because the json requires a key to be a string. https://en.wikipedia.org/wiki/JSON#Data_types_and_syntax
Didn't know about that...thanks. Is there anything to take a map and produce a JSON string?
Didn't know about that...thanks. Is there anything to take a map and produce a JSON string?
Actually I don't think there is. To be honest I would love to see this parser replaced with something much more user friendly.
Of course there are existing alternatives, but none I'd actually recommend. Bad alternative
I've used json-simple before (for Ghidra and other things). It's pretty good.
FYI, if you simply wish to turn Java objects to pretty JSON strings, then you can use our new generic.json.Json class's toString() methods.
I was looking to go both directions with it.
I know this is closed, but just wanted to say thank you for doing this! Adding a way to import GSON saves me from making most of my Ghidra script's into full extensions (since that was the best way to bring that JAR along).
if i remember well i think i saw google's gson in the path of ghidra 9.2. Pretty handy lib easy to use.
there is always the option to add a jar : via plugin path

if i remember well i think i saw google's gson in the path of ghidra 9.2. Pretty handy lib easy to use.
there is always the option to add a jar : via plugin path
Was just about to say this.
Anyone have a simple example for using gson? Python's json has spoiled me rotten.
Anyone have a simple example for using gson? Python's json has spoiled me rotten.
There is a trivial example in the ExportFunctionInfoScript.