Skip to content

Instantly share code, notes, and snippets.

@ahmed-alamir
Forked from jamiechapman/ParseProxyObject.java
Last active March 17, 2016 12:30
Show Gist options
  • Select an option

  • Save ahmed-alamir/abdd8a613312aa64f10a to your computer and use it in GitHub Desktop.

Select an option

Save ahmed-alamir/abdd8a613312aa64f10a to your computer and use it in GitHub Desktop.
A proxy class to simplify serializing ParseObject (Parse Android SDK).
// By Jamie Chapman, @chappers57
// Modified by Ahmed Al-Amir @alamir_ahmed
// License: open, do as you wish, just don't blame me if stuff breaks ;-)
import com.parse.ParseObject;
import com.parse.ParseUser;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
public class ParseProxyObject implements Serializable {
private static final long serialVersionUID = 1L;
private HashMap<String, Object> values = new HashMap<String, Object>();
private String objectId;
private Date createdAt;
public HashMap<String, Object> getValues() {
return values;
}
public void setValues(HashMap<String, Object> values) {
this.values = values;
}
public ParseProxyObject(ParseObject object) {
// grab object id and creation date
this.objectId = object.getObjectId();
this.createdAt = object.getCreatedAt();
// 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);
}
/**
* Get objectId
*/
public String getObjectId() {
return objectId;
}
/**
* Get createdId
*/
public Date getCreatedAt() {
return createdAt;
}
/**
* Create a ParseObject copy with the same objectId
*/
public <T extends ParseObject> T getParseObject(Class<T> className)
{
T object = ParseObject.createWithoutData(className, objectId);
for (String key : values.keySet())
object.put(key, values.get(key));
return object;
}
}
@ahmed-alamir

Copy link
Copy Markdown
Author

ParseObject, as seen in Parse Android SDK, is not serializable, meaning it cannot be [passed via Android's Intent](http://developer.android.com/reference/android/content/Intent.html#putExtra%28java.lang.String, java.io.Serializable%29).

This proxy class simply grabs the internal data from a ParseObject into a serializable format.

I added a simple function to populate back a ParseObject of a given type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment