Last active
March 14, 2019 07:37
-
-
Save jamiechapman/5375115 to your computer and use it in GitHub Desktop.
A Parse.com Serializable ParseObject Proxy
This file contains 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
// By Jamie Chapman, @chappers57 | |
// License: open, do as you wish, just don't blame me if stuff breaks ;-) | |
public class ParseProxyObject implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private HashMap<String, Object> values = new HashMap<String, Object>(); | |
public HashMap<String, Object> getValues() { | |
return values; | |
} | |
public void setValues(HashMap<String, Object> values) { | |
this.values = values; | |
} | |
public ParseProxyObject(ParseObject object) { | |
// Loop the keys in the ParseObject | |
for(String key : object.keySet()) { | |
@SuppressWarnings("rawtypes") | |
Class classType = object.get(key).getClass(); | |
if(classType == byte[].class || classType == String.class || | |
classType == Integer.class || classType == Boolean.class) { | |
values.put(key, object.get(key)); | |
} else if(classType == ParseUser.class) { | |
ParseProxyObject parseUserObject = new ParseProxyObject((ParseObject)object.get(key)); | |
values.put(key, parseUserObject); | |
} else { | |
// You might want to add more conditions here, for embedded ParseObject, ParseFile, etc. | |
} | |
} | |
} | |
public String getString(String key) { | |
if(has(key)) { | |
return (String) values.get(key); | |
} else { | |
return ""; | |
} | |
} | |
public int getInt(String key) { | |
if(has(key)) { | |
return (Integer)values.get(key); | |
} else { | |
return 0; | |
} | |
} | |
public Boolean getBoolean(String key) { | |
if(has(key)) { | |
return (Boolean)values.get(key); | |
} else { | |
return false; | |
} | |
} | |
public byte[] getBytes(String key) { | |
if(has(key)) { | |
return (byte[])values.get(key); | |
} else { | |
return new byte[0]; | |
} | |
} | |
public ParseProxyObject getParseUser(String key) { | |
if(has(key)) { | |
return (ParseProxyObject) values.get(key); | |
} else { | |
return null; | |
} | |
} | |
public Boolean has(String key) { | |
return values.containsKey(key); | |
} | |
} |
Hi.
With this code I can only get attributes of a ParseObject, is there a way to retrieve the ParseObject?
add the following API to parseObject class:
- change method from package to public:
public static <T extends ParseObject> T fromJSON(JSONObject json, String defaultClassName, boolean isComplete) {
//...
}
- add toJSONObject method:
public JSONObject toJSONObject(){
return toRest(new ParseEncoder() {
// see OfflineStore
@Override
protected JSONObject encodeRelatedObject(ParseObject object) {
try {
if (object.getObjectId() != null) {
JSONObject result = new JSONObject();
result.put("__type", "Pointer");
result.put("objectId", object.getObjectId());
result.put("className", object.getClassName());
return result;
}
} catch (JSONException e) {
// This can literally never happen.
throw new RuntimeException(e);
}
return new JSONObject();
}
});
}
- sender
ParseObject p = ...;
Fragment f = new ChatFragment();
Bundle b = new Bundle();
b.putString("CONNECTION", p.toJSONObject().toString());
f.setArguments(b);
- receiver
JSONObject object = new JSONObject(b.getString("CONNECTION")); // same as bundle key
ParseObject mConnection = ParseObject.fromJSON(object, "CONNECTION", true); // change class name
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How would you retrieve a ParseObject which is an attribute of another ParseObject?