pax_global_header00006660000000000000000000000064144664117640014527gustar00rootroot0000000000000052 comment=47f24a8d6687b2181884b65fdc6d1dbb4e8c6f6c raku-json-marshal-0.0.25/000077500000000000000000000000001446641176400151515ustar00rootroot00000000000000raku-json-marshal-0.0.25/.github/000077500000000000000000000000001446641176400165115ustar00rootroot00000000000000raku-json-marshal-0.0.25/.github/workflows/000077500000000000000000000000001446641176400205465ustar00rootroot00000000000000raku-json-marshal-0.0.25/.github/workflows/main.yml000066400000000000000000000006411446641176400222160ustar00rootroot00000000000000name: CI on: push: branches: [ master ] pull_request: branches: [ master ] jobs: test: runs-on: ubuntu-latest container: image: rakudo-star:latest steps: - uses: actions/checkout@v2 - name: Raku version run: raku -v - name: Install dependencies run: zef install --deps-only --/test --test-depends . - name: Run tests run: zef test -v --debug . raku-json-marshal-0.0.25/.gitignore000066400000000000000000000003101446641176400171330ustar00rootroot00000000000000/blib/ /.build/ _build/ cover_db/ inc/ Build !Build/ Build.bat .last_cover_stats /Makefile /Makefile.old /MANIFEST.bak /META.yml /META.json /MYMETA.* nytprof.out /pm_to_blib *.o *.bs /_eumm/ .precomp raku-json-marshal-0.0.25/.layout000066400000000000000000000035551446641176400164770ustar00rootroot00000000000000{ "type": "layout", "children": [ { "type": "file", "name": "META6.json" }, { "type": "file", "name": "Changes" }, { "type": "file", "name": "LICENCE" }, { "children": [ { "name": "020-marshal.t", "type": "file" }, { "name": "050-skip-null.t", "type": "file" }, { "type": "file", "name": "010-use.t" }, { "name": "110-skip.t", "type": "file" }, { "name": "120-pretty.t", "type": "file" }, { "name": "130-custom-accessor.t", "type": "file" }, { "name": "140-opt-in.t", "type": "file" }, { "name": "040-json-name.t", "type": "file" }, { "name": "060-array.t", "type": "file" }, { "name": "080-type-constraints.t", "type": "file" }, { "name": "100-meta6-issue-22.t", "type": "file" }, { "name": "090-issue-8.t", "type": "file" }, { "name": "070-inheritance.t", "type": "file" }, { "type": "file", "name": "030-trait.t" } ], "type": "directory", "name": "t", "purpose": null }, { "children": [ { "name": "JSON", "type": "directory", "purpose": null, "children": [ { "type": "file", "name": "Marshal.rakumod" } ] } ], "purpose": "library", "name": "lib", "type": "directory" }, { "name": "README.md", "type": "file" } ] } raku-json-marshal-0.0.25/Changes000066400000000000000000000037571446641176400164600ustar00rootroot00000000000000v0.0.25 Mon 14 Aug 12:03:19 BST 2023 * Make skip-null work on nested object (from Vadim Belman.) * Allow some private attributes to be serialized (from Vadim Belman.) v0.0.24 Sun 7 Aug 09:48:39 BST 2022 * Upload to zef repository v0.0.23 Wed 22 Sep 10:43:22 BST 2021 * Provide for opt-in serialization v0.0.22 Sat 4 Sep 14:07:48 BST 2021 * Don't use fixed string to test pretty * Fix version of JSON::Fast v0.0.21 Tue 27 Jul 16:03:51 BST 2021 * Use accessor method if possible v0.0.20 Tue 2 Feb 08:17:07 GMT 2021 * Add "pretty" adverb to pass to JSON::Fast (from Vadim Belman.) * Switch to GH actions * A little spring cleans v0.0.19 Sun 27 Sep 18:21:50 BST 2020 * Fix for method name marshaller with undefined value * Fix for anonymous constraint on attribute v0.0.18 Thu 18 Apr 14:20:37 BST 2019 * Pass on the :sorted-keys to JSON::Fast::to-json v0.0.17 Mon 4 Mar 19:42:48 GMT 2019 * Remove :api from class module definition to appease ancient rakudo v0.0.16 Thu 17 Jan 09:36:04 GMT 2019 * Spring Clean and upload to CPAN v0.0.15 Mon 5 Nov 13:55:34 GMT 2018 * Add a json-skip trait to inhibit marshalling of attribute v0.0.14 Thu 28 Dec 14:52:18 GMT 2017 * Fix for marshalling of class and aggregate type objects v0.0.13 Sat Jul 22 09:26:54 2017 +0100 * Fix version matching from @ugexe in #6 v0.0.12 Sat Apr 22 15:32:30 2017 +0100 * Allow skipping of empty aggregate attributes v0.0.11 Wed Dec 21 15:20:45 2016 +0000 * Fix custom marshaller on local over-ride of the attribute in child v0.0.10 Fri May 27 10:41:11 2016 +0100 * Make JSON::Fast an explicit test dependency as the builtin parser is deprecated v0.0.9 Sun May 1 17:41:14 2016 +0100 * Remove possible warning from custom marshaller v0.0.8 Tue Apr 19 14:49:05 2016 +0100 * Switch to using JSON::Fast v0.0.7 Fri Apr 1 20:59:12 2016 +0100 * Allow marshalling of positional, associative objects. v0.0.6 Thu Jan 28 10:35:09 2016 +0000 * Add skipping the serialisation of undefined attributes raku-json-marshal-0.0.25/LICENCE000066400000000000000000000213271446641176400161430ustar00rootroot00000000000000 The Artistic License 2.0 Copyright (c) 2015 Jonathan Stowe Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble This license establishes the terms under which a given free software Package may be copied, modified, distributed, and/or redistributed. The intent is that the Copyright Holder maintains some artistic control over the development of that Package while still keeping the Package available as open source and free software. You are always permitted to make arrangements wholly outside of this license directly with the Copyright Holder of a given Package. If the terms of this license do not permit the full use that you propose to make of the Package, you should contact the Copyright Holder and seek a different licensing arrangement. Definitions "Copyright Holder" means the individual(s) or organization(s) named in the copyright notice for the entire Package. "Contributor" means any party that has contributed code or other material to the Package, in accordance with the Copyright Holder's procedures. "You" and "your" means any person who would like to copy, distribute, or modify the Package. "Package" means the collection of files distributed by the Copyright Holder, and derivatives of that collection and/or of those files. A given Package may consist of either the Standard Version, or a Modified Version. "Distribute" means providing a copy of the Package or making it accessible to anyone else, or in the case of a company or organization, to others outside of your company or organization. "Distributor Fee" means any fee that you charge for Distributing this Package or providing support for this Package to another party. It does not mean licensing fees. "Standard Version" refers to the Package if it has not been modified, or has been modified only in ways explicitly requested by the Copyright Holder. "Modified Version" means the Package, if it has been changed, and such changes were not explicitly requested by the Copyright Holder. "Original License" means this Artistic License as Distributed with the Standard Version of the Package, in its current version or as it may be modified by The Perl Foundation in the future. "Source" form means the source code, documentation source, and configuration files for the Package. "Compiled" form means the compiled bytecode, object code, binary, or any other form resulting from mechanical transformation or translation of the Source form. Permission for Use and Modification Without Distribution (1) You are permitted to use the Standard Version and create and use Modified Versions for any purpose without restriction, provided that you do not Distribute the Modified Version. Permissions for Redistribution of the Standard Version (2) You may Distribute verbatim copies of the Source form of the Standard Version of this Package in any medium without restriction, either gratis or for a Distributor Fee, provided that you duplicate all of the original copyright notices and associated disclaimers. At your discretion, such verbatim copies may or may not include a Compiled form of the Package. (3) You may apply any bug fixes, portability changes, and other modifications made available from the Copyright Holder. The resulting Package will still be considered the Standard Version, and as such will be subject to the Original License. Distribution of Modified Versions of the Package as Source (4) You may Distribute your Modified Version as Source (either gratis or for a Distributor Fee, and with or without a Compiled form of the Modified Version) provided that you clearly document how it differs from the Standard Version, including, but not limited to, documenting any non-standard features, executables, or modules, and provided that you do at least ONE of the following: (a) make the Modified Version available to the Copyright Holder of the Standard Version, under the Original License, so that the Copyright Holder may include your modifications in the Standard Version. (b) ensure that installation of your Modified Version does not prevent the user installing or running the Standard Version. In addition, the Modified Version must bear a name that is different from the name of the Standard Version. (c) allow anyone who receives a copy of the Modified Version to make the Source form of the Modified Version available to others under (i) the Original License or (ii) a license that permits the licensee to freely copy, modify and redistribute the Modified Version using the same licensing terms that apply to the copy that the licensee received, and requires that the Source form of the Modified Version, and of any works derived from it, be made freely available in that license fees are prohibited but Distributor Fees are allowed. Distribution of Compiled Forms of the Standard Version or Modified Versions without the Source (5) You may Distribute Compiled forms of the Standard Version without the Source, provided that you include complete instructions on how to get the Source of the Standard Version. Such instructions must be valid at the time of your distribution. If these instructions, at any time while you are carrying out such distribution, become invalid, you must provide new instructions on demand or cease further distribution. If you provide valid instructions or cease distribution within thirty days after you become aware that the instructions are invalid, then you do not forfeit any of your rights under this license. (6) You may Distribute a Modified Version in Compiled form without the Source, provided that you comply with Section 4 with respect to the Source of the Modified Version. Aggregating or Linking the Package (7) You may aggregate the Package (either the Standard Version or Modified Version) with other packages and Distribute the resulting aggregation provided that you do not charge a licensing fee for the Package. Distributor Fees are permitted, and licensing fees for other components in the aggregation are permitted. The terms of this license apply to the use and Distribution of the Standard or Modified Versions as included in the aggregation. (8) You are permitted to link Modified and Standard Versions with other works, to embed the Package in a larger work of your own, or to build stand-alone binary or bytecode versions of applications that include the Package, and Distribute the result without restriction, provided the result does not expose a direct interface to the Package. Items That are Not Considered Part of a Modified Version (9) Works (including, but not limited to, modules and scripts) that merely extend or make use of the Package, do not, by themselves, cause the Package to be a Modified Version. In addition, such works are not considered parts of the Package itself, and are not subject to the terms of this license. General Provisions (10) Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License. By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license. (11) If your Modified Version has been derived from a Modified Version made by someone other than you, you are nevertheless required to ensure that your Modified Version complies with the requirements of this license. (12) This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder. (13) This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement, then this Artistic License to you shall terminate on the date that such litigation is filed. (14) Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. raku-json-marshal-0.0.25/META6.json000066400000000000000000000017031446641176400166610ustar00rootroot00000000000000{ "license": "Artistic-2.0", "description": "Simple serialisation of objects to JSON", "version": "0.0.25", "tags": [ "object", "serialisation", "JSON" ], "authors": [ "Jonathan Stowe " ], "provides": { "JSON::Marshal": "lib/JSON/Marshal.rakumod" }, "support": { "email": "jns+git@gellyfish.co.uk", "source": "https://github.com/jonathanstowe/JSON-Marshal.git", "bugtracker": "https://github.com/jonathanstowe/JSON-Marshal/issues" }, "source-url": "https://github.com/jonathanstowe/JSON-Marshal.git", "raku": "6.*", "resources": [ ], "author": "Jonathan Stowe ", "auth": "zef:jonathanstowe", "meta-version": "1", "build-depends": [ ], "api": "1.0", "depends": [ "JSON::Fast:ver<0.16+>", "JSON::Name:ver<0.0.6+>", "JSON::OptIn" ], "name": "JSON::Marshal", "test-depends": [ "Test", "JSON::Fast:ver<0.16+>" ] } raku-json-marshal-0.0.25/README.md000066400000000000000000000074031446641176400164340ustar00rootroot00000000000000# JSON::Marshal Make JSON from an Object (the opposite of JSON::Unmarshal) ![Build Status](https://github.com/jonathanstowe/JSON-Marshal/workflows/CI/badge.svg) ## Synopsis ```raku use JSON::Marshal; class SomeClass { has Str $.string; has Int $.int; has Version $.version is marshalled-by('Str'); } my $object = SomeClass.new(string => "string", int => 42, version => Version.new("0.0.1")); my Str $json = marshal($object); # -> "{ "string" : "string", "int" : 42, "version" : "0.0.1" }' ``` Or with _opt-in_ marshalling: ```raku use JSON::Marshal; use JSON::OptIn; class SomeClass { has Str $.string is json; has Int $.int is json; has Str $.secret; has Version $.version is marshalled-by('Str'); } my $object = SomeClass.new(secret => "secret", string => "string", int => 42, version => Version.new("0.0.1")); my Str $json = marshal($object, :opt-in); # -> "{ "string" : "string", "int" : 42, "version" : "0.0.1" }' ``` ## Description This provides a single exported subroutine to create a JSON representation of an object. It should round trip back into an object of the same class using [JSON::Unmarshal](https://github.com/tadzik/JSON-Unmarshal). It only outputs the "public" attributes (that is those with accessors created by declaring them with the '.' twigil. Attributes without acccessors are ignored. If you want to ignore any attributes without a value you can use the ```:skip-null``` adverb to ```marshal```, which will supress the marshalling of any undefined attributes. Additionally if you want a finer-grained control over this behaviour there is a 'json-skip-null' attribute trait which will cause the specific attribute to be skipped if it isn't defined irrespective of the ```skip-null```. If you want to always explicitly suppress the marshalling of an attribute then the the trait `json-skip` on an attribute will prevent it being output in the JSON. By default *all* public attributes will be candidates to be marshalled to JSON, which may not be convenient for all applications (for example only a small number of attributes should be marshalled in a large class,) so the `marshal` provides an `:opt-in` adverb that inverts the behaviour so that only those attributes which have one of the traits that control marshalling (with the exception of `json-skip`,) will be candidates. The `is json` trait from [JSON::OptIn](https://github.com/jonathanstowe/JSON-OptIn) can be supplied to an attribute to mark it for marshalling explicitly, (it is implicit in all the other traits bar `json-skip`.) To allow a finer degree of control of how an attribute is marshalled an attribute trait ```is marshalled-by``` is provided, this can take either a Code object (an anonymous subroutine,) which should take as an argument the value to be marshalled and should return a value that can be completely represented as JSON, that is to say a string, number or boolean or a Hash or Array who's values are those things. Alternatively the name of a method that will be called on the value, the return value being constrained as above. By default the JSON produced is _pretty_ (that is newlines and indentation,) which is nice for humans to read but has a lot of superfluous characters in it, this can be controlled by passing `:!pretty` to `marshal`. ## Installation Assuming you have a working Rakudo installation, you can install this with ```zef``` : # From the source directory zef install . # Remote installation zef install JSON::Marshal ## Support Suggestions/patches are welcomed via github at https://github.com/jonathanstowe/JSON-Marshal/issues ## Licence Please see the [LICENCE](LICENCE) file in the distribution © Jonathan Stowe 2015, 2016, 2017, 2018, 2019, 2020, 2021 raku-json-marshal-0.0.25/lib/000077500000000000000000000000001446641176400157175ustar00rootroot00000000000000raku-json-marshal-0.0.25/lib/JSON/000077500000000000000000000000001446641176400164705ustar00rootroot00000000000000raku-json-marshal-0.0.25/lib/JSON/Marshal.rakumod000066400000000000000000000177351446641176400214600ustar00rootroot00000000000000use v6; =begin pod =head1 NAME JSON::Marshal - Make JSON from an Object. =head1 SYNOPSIS =begin code use JSON::Marshal; class SomeClass { has Str $.string; has Int $.int; has Version $.version is marshalled-by('Str'); } my $object = SomeClass.new(string => "string", int => 42, version => Version.new("0.0.1")); my Str $json = marshal($object); # -> "{ "string" : "string", "int" : 42, "version" : "0.0.1" }' =end code Or with 'opt-in' marshalling: =begin code use JSON::Marshal; use JSON::OptIn; class SomeClass { has Str $.string is json; has Int $.int is json; has Str $.secret; has Version $.version is marshalled-by('Str'); } my $object = SomeClass.new(secret => "secret", string => "string", int => 42, version => Version.new("0.0.1")); my Str $json = marshal($object, :opt-in); # -> "{ "string" : "string", "int" : 42, "version" : "0.0.1" }' =end code =head1 DESCRIPTION This provides a single exported subroutine to create a JSON representation of an object. It should round trip back into an object of the same class using L. It only outputs the "public" attributes (that is those with accessors created by declaring them with the '.' twigil. Attributes without acccessors are ignored. If you want to ignore any attributes without a value you can use the :skip-null adverb to C, which will supress the marshalling of any undefined attributes. Additionally if you want a finer-grained control over this behaviour there is a 'json-skip-null' attribute trait which will cause the specific attribute to be skipped if it isn't defined irrespective of the C. C or the C trait is applied to a C or C attribute this will suppress the marshalling of an empty list or object attribute. If you want to always explicitly suppress the marshalling of an attribute then the the trait C on an attribute will prevent it being output in the JSON. By default B public attributes will be candidates to be marshalled to JSON, which may not be convenient for all applications (for example only a small number of attributes should be marshalled in a large class,) so the C provides an C<:opt-in> adverb that inverts the behaviour so that only those attributes which have one of the traits that control marshalling (with the exception of C,) will be candidates. The C trait from L can be supplied to an attribute to mark it for marshalling explicitly, (it is implicit in all the other traits bar C.) To allow a finer degree of control of how an attribute is marshalled an attribute trait C is provided, this can take either a Code object (an anonymous subroutine,) which should take as an argument the value to be marshalled and should return a value that can be completely represented as JSON, that is to say a string, number or boolean or Nil or a Hash or Array who's values are those things. Alternatively the name of a method that will be called on the value, the return value being constrained as above. In the case of the Code argument to the trait, the supplied subroutine should handle the case of an undefined value for itself as appropriate, in the case of a method name it will not be called at all and the attribute will be marshalled as Nil. You can pass the adverb C<:sorted-keys> to C which is in turn passed on to C to cause the keys to be sorted before the JSON is created. By default the JSON produced is I (that is newlines and indentation,) which is nice for humans to read but has a lot of superfluous characters in it, this can be controlled by passing C<:!pretty> to C which is passed to C =end pod use JSON::OptIn; use JSON::Name:ver<0.0.6+>; module JSON::Marshal:ver<0.0.25>:auth { use JSON::Fast:ver(v0.16+); my class MarshalParams { has Bool $.skip-null; has Bool $.opt-in; } role CustomMarshaller does JSON::OptIn::OptedInAttribute { method marshal($value, Mu:D $object) { ... } } role CustomMarshallerCode does CustomMarshaller { has &.marshaller is rw; method marshal($value, Mu:D $object) { # the dot below is important otherwise it refers # to the accessor method self.marshaller.($value); } } role CustomMarshallerMethod does CustomMarshaller { has Str $.marshaller is rw; method marshal($value, Mu:D $type) { my $meth = self.marshaller; $value.defined ?? $value."$meth"() !! Nil; } } multi sub trait_mod: (Attribute $attr, :&marshalled-by!) is export { $attr does CustomMarshallerCode; $attr.marshaller = &marshalled-by; } multi sub trait_mod: (Attribute $attr, Str:D :$marshalled-by!) is export { $attr does CustomMarshallerMethod; $attr.marshaller = $marshalled-by; } role SkipNull does JSON::OptIn::OptedInAttribute { } multi sub trait_mod: (Attribute $attr, :$json-skip-null!) is export { $attr does SkipNull; } # This is a hard skip the attribute will never be emitted. role JsonSkip { } multi sub trait_mod: (Attribute $attr, :$json-skip!) is export { $attr does JsonSkip; } multi sub _marshal(Cool $value) { $value; } multi sub _marshal(Associative:D \obj --> Hash:D) { obj.kv.map(-> $key, $value { $key => _marshal($value) }).Hash } multi sub _marshal(Positional:D \obj --> Positional:D) { obj.map({ _marshal($_) }).eager } multi sub _marshal(Mu:U --> Nil) { Nil; } multi sub _marshal(Mu:D $obj --> Hash:D) { # Though the params are required by serialize-ok, it is better to pull in the dynamic here because it reduces # the number of slow dynamic lookups. my $params = $*JSON-MARSHAL-PARAMS; my %ret; my %local-attrs = $obj.^attributes(:local).map({ $_.name => $_.package }); for $obj.^attributes -> $attr { if %local-attrs{$attr.name}:exists && !(%local-attrs{$attr.name} === $attr.package ) { next; } if $attr.has_accessor || $attr.is_built || $attr ~~ JSON::OptIn::OptedInAttribute { my $accessor-name = $attr.name.substr(2); # lose the sigil my $name = do if $attr ~~ JSON::Name::NamedAttribute { $attr.json-name; } else { $accessor-name; } my $value = $obj.^can($accessor-name) ?? $obj."$accessor-name"() !! $attr.get_value($obj); if serialise-ok($attr, $value, $params) { %ret{$name} = do if $attr ~~ CustomMarshaller { $attr.marshal($value, $obj); } else { _marshal($value); } } } } %ret; } sub serialise-ok(Attribute $attr, Mu $value, MarshalParams:D $params --> Bool ) { my $rc = True; if $attr ~~ JsonSkip || ( $params.opt-in && ( $attr !~~ JSON::OptIn::OptedInAttribute ) ) { $rc = False; } elsif $params.skip-null || ( $attr ~~ SkipNull ) { if $attr.type ~~ Associative|Positional { $rc = ?$value.elems; } else { $rc = $value.defined; } } $rc; } sub marshal(Any $obj, Bool :$skip-null, Bool :$sorted-keys = False, Bool :$pretty = True, Bool :$opt-in = False --> Str ) is export { my $*JSON-MARSHAL-PARAMS = MarshalParams.new(:$skip-null, :$opt-in); my $ret = _marshal($obj); to-json($ret, :$sorted-keys, :$pretty); } } # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/000077500000000000000000000000001446641176400154145ustar00rootroot00000000000000raku-json-marshal-0.0.25/t/010-use.t000066400000000000000000000002111446641176400166650ustar00rootroot00000000000000#!raku use v6; use Test; use-ok('JSON::Marshal', 'Can load "JSON::Marshal" ok'); done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/020-marshal.t000066400000000000000000000023721446641176400175330ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Marshal; use JSON::Fast; plan 12; class Inner { has %.hash = A => 1, B => 2; has Rat $.rat = 4.2; } class Outer { has Bool $.bool = True; has Str $.string = "string"; has Str @.str-array = ; has Int $.int = 42; has Inner $.inner = Inner.new; has $!private = 'private'; has Real $!half-priv is built = 42; } my $outer = Outer.new; my $ret; lives-ok { $ret = marshal($outer) }, "marshal object"; my %json = from-json($ret); ok not %json:exists, "didn't get the private attribute"; is %json, $outer.bool, "bool right"; is %json, $outer.string, "string right"; is %json, $outer.int, "int right"; is %json, $outer.str-array, "arrays are the same"; is %json, 42, "is built attribute is included"; is %json, $outer.inner.rat, "inner class rat the same"; is %json, $outer.inner.hash, "inner hash 1"; is %json, $outer.inner.hash, "inner hash 2"; lives-ok { $ret = marshal($outer, :sorted-keys) }, "marshal object with sorted keys"; ok $ret ~~ /.*"bool".*"inner".*"int".*"str-array".*"string"/, "keys are in the order expected"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/030-trait.t000066400000000000000000000036521446641176400172320ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Marshal; use JSON::Fast; subtest { class VersionClassCode { has Version $.version is marshalled-by(-> Version $v { $v.defined ?? $v.Str !! Nil }); } my VersionClassCode $obj = VersionClassCode.new(version => Version.new("0.0.1")); my $json; lives-ok { $json = marshal($obj) }, "marshall with attribute trait (code)"; my $parsed; lives-ok { $parsed = from-json($json) }, "parse the resulting JSON"; ok $parsed.defined, "got something back"; is $parsed, "0.0.1", "and has the right value"; $obj = VersionClassCode.new; lives-ok { $json = marshal($obj) }, "marshall with attrbute trait (code) but attribute not defined"; lives-ok { $parsed = from-json($json) }, "got sensible JSON back"; ok $parsed.defined, "got something back"; ok $parsed:exists, "got the key"; ok !$parsed.defined, "and has the right value (Nil)"; }, "marshalled-by trait with Code"; subtest { class VersionClassMethod { has Version $.version is marshalled-by('Str'); } my VersionClassMethod $obj = VersionClassMethod.new(version => Version.new("0.0.1")); my $json; lives-ok { $json = marshal($obj) }, "marshall with attrbute trait (method name)"; my $parsed; lives-ok { $parsed = from-json($json) }, "got sensible JSON back"; ok $parsed.defined, "got something back"; is $parsed, "0.0.1", "and has the right value"; $obj = VersionClassMethod.new; lives-ok { $json = marshal($obj) }, "marshall with attrbute trait (method name) but attribute not defined"; lives-ok { $parsed = from-json($json) }, "got sensible JSON back"; ok $parsed.defined, "got something back"; ok $parsed:exists, "got the key"; ok !$parsed.defined, "and has the right value (Nil)"; }, "marshalled-by trait with Method name"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/040-json-name.t000066400000000000000000000013211446641176400177660ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Marshal; use JSON::Fast; use JSON::Name; class TestClass { has $.nice-name is rw is json-name('666.evil.name'); } my $obj; lives-ok { $obj = TestClass.new(nice-name => "value for money") }, "create on object with a json-name attribute"; my $json; lives-ok { $json = marshal($obj) }, "marshal that object"; my $back; lives-ok { $back = from-json($json) }, "parse the JSON"; is $back<666.evil.name>, $obj.nice-name, "and we got the key back with the json name"; lives-ok { $obj = TestClass.new }, "create on object with a json-name attribute but not defined"; lives-ok { $json = marshal($obj) }, "marshal that object"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/050-skip-null.t000066400000000000000000000034031446641176400200210ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Marshal; use JSON::Fast; plan 15; # test default global behaviour class SkipTestClassOne { has Str $.id; has Str $.name; has Str %.stuff; has Str @.things; } my $res-default; lives-ok { $res-default = marshal(SkipTestClassOne.new(name => "foo"), :skip-null) }, "apply skip-null to marshal"; my $out = from-json($res-default); nok $out:exists, "and the (null) id was skipped"; nok $out:exists, "and the empty stuff was skipped"; nok $out:exists, "and the empty things was skipped"; is $out, "foo", "but we still got the defined one"; class SkipTestClassTwo { has Str $.id is json-skip-null; has Str $.rev is json-skip-null; has Str $.name; has Str $.leave-blank; has Str %.empty-hash; has Str %.skip-hash is json-skip-null; } lives-ok { $res-default = marshal(SkipTestClassTwo.new(name => "foo", rev => "bar")) }, "apply skip-null trait to single attribute"; $out = from-json($res-default); nok $out:exists, "and the (null) id was skipped"; is $out, "foo", "but we still got the defined one"; ok $out:exists, "one not defined but without trait still there"; nok $out.defined, "and it isn't defined"; is $out, "bar", "one with the trait but with value is there"; ok $out:exists, "the empty hash is there"; nok $out:exists, "the skipped one isn't there"; class NestedStruct { has SkipTestClassOne:D $.stc1 .= new; has SkipTestClassTwo:D $.stc2 .= new; } lives-ok { $res-default = marshal(NestedStruct.new, :skip-null, :sorted-keys, :!pretty) }, "skip-null with a deep struct"; is $res-default, '{"stc1":{},"stc2":{}}', "no empty keys are included"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/060-array.t000066400000000000000000000007211446641176400172220ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Marshal; use JSON::Fast; class TestObject { has Str $.string; } my @arr = (TestObject.new(string => "one"), TestObject.new(string => "two")); my $out; lives-ok { $out = marshal(@arr) }, "marshal an array"; my $test = from-json($out); is $test[0], 'one', "got right object in first element"; is $test[1], 'two', "got right object in second element"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/070-inheritance.t000066400000000000000000000015651446641176400204050ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Marshal; use JSON::Fast; class ParentOne { has Str $.name = "default"; has Int $.number; } class ChildOne is ParentOne { has Str $.name; } my $outer = ChildOne.new(name => 'foo', number => 42); my $ret; lives-ok { $ret = marshal($outer) }, "marshal object"; my %json = from-json($ret); is %json, "foo", "got string attribute from child class"; is %json, 42, "got number attribute from child class"; class ParentTwo { has Version $.version; } class ChildTwo is ParentTwo { has Version $.version is marshalled-by(-> Version $v { $v.Str }); } $outer = ChildTwo.new(version => Version.new("0.0.1")); lives-ok { $ret = marshal($outer) }, "marshal object"; %json = from-json($ret); is %json, '0.0.1', "and it got the right custom marshaller"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/080-type-constraints.t000066400000000000000000000026011446641176400214330ustar00rootroot00000000000000#!/usr/bin/env raku use Test; use JSON::Marshal; use JSON::Fast; my class TestClassConstraint { has $.depends is rw where Positional|Associative; } my $obj; lives-ok { $obj = TestClassConstraint.new }, "create object with anonymous constraint (no arguments)"; my $json; lives-ok { $json = marshal($obj) ; }, "marshal that"; my $parsed; lives-ok { $parsed = from-json($json) }, "got some sane JSON"; ok $parsed:exists, "and we got the expected key"; ok !$parsed.defined, "and is 'null' as expected"; lives-ok { $obj = TestClassConstraint.new(depends => ['foo'] ) }, "create object with anonymous constraint positional argument"; lives-ok { $json = marshal($obj) ; }, "marshal that"; lives-ok { $parsed = from-json($json) }, "got some sane JSON"; ok $parsed:exists, "and we got the expected key"; ok $parsed ~~ Positional, "and is a Positional as expected"; lives-ok { $obj = TestClassConstraint.new(depends => { foo => 'bar' } ) }, "create object with anonymous constraint associative argument"; lives-ok { $json = marshal($obj) ; }, "marshal that"; lives-ok { $parsed = from-json($json) }, "got some sane JSON"; ok $parsed:exists, "and we got the expected key"; ok $parsed ~~ Associative, "and is a Associative as expected"; is $parsed, 'bar' , "with the expected value"; done-testing; # vim: ft=raku raku-json-marshal-0.0.25/t/090-issue-8.t000066400000000000000000000015661446641176400174140ustar00rootroot00000000000000#!/usr/bin/env raku use v6; use Test; use JSON::Marshal; use JSON::Fast; class C { } class D { has Str $.gh; } my @tests = ( { description => "class with no attributes", type_object => C, }, { description => "class with attributes", type_object => D, }, { description => "Hash type object", type_object => Hash, }, { description => "Array type object", type_object => Array, }, ); for @tests -> $test { subtest { my $out; lives-ok { $out = marshal($test) }, "marshal type-object"; my $in = from-json($out); nok $in.defined, "roundtripped value not defined"; ok $in ~~ Any, "it's an Any"; ok $in !~~ Hash, "and it's not a hash"; }, $test; } done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/100-meta6-issue-22.t000066400000000000000000000006031446641176400204610ustar00rootroot00000000000000#!/usr/bin/env raku use Test; use JSON::Marshal; my class TestClass22 { has Version $.perl-version is rw is marshalled-by('Str') is json-name('perl'); } my $obj; lives-ok { $obj = TestClass22.new }, "create object with json-name and marshalled-by attribute not provided"; my $json; lives-ok { $json = marshal($obj) }, "and try to marshal it"; done-testing; # vim: ft=raku raku-json-marshal-0.0.25/t/110-skip.t000066400000000000000000000006761446641176400170570ustar00rootroot00000000000000#!/usr/bin/env raku use v6; use Test; use JSON::Marshal; use JSON::Fast; class WithSkip { has Str $.skipped is json-skip = "skipped"; has Str $.not-skipped = "not skipped"; } my $c = WithSkip.new; my $json = marshal($c); my %data = from-json($json); ok !(%data:exists), "the skipped attribute isn't in the JSON"; ok %data:exists, "the not skipped attribute is in the JSON"; done-testing; # vim: ft=raku raku-json-marshal-0.0.25/t/120-pretty.t000066400000000000000000000020051446641176400174250ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Marshal; use JSON::Fast; plan 4; class Outer { has Bool $.bool = True; has Str $.string = "string"; has Str @.str-array = ; has Int $.int = 42; has $!private = 'private'; } my $outer = Outer.new; # Structure that we expect to be serialised my %exp = ( bool => True, string => 'string', str-array => , int => 42, ); my $ret; # This isn't for testing the output of the JSON::Fast to-json but rather that the switches are # passed on correctly. Don't want to be a canary for a regression in JSON::Fast lives-ok { $ret = marshal($outer, :sorted-keys, :pretty) }, "pretty-marshal object doesn't fail"; is $ret, to-json(%exp, :sorted-keys, :pretty), "marshalled JSON is pretty"; lives-ok { $ret = marshal($outer, :sorted-keys, :!pretty) }, "compact-marshal object"; is $ret, to-json(%exp, :sorted-keys, :!pretty ), "marshalled JSON is compact"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-marshal-0.0.25/t/130-custom-accessor.t000066400000000000000000000007061446641176400212170ustar00rootroot00000000000000#!/usr/bin/env raku use Test; use JSON::Marshal; use JSON::Fast; class CustomAttr { has Int $.int = 10; has Str $.string; method string( --> Str ) { "TESTSTRING"; } } my $obj = CustomAttr.new; my $json = marshal($obj); my $data = from-json($json); is $data, $obj.int, "got the right value for straight accessor"; is $data, $obj.string, "got the right value for custom accessor"; done-testing; # vim: ft=raku raku-json-marshal-0.0.25/t/140-opt-in.t000066400000000000000000000026261446641176400173170ustar00rootroot00000000000000#!/usr/bin/env raku use Test; use JSON::Marshal; use JSON::Name; use JSON::OptIn; use JSON::Fast; plan 11; class TestOptin { has Str $.secret = 'secret'; has Str $.public is json = 'public'; has Str $.skipped is json-skip = 'skipped'; has Str $.null is json-skip-null; has Str $.nullable is json-skip-null = 'nullable'; has DateTime $.marshalled is marshalled-by('Str') = DateTime.now; has DateTime $.marshalled-sub is marshalled-by(-> $v { $v.Str }) = DateTime.now; has Str $.named is json-name('zubzub') = 'named'; has Real $!half-priv is json = 42; has Real $!priv-built is built = 13; } my Str $json; lives-ok { $json = marshal(TestOptin.new, :opt-in) }, "marshal() with opt-in"; my %data = from-json($json); is %data, 'public', "explicitly opted in"; ok %data:!exists, "not opted-in at all"; ok %data:!exists, "skipped"; ok %data:!exists, "skip-null"; ok %data:!exists, "is built but not opted-in"; is %data, 'nullable', 'skip-null with value'; ok %data:exists, "marshalled-by implicit opt-in (method)"; ok %data:exists, "marshalled-by implicit opt-in (sub)"; is %data, 'named', 'json-name implicit opt-in'; is %data, 42, 'opted-in private'; done-testing; # vim: ft=raku