Skip to content

Commit 70edcf7

Browse files
committed
Add support for :skip-null
1 parent 97cdae5 commit 70edcf7

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

META.info

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"provides" : {
33
"JSON::Class" : "lib/JSON/Class.pm"
44
},
5-
"version" : "0.0.3",
5+
"version" : "0.0.4",
66
"test-depends" : [
77
"Test"
88
],

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ both serialisation and de-serialisation. The exact behaviour depends on that
3333
of (JSON::Marshal)[https://github.com/jonathanstowe/JSON-Marshal] and
3434
(JSON::Unmarshal)[https://github.com/tadzik/JSON-Unmarshal] respectively.
3535

36+
37+
If the ```:skip-null``` adverb is provided to ```to-json``` all attributes
38+
without a defined value will be ignored in serialisation. If you need
39+
finer grained control then you should apply the ```json-skip-null```
40+
attribute trait (defined by ```JSON::Marshal``` ) to the traits you
41+
want to skip if they aren't defined (```:json-skip``` will still have
42+
the same effect though.)
43+
3644
The (JSON::Marshal)[https://github.com/jonathanstowe/JSON-Marshal] and
3745
(JSON::Unmarshal)[https://github.com/tadzik/JSON-Unmarshal] provide traits
3846
for controlling the unmarshalling/marshalling of specific attributes which are

lib/JSON/Class.pm

+11-4
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,19 @@ type for the target class then an exception may be thrown.
5858
5959
=head2 method to-json
6060
61-
method to-json() returns Str
61+
method to-json(Bool :$skip-null) returns Str
6262
6363
Serialises the public attributes of the object to a JSON string that
6464
represents the object, this JSON can be fed to the L<from-json> of the
6565
class to create a new object with matching (public) attributes.
6666
67+
If the C<:skip-null> adverb is provided all attributes without a
68+
defined value will be ignored in serialisation. If you need finer
69+
grained control then you should apply the C<json-skip-null> attribute
70+
trait (defined by L<JSON::Marshal> ) to the traits you want to skip
71+
if they aren't defined (C<:json-skip> will still have the same effect
72+
though.)
73+
6774
=end pod
6875

6976
use JSON::Unmarshal;
@@ -73,15 +80,15 @@ sub EXPORT {
7380
{ '&trait_mod:<is>' => &trait_mod:<is> }
7481
}
7582

76-
role JSON::Class:ver<0.0.3>:auth<github:jonathanstowe> {
83+
role JSON::Class:ver<0.0.4>:auth<github:jonathanstowe> {
7784

7885

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

83-
method to-json() returns Str {
84-
marshal(self);
90+
method to-json(Bool :$skip-null) returns Str {
91+
marshal(self, :$skip-null);
8592
}
8693
}
8794

0 commit comments

Comments
 (0)