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

Convert a Property file data/object to JSONObject and vice versa #82

Closed
wants to merge 1 commit into from

Conversation

kensenjohn
Copy link

Hello Douglas,
I have a been using your code for a quiet some time now. I wrote a simple Property file converter. Thought I will share it.

Currently there is no way for converting java.util.Properties into a JSONObject.
Added two new methods to
a) JSONObject toJSONObject(java.util.Properties properties) // convert java.util.Properties to JSONObject
b) java.util.Properties toProperties( JSONObject jo ) // convert JSONObject to java.util.Properties
/*
http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

    /* Example:
    sample property file  : testdata.properties
    weight=10kg
    age=
    height=5'8"
    */
    //Test Code to read property file and convert to JSON and vice versa

    try {
        // load property file
        File fh  = new File("/usr/local/testdata.properties");
        FileInputStream fis = new FileInputStream(fh);
        Properties pConfig = new Properties();
        pConfig.load(fis);

        //Convert to JSON
        JSONObject propJSON = Property.toJSONObject(pConfig);
        log("Property as JSON " + propJSON );  // output : { "weight" : "10kg","height" : "5'8\"", "age":""}

        //Convert JSON to java.util.Properties
        java.util.Properties tmpProperties = Property.toProperties(propJSON);
        log("JSON  as Property" + tmpProperties );  // This will display the name/value pair as per Java's specs.
    } catch (Exception e) {

    }

@douglascrockford
Copy link
Contributor

Thanks.

BGehrels pushed a commit to BGehrels/JSON-java that referenced this pull request Apr 29, 2020
Correct the message to match the function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants