1

I am working on a project which has a python wrapper around java code. So, there is a communication between python and java code. (I am aware of jython, but we wanted to keep each implementation separate.)

To talk from python to java, we pass a string like

{
    "1-1": {
        "Max Capacity (MB)": "393216",
        "Current Capacity (MB)": "24576",
        "Max Devices": "12",
        "Populated": "12"
    },
    "1-3": {
        "Max Capacity (MB)": "262144",
        "Current Capacity (MB)": "8192",
        "Max Devices": "32",
        "Populated": "2"
    },
    "1-2": {
        "Max Capacity (MB)": "393216",
        "Current Capacity (MB)": "4096",
        "Max Devices": "12",
        "Populated": "1"
    }
}

which is a dict in python and we pass as a string to java code.

Is there I library in java which does parsing a python dict like string to a java hash-map or equivalent for easy traversal?

Solution:

The json solution worked. Used simplejson in python to convert the dict to json format and java gson library. Here is the java part:

import java.util.Map;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class ReadFromJson {
    public static void main(String args[]) {
        Type mapOfStringObjectType = new TypeToken<Map<String, Map<String,String>>>() {}.getType();
        Gson gson = new Gson();
        Map<String, Map<String,String>> obj = gson.fromJson(jsonStringToBeRead, mapOfStringObjectType);
        System.out.println(obj);
    }
}
2
  • 1
    Maybe you should communicate via JSON? Commented Feb 26, 2013 at 0:14
  • I tried that but I am doing something like java -dname="TheAboveString" and retriving it by System.getProperty("name"), so not able to pass a well formed json.. Commented Feb 26, 2013 at 0:18

1 Answer 1

1

I tried that but I am doing something like java -dname="TheAboveString" and retriving it by System.getProperty("name"), so not able to pass a well formed json.. – kmad 15 mins ago

...and why are you passing it as a system property?

Just pipe in the JSON echo "TheAboveString" | java and dump it from stdin to a JSON parser. This should be a little easier and much safer using the subprocess module.

Sign up to request clarification or add additional context in comments.

2 Comments

its because I need to run a junit test with the "TheAboveString" argument. There was no other way to pass an argument to a single junit test.
@kmad Well, I suppose you could swap System.in with another InputStream that sends fake data. Maybe you should refactor your code so that the decoding method takes an InputStream as an argument so that it is easy to inject test data. Anyway, I don't think you should avoid JSON because you can't send strings into the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.