-
-
Save jamiechapman/5375115 to your computer and use it in GitHub Desktop.
// 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); | |
} | |
} |
Thanks Jamie, I forked this as well to retain a couple of more stuff from a ParseObject.
Hi Jamie,
Is this class still working? I'm getting a "Cannot resolve method" for the below code:
ParseObject project = mProjects.get(position);
ParseProxyObject proxyProject = new ParseProxyObject(project);
Intent projectIntent = new Intent(ProjectListActivity.this, ProjectActivity.class);
projectIntent.putExtra("project", proxyProject);
startActivity(projectIntent);
Anyone tried with adding ParseFile in this class? if yes, then plz help a bit
Hi Fahid,
I believe that you can convert your ParseFile
Object into a byte[]
array. Once you have converted the ParseFile
Object into a byte[]
array, you can store the contents of the array within your HashMap<K, V>
.
After the fact, when attempting to retrieve the byte[]
array, you can utilise the getBytes(String K)
access method on the ParseProxyObject
Class.
I hope that helps.
Hi.
With this code I can only get attributes of a ParseObject, is there a way to retrieve the ParseObject?
Just Genius.
Thanks a lot!!
I used the idea of Unknown . Work for me for put extra images.
Hi Fahid,
I believe that you can convert your ParseFile Object into a byte[] array. Once you have converted the ParseFile Object into a byte[] array, you can store the contents of the array within your HashMap<K, V>.
After the fact, when attempting to retrieve the byte[] array, you can utilise the getBytes(String K) access method on the ParseProxyObject Class.
I hope that helps.
Maybe need to work on objectId for ParseObject
I am trying to get the UserObject but couldn't. This is what I did
if (parseProxyObject.has("seller")) {
ParseProxyObject parseUser = parseProxyObject.getParseUser(Constants.COLUMN_SELLER);
if (parseUser != null) {
//Log.e(TAG, parseProxyObject.getString("seller"));
Log.e(TAG, parseUser.has(Constants.COLUMN_NAME) ? parseUser.getString(Constants.COLUMN_NAME) : parseUser.getString(Constants.COLUMN_NAME));
}
}
Any help. Thanks
How would you retrieve a ParseObject which is an attribute of another ParseObject?
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
Hi Jamie, Thanks , helps me a lot.
I forked it and I added another attributes and methods like createdAt, updatedAt and objectId;