Skip to content

ParseObject encode/decode #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Sep 8, 2017

Conversation

montymxb
Copy link
Contributor

@montymxb montymxb commented Sep 6, 2017

This proposes to add new encode and decode to the ParseObject class. These would be separate from the previously existing ParseObject::_encode, which remains unmodified in terms of prior behavior.

This is essentially a lighter and more integrated approach as opposed just using serialize and unserialize. Through this an object could be encoded or decoded as follows.

$obj = new ParseObject('MyClass');
// set some values...

// encode
$encoded = $obj->encode();
// NOTE this is not the same as the previously existing _encode

// do something with your encoded object...

// decode
$decoded = ParseObject::decode($encoded);

Currently this will preserve saved and unsaved values, allowing you to encode an object with unsaved changes preserved until later decoded.

A few notes as to how this works with specific values:

  • Scalars are preserved as is
  • instances of DateTime are recognized and encoded separately of other objects, so that a DateTime object is returned during decoding
  • Pointers are stripped down, as in they'll have to be fetched to get data later (this is to prevent cyclical references)
  • Keep in mind objects (like instances of stdClass, but not ParseObjects, ParsePolygons, ParseGeoPoints, etc.) will be encoded to JSON if possible and returned as an array value (this is how it works currently)
  • Unsaved changes are preserved as they are, including pending additions/removals from a relation.

As it stands this is good to go. The only issue being about preserving the pending set of operations that have been applied to an object (like the aforementioned concern of unsaved additions/removals in a relation). I might still make a few changes yet, and if I have the time I may make the change to support pending ops.

This also includes a small style change to the README.

@codecov
Copy link

codecov bot commented Sep 7, 2017

Codecov Report

Merging #351 into master will increase coverage by 0.31%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #351      +/-   ##
==========================================
+ Coverage   98.61%   98.93%   +0.31%     
==========================================
  Files          35       35              
  Lines        3178     3281     +103     
==========================================
+ Hits         3134     3246     +112     
+ Misses         44       35       -9
Impacted Files Coverage Δ
src/Parse/Internal/ParseRelationOperation.php 100% <ø> (+3.63%) ⬆️
src/Parse/ParseRelation.php 100% <100%> (ø) ⬆️
src/Parse/ParseObject.php 98.09% <100%> (+0.77%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9ecd520...aa77986. Read the comment docs.

@montymxb montymxb merged commit 89f38f7 into parse-community:master Sep 8, 2017
@montymxb montymxb deleted the obj-encode-decode branch September 8, 2017 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant