From 70edcf76545c4ecf747c22d89e818db745a5b497 Mon Sep 17 00:00:00 2001 From: Jonathan Stowe Date: Thu, 28 Jan 2016 10:59:10 +0000 Subject: [PATCH] Add support for :skip-null --- META.info | 2 +- README.md | 8 ++++++++ lib/JSON/Class.pm | 15 +++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/META.info b/META.info index d10316c..5f9fb78 100644 --- a/META.info +++ b/META.info @@ -2,7 +2,7 @@ "provides" : { "JSON::Class" : "lib/JSON/Class.pm" }, - "version" : "0.0.3", + "version" : "0.0.4", "test-depends" : [ "Test" ], diff --git a/README.md b/README.md index bb45167..6701751 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/JSON/Class.pm b/lib/JSON/Class.pm index 1ac8905..f9d3b57 100644 --- a/lib/JSON/Class.pm +++ b/lib/JSON/Class.pm @@ -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 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 attribute +trait (defined by L ) 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; @@ -73,15 +80,15 @@ sub EXPORT { { '&trait_mod:' => &trait_mod: } } -role JSON::Class:ver<0.0.3>:auth { +role JSON::Class:ver<0.0.4>:auth { 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); } }