douglascrockford/JSON-java — pull request #82 by kensenjohn (2013-05-25 05:41:50+00:00)
Title: Convert a Property file data/object to JSONObject and vice versa

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

}