Skip to content

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