Skip to content

Instantly share code, notes, and snippets.

@janakagamini
Forked from jamiechapman/ParseProxyObject.java
Last active January 4, 2016 11:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save janakagamini/f5c63ea27bee8b7b7581 to your computer and use it in GitHub Desktop.
Save janakagamini/f5c63ea27bee8b7b7581 to your computer and use it in GitHub Desktop.
Since ParseObject is not Parcelable or Serializable this class makes a best effort to make a copy of a given ParseObject that is Serializable
// By Janaka Jayasuriya, @pinkydoe
// Original By Jamie Chapman, @chappers57
// License: open, do as you wish, just don't blame me if stuff breaks ;-)
package us.peripl.app.util;
import com.parse.ParseFile;
import com.parse.ParseGeoPoint;
import com.parse.ParseObject;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
public class ParseProxyObject implements Serializable {
public static final String PARSE_LOCAL_OBJECT = "parse_local_object";
private HashMap<String, Object> values = new HashMap<String, Object>();
private HashMap<String, String> fileUrls = new HashMap<String, String>();
private HashMap<String, double[]> geoPoints = new HashMap<String, double[]>();
private String className;
private Date createdAt;
private String objectId;
private Date updatedAt;
public ParseProxyObject(ParseObject parseObject) {
for(String key : parseObject.keySet()) {
Class classType = parseObject.get(key).getClass();
if(classType == Boolean.class ||
classType == byte[].class ||
classType == Date.class ||
classType == Double.class ||
classType == Integer.class ||
classType == Long.class ||
classType == Number.class ||
classType == String.class) {
values.put(key, parseObject.get(key));
} else if (classType == ParseFile.class) {
// In the case of ParseFile, the url to the file will be retained as a String, since ParseFile is not serializable
fileUrls.put(key, ((ParseFile) parseObject.get(key)).getUrl());
} else if (classType == ParseGeoPoint.class) {
// In the case of a ParseGeoPoint, the doubles values for lat, long will be retained in a double[], since ParseGeoPoint is not serializable
double[] latlong = {((ParseGeoPoint)parseObject.get(key)).getLatitude(), ((ParseGeoPoint)parseObject.get(key)).getLongitude()};
geoPoints.put(key, latlong);
}
this.className = parseObject.getClassName();
this.createdAt = parseObject.getCreatedAt();
this.objectId = parseObject.getObjectId();
this.updatedAt = parseObject.getUpdatedAt();
}
}
public boolean getBoolean(String key) {
if(values.containsKey(key)) {
return (Boolean)values.get(key);
} else {
return false;
}
}
public byte[] getBytes(String key) {
if(values.containsKey(key)) {
return (byte[])values.get(key);
} else {
return null;
}
}
public String getClassName() {
return className;
}
public Date getCreatedAt() {
return createdAt;
}
public Date getDate(String key) {
if(values.containsKey(key)) {
return (Date)values.get(key);
} else {
return null;
}
}
public double getDouble(String key) {
if(values.containsKey(key)) {
return (Double) values.get(key);
} else {
return 0;
}
}
public int getInt(String key) {
if(values.containsKey(key)) {
return (Integer)values.get(key);
} else {
return 0;
}
}
public long getLong(String key) {
if(values.containsKey(key)) {
return (Long)values.get(key);
} else {
return 0;
}
}
public Number getNumber(String key) {
if(values.containsKey(key)) {
return (Number)values.get(key);
} else {
return null;
}
}
public String getObjectId() {
return objectId;
}
//Note: only the url to the file is returned, not an actual ParseFile
public String getParseFile(String key) {
if(fileUrls.containsKey(key)) {
return fileUrls.get(key);
} else {
return null;
}
}
// Note only the lat, long values are returned, not the actual ParseGeoPoint
// [0] latitude
// [1] longitude
public double[] getParseGeoPointArray(String key) {
if(geoPoints.containsKey(key)) {
return geoPoints.get(key);
} else {
return null;
}
}
public String getString(String key) {
if(values.containsKey(key)) {
return (String)values.get(key);
} else {
return null;
}
}
public Date getUpdatedAt() {
return updatedAt;
}
}
@hatpick
Copy link

hatpick commented Feb 24, 2015

Any solution for embeded ParseOnject? (called pointers on parse.com)

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