Today I discovered that Android’s serializable interface is not the same as Java’s serializable interface, and they are not really compatible. The solution? Well, there are quite a few possible solutions, but one of the easier/lightweight/non-XML ways is to use JSON, a cross platform Javascript human readable data format. I personally am using the JSON-simple library, just because it was the easiest to set up in Eclipse.
I also found there weren’t many examples of how to encode into, and decode out of, JSON using Java – so here is an example. (note that you need json-simple for this example, which you can download here.)
//Runnable ‘main’ java class
package JSONExamplePackage;
import org.json.simple.parser.ParseException;
/**
*
*
* @author Ben Delaporte
*
*/
public class JSONExample {
public static void main(String[] args) {
// TODO Auto-generated method stub
TestObject firstObject = new TestObject();
firstObject.Name = “Jim”;
firstObject.ID = 5;
firstObject.Money = 78910.3;
System.out.println (“Object before being put into JSON format:(” + firstObject.Name + “,” + firstObject.ID + “,” + firstObject.Money + “)”);
org.json.simple.JSONObject firstJSON = ReturnJSONfromTestObject(firstObject);
System.out.println(“Object in JSON format, before converting to string and parsing: ” + firstJSON.toString());
//Send firstJSON to a string
String testString = firstJSON.toString();
//Now make a new JSONObject, to parse the string into
org.json.simple.JSONObject secondJSON2 = new org.json.simple.JSONObject();
try {
//Use an org.json.simple,parser.JSONParser to parse the string
org.json.simple.parser.JSONParser testParser = new org.json.simple.parser.JSONParser();
secondJSON2 = (org.json.simple.JSONObject) testParser.parse(testString);
System.out.println(“Object in JSON format, after parsing from string: ” + secondJSON2.toString());
} catch (ParseException e) {
System.out.println(“Unable to parse JSON string ” + secondJSON2.toString());
}
TestObject secondObject = ReturnTestObjectfromJSON(secondJSON2);
System.out.println (“Object after conversion from JSON to TestObject type:(” + secondObject.Name + “,” + secondObject.ID + “,” + secondObject.Money + “)”);
}
public static org.json.simple.JSONObject ReturnJSONfromTestObject(TestObject customObject)
{
org.json.simple.JSONObject jsonObject = new org.json.simple.JSONObject();
jsonObject.put(“Name”, customObject.Name);
jsonObject.put(“ID”, customObject.ID);
jsonObject.put(“Money”, customObject.Money);
return jsonObject;
}
public static TestObject ReturnTestObjectfromJSON(org.json.simple.JSONObject jsonObject)
{
TestObject customObject = new TestObject();
customObject.Name = (String) jsonObject.get(“Name”);
customObject.ID = (Long) jsonObject.get(“ID”);
customObject.Money = (Double) jsonObject.get(“Money”);
return customObject;
}
}
//TestObject class
package JSONExamplePackage;
/**
*
*
* @author Ben Delaporte
*
*/
public class TestObject {
public String Name;
//Note* on choice of (long) type.
public long ID;
public double Money;
}
//Console output
Object before being put into JSON format:(Jim,5,78910.3)
Object in JSON format, before converting to string and parsing: {“Name”:”Jim”,”Money”:78910.3,”ID”:5}
Object in JSON format, after parsing from string: {“Name”:”Jim”,”Money”:78910.3,”ID”:5}
Object after conversion from JSON to TestObject type:(Jim,5,78910.3)
//*At first, I had TestObject.ID set up as an ‘int’ type. But the parser kept returning a ‘long’ type, which was throwing a ‘long can’t convert to int’ error. So I went ahead and changed TestObject.ID to ‘long’ type. Just a heads up.
Related Articles
No user responded in this post