Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make json-simple available to scripts #1982

Closed
ryanmkurtz opened this issue Jun 12, 2020 · 10 comments
Closed

Make json-simple available to scripts #1982

ryanmkurtz opened this issue Jun 12, 2020 · 10 comments
Assignees
Labels
Milestone

Comments

@ryanmkurtz
Copy link
Collaborator

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.

@astrelsky
Copy link
Contributor

astrelsky commented Jun 12, 2020

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

@ryanmkurtz
Copy link
Collaborator Author

Didn't know about that...thanks. Is there anything to take a map and produce a JSON string?

@astrelsky
Copy link
Contributor

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

@ryanmkurtz
Copy link
Collaborator Author

I've used json-simple before (for Ghidra and other things). It's pretty good.

@dragonmacher
Copy link
Collaborator

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.

@ryanmkurtz
Copy link
Collaborator Author

I was looking to go both directions with it.

@dragonmacher dragonmacher self-assigned this Aug 19, 2020
@revolver-ocelot-saa
Copy link

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).

@pabx06
Copy link

pabx06 commented Nov 23, 2020

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
image

@astrelsky
Copy link
Contributor

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
image

Was just about to say this.

Anyone have a simple example for using gson? Python's json has spoiled me rotten.

@dragonmacher
Copy link
Collaborator

Anyone have a simple example for using gson? Python's json has spoiled me rotten.

There is a trivial example in the ExportFunctionInfoScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants