Skip to content

Commit

Permalink
Add support for :skip-null
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanstowe committed Jan 28, 2016
1 parent 97cdae5 commit 70edcf7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion META.info
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"provides" : {
"JSON::Class" : "lib/JSON/Class.pm"
},
"version" : "0.0.3",
"version" : "0.0.4",
"test-depends" : [
"Test"
],
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ both serialisation and de-serialisation. The exact behaviour depends on that
of (JSON::Marshal)[https://github.com/jonathanstowe/JSON-Marshal] and
(JSON::Unmarshal)[https://github.com/tadzik/JSON-Unmarshal] respectively.


If the ```:skip-null``` adverb is provided to ```to-json``` all attributes
without a defined value will be ignored in serialisation. If you need
finer grained control then you should apply the ```json-skip-null```
attribute trait (defined by ```JSON::Marshal``` ) to the traits you
want to skip if they aren't defined (```:json-skip``` will still have
the same effect though.)

The (JSON::Marshal)[https://github.com/jonathanstowe/JSON-Marshal] and
(JSON::Unmarshal)[https://github.com/tadzik/JSON-Unmarshal] provide traits
for controlling the unmarshalling/marshalling of specific attributes which are
Expand Down
15 changes: 11 additions & 4 deletions lib/JSON/Class.pm
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,19 @@ type for the target class then an exception may be thrown.
=head2 method to-json
method to-json() returns Str
method to-json(Bool :$skip-null) returns Str
Serialises the public attributes of the object to a JSON string that
represents the object, this JSON can be fed to the L<from-json> of the
class to create a new object with matching (public) attributes.
If the C<:skip-null> adverb is provided all attributes without a
defined value will be ignored in serialisation. If you need finer
grained control then you should apply the C<json-skip-null> attribute
trait (defined by L<JSON::Marshal> ) to the traits you want to skip
if they aren't defined (C<:json-skip> will still have the same effect
though.)
=end pod

use JSON::Unmarshal;
Expand All @@ -73,15 +80,15 @@ sub EXPORT {
{ '&trait_mod:<is>' => &trait_mod:<is> }
}

role JSON::Class:ver<0.0.3>:auth<github:jonathanstowe> {
role JSON::Class:ver<0.0.4>:auth<github:jonathanstowe> {


method from-json(Str $json) returns JSON::Class {
unmarshal($json, self);
}

method to-json() returns Str {
marshal(self);
method to-json(Bool :$skip-null) returns Str {
marshal(self, :$skip-null);
}
}

Expand Down

0 comments on commit 70edcf7

Please sign in to comment.