Last active
August 29, 2015 14:24
-
-
Save lukaszjanyga/87692cc33a625f15a55a to your computer and use it in GitHub Desktop.
Parser converts any ParseObject to JSONObject or JSONString and vice versa. * Reflection is used for maintaining type compatibility. * Each ParseObject property value is stored as JSONArray [type, value] under given key. * Created by Łukasz Janyga on 2015-07-07.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parser converts any ParseObject to JSONObject or JSONString and vice versa. | |
* Reflection is used for maintaining type compatibility. | |
* Each ParseObject property value is stored as JSONArray [type, value] under given key. | |
* Created by Łukasz Janyga on 2015-07-07. | |
*/ | |
public class PObjectToJsonParser { | |
public static JSONObject toJsonObject(ParseObject parseObject) { | |
JSONObject jsonObject = new JSONObject(); | |
try { | |
jsonObject.put("_parseClassName", parseObject.getClassName()); | |
for (String key : parseObject.keySet()) { | |
try { | |
JSONArray jsonArray = new JSONArray(); | |
jsonArray.put(parseObject.get(key).getClass().getName()); | |
jsonArray.put(parseObject.get(key)); | |
jsonObject.put(key, jsonArray); | |
} catch (JSONException jsonException) { | |
jsonException.printStackTrace(); | |
} | |
} | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return jsonObject; | |
} | |
public static String toJsonString(ParseObject parseObject) { | |
String jsonString = ""; | |
jsonString = toJsonObject(parseObject).toString(); | |
return jsonString; | |
} | |
public static ParseObject fromJsonObject(JSONObject jsonObject) { | |
ParseObject parseObject = null; | |
Iterator jsonIterator = jsonObject.keys(); | |
try { | |
parseObject = ParseObject.create(jsonObject.getString("_parseClassName")); | |
while (jsonIterator.hasNext()) { | |
String key = (String) jsonIterator.next(); | |
if (!key.equals("_parseClassName")) { | |
Object object = createObjectOfClass(jsonObject.getJSONArray(key).getString(0), jsonObject.getJSONArray(key).getString(1)); | |
parseObject.put(key, object); | |
} | |
} | |
} catch (JSONException jsonException) { | |
jsonException.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return parseObject; | |
} | |
public static ParseObject fromJsonString(String jsonString) { | |
ParseObject parseObject = null; | |
try { | |
JSONObject jsonObject = new JSONObject(jsonString); | |
parseObject = fromJsonObject(jsonObject); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return parseObject; | |
} | |
public static Object createObjectOfClass(String className, Object argument) { | |
try { | |
Class<?> cl = Class.forName(className); | |
Constructor<?> ctor = cl.getConstructor(String.class); | |
return ctor.newInstance(argument); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment