Skip to content

Instantly share code, notes, and snippets.

@jamiechapman
Last active March 14, 2019 07:37
Show Gist options
  • Save jamiechapman/5375115 to your computer and use it in GitHub Desktop.
Save jamiechapman/5375115 to your computer and use it in GitHub Desktop.
A Parse.com Serializable ParseObject Proxy
// 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);
}
}
@ayhoung
Copy link

ayhoung commented Jun 19, 2014

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);

@FahidMirza
Copy link

Anyone tried with adding ParseFile in this class? if yes, then plz help a bit

Copy link

ghost commented May 20, 2015

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.

@kujma10-zz
Copy link

Hi.
With this code I can only get attributes of a ParseObject, is there a way to retrieve the ParseObject?

Copy link

ghost commented Jul 7, 2015

Just Genius.
Thanks a lot!!

@allysonlm
Copy link

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.

@chengbooxup
Copy link

Maybe need to work on objectId for ParseObject

@tonespy
Copy link

tonespy commented Mar 18, 2016

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

@abhijith-gururaj
Copy link

How would you retrieve a ParseObject which is an attribute of another ParseObject?

@AvishagCs
Copy link

Hi.
With this code I can only get attributes of a ParseObject, is there a way to retrieve the ParseObject?

Copy link

ghost commented Dec 29, 2016

add the following API to parseObject class:

  1. change method from package to public:
public static <T extends ParseObject> T fromJSON(JSONObject json, String defaultClassName, boolean isComplete) {
//...
}
  1. 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();
            }
        });
    }
  1. sender
ParseObject p = ...;
Fragment f = new ChatFragment();
Bundle b = new Bundle();
b.putString("CONNECTION", p.toJSONObject().toString());
f.setArguments(b);
  1. 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