pax_global_header00006660000000000000000000000064144664130220014514gustar00rootroot0000000000000052 comment=bab02095a5b736dc75697c75de044bc7d3b0e6d3 raku-json-class-0.0.21/000077500000000000000000000000001446641302200146105ustar00rootroot00000000000000raku-json-class-0.0.21/.github/000077500000000000000000000000001446641302200161505ustar00rootroot00000000000000raku-json-class-0.0.21/.github/workflows/000077500000000000000000000000001446641302200202055ustar00rootroot00000000000000raku-json-class-0.0.21/.github/workflows/main.yml000066400000000000000000000006411446641302200216550ustar00rootroot00000000000000name: 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-class-0.0.21/.gitignore000066400000000000000000000003101446641302200165720ustar00rootroot00000000000000/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-class-0.0.21/.layout000066400000000000000000000023111446641302200161230ustar00rootroot00000000000000{ "type": "layout", "children": [ { "type": "directory", "purpose": null, "children": [ { "name": "060-associative.t", "type": "file" }, { "name": "070-opt-in.t", "type": "file" }, { "name": "030-roundtrip.t", "type": "file" }, { "name": "050-array.t", "type": "file" }, { "name": "010-use.t", "type": "file" }, { "type": "file", "name": "040-traits.t" } ], "name": "t" }, { "name": "README.md", "type": "file" }, { "type": "directory", "purpose": null, "name": "lib", "children": [ { "purpose": null, "children": [ { "name": "Class.rakumod", "type": "file" } ], "name": "JSON", "type": "directory" } ] }, { "type": "file", "name": "META6.json" }, { "name": "Changes", "type": "file" }, { "type": "file", "name": "LICENCE" } ] } raku-json-class-0.0.21/Changes000066400000000000000000000031051446641302200161020ustar00rootroot00000000000000v0.0.21 Mon 14 Aug 12:54:26 BST 2023 * Up dependency version of JSON::Marshal v0.0.20 Tue 25 Jul 18:12:20 BST 2023 * Up dependency version of JSON::Unmarshal v0.0.19 Sun 7 Aug 09:45:06 BST 2022 * Upload to zef repository v0.0.18 Thu 21 Oct 15:05:48 BST 2021 * Add ':opt-in' marshalling v0.0.17 Wed 28 Jul 14:13:48 BST 2021 * Up the version of JSON::Marshall to get the accessor behaviour v0.0.16 Tue 2 Feb 09:32:49 GMT 2021 * Accept :pretty adverb for JSON::Marshal -> JSON::Fast v0.0.15 Sun 27 Sep 18:45:43 BST 2020 * Up minimum version JSON::Marshal * Remove ancient todo test v0.0.14 Tue 17 Dec 19:51:20 GMT 2019 * Specify minimum versions of dependencies * Raku it up v0.0.13 Thu 18 Apr 14:33:10 BST 2019 * Pass through the :sorted-keys from to-json to JSON::Marshal v0.0.12 Mon 4 Mar 19:52:49 GMT 2019 * Remove :api adverb to apease the old rakudo v0.0.11 Thu 17 Jan 21:44:46 GMT 2019 * Spting clean and upload to CPAN v0.0.10 Tue 21 Nov 15:47:45 GMT 2017 * Change the way the re-export of traits is performed v0.0.9 Sat Jul 22 10:03:29 2017 +0100 * Fix for version matching from @ugexe from #5 v0.0.8 Mon May 30 10:44:08 2016 +0100 * Was using JSON::Tiny instead of JSON::Fast v0.0.7 Sat May 28 23:52:38 2016 +0100 * Use JSON::Fast explicitly v0.0.6 Fri Apr 1 21:25:30 2016 +0100 * Support for positional/associative types v0.0.5 Thu Jan 28 11:40:10 2016 +0000 * Supply version of JSON::Marshal v0.0.4 Thu Jan 28 10:59:29 2016 +0000 * Add support for :skip-null v0.0.3 Wed Jan 27 14:23:43 2016 +0000 * Re-export the attribute traits raku-json-class-0.0.21/LICENCE000066400000000000000000000213271446641302200156020ustar00rootroot00000000000000 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-class-0.0.21/META6.json000066400000000000000000000017201446641302200163170ustar00rootroot00000000000000{ "provides": { "JSON::Class": "lib/JSON/Class.rakumod" }, "authors": [ "Jonathan Stowe " ], "resources": [ ], "depends": [ "JSON::Marshal:ver<0.0.25+>", "JSON::Unmarshal:ver<0.14+>", "JSON::OptIn", "JSON::Name:ver<0.0.6+>" ], "description": "role to provide simple serialisation/deserialisation of objects to/from JSON", "source-url": "https://github.com/jonathanstowe/JSON-Class.git", "author": "Jonathan Stowe ", "raku": "6.*", "version": "0.0.21", "auth": "zef:jonathanstowe", "name": "JSON::Class", "license": "Artistic-2.0", "build-depends": [ ], "test-depends": [ "JSON::Fast", "Test" ], "api": "1.0", "tags": [ "object", "serialisation", "JSON" ], "support": { "source": "https://github.com/jonathanstowe/JSON-Class.git", "bugtracker": "https://github.com/jonathanstowe/JSON-Class/issues" }, "meta-version": "1" } raku-json-class-0.0.21/README.md000066400000000000000000000057471446641302200161040ustar00rootroot00000000000000# JSON::Class A Role to allow Raku objects to be constructed and serialised from/to JSON. ![Build Status](https://github.com/jonathanstowe/JSON-Class/workflows/CI/badge.svg) ## Synopsis ```raku use JSON::Class; class Something does JSON::Class { has Str $.foo; } my Something $something = Something.from-json('{ "foo" : "stuff" }'); ... my Str $json = $something.to-json(); # -> '{ "foo" : "stuff" }' ``` or with 'opt-in' serialization: ```raku use JSON::Class; use JSON::OptIn; class Something does JSON::Class[:opt-in] { has Str $.foo is json; has Str $.secret = 'secret'; } my Something $something = Something.from-json('{ "foo" : "stuff" }'); ... my Str $json = $something.to-json(); # -> '{ "foo" : "stuff" }' ``` ## Description This is a simple role that provides methods to instantiate a class from a JSON string that (hopefully,) represents it, and to serialise an object of the class to a JSON string. The JSON created from an instance should round trip to a new instance with the same values for the "public attributes". "Private" attributes (that is ones without accessors,) will be ignored for 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 attributes you want to skip if they aren't defined (`:skip-null` will still have the same effect though.) If you don't need prettified, human readable JSON output then you can supply the `:!pretty` adverb to `to-json`. 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 re-exported by the module. If your application exposes the marshalled data via, for example, an API, then you may choose to use the `:opt-in` parameter to the role, which will cause only those attributes that are explicitly marked to be marshalled, avoiding the risk of inadvertently exposing sensitive data. This is described in more detail in [JSON::Marshal](https://github.com/jonathanstowe/JSON-Marshal). ## Installation Assuming you have a working Rakudo installation you should be able to install this with *zef* : # From the source directory zef install . # Remote installation zef install JSON::Class ## Support Suggestions/patches are welcomed via github at: https://github.com/jonathanstowe/JSON-Class ## Licence This is free software. Please see the (LICENCE)[LICENCE] file in the distribution for the details. © Jonathan Stowe 2015, 2016, 2017, 2019, 2020, 2021 raku-json-class-0.0.21/lib/000077500000000000000000000000001446641302200153565ustar00rootroot00000000000000raku-json-class-0.0.21/lib/JSON/000077500000000000000000000000001446641302200161275ustar00rootroot00000000000000raku-json-class-0.0.21/lib/JSON/Class.rakumod000066400000000000000000000077501446641302200205710ustar00rootroot00000000000000use v6; =begin pod =head1 NAME JSON::Class - Role to allow a class to unmarshall/marshall itself from JSON =head1 SYNOPSIS =begin code use JSON::Class; class Something does JSON::Class { has Str $.foo; } my Something $something = Something.from-json('{ "foo" : "stuff" }'); ... my Str $json = $something.to-json(); # -> '{ "foo" : "stuff" }' =end code Or with C<:opt-in> marshalling: =begin code use JSON::Class; use JSON::OptIn; class Something does JSON::Class[:opt-in] { has Str $.foo is json; has Str $.secret = 'secret'; } my Something $something = Something.from-json('{ "foo" : "stuff" }'); ... my Str $json = $something.to-json(); # -> '{ "foo" : "stuff" }' =end code =head1 DESCRIPTION This is a simple role that provides methods to instantiate a class from a JSON string that (hopefully,) represents it, and to serialise an object of the class to a JSON string. The JSON created from an instance should round trip to a new instance with the same values for the "public attributes". "Private" attributes (that is ones without accessors,) will be ignored for both serialisation and de-serialisation. The exact behaviour depends on that of L and L respectively. The L and L provide traits for controlling the unmarshalling/marshalling of specific attributes which are re-exported by this module. If your application exposes the marshalled data via, for example, an API, then you may choose to use the C<:opt-in> parameter to the role, which will cause only those attributes that are explicitly marked to be marshalled, avoiding the risk of inadvertently exposing sensitive data. This is described in more detail in L. =head1 METHODS =head2 method from-json method from-json(Str $json) returns JSON::Class Deserialises the provided JSON string, returning a new object, with the public attributes initialised with the corresponding values in the JSON structure. If the JSON is not valid or the data cannot be coerced into the correct type for the target class then an exception may be thrown. =head2 method to-json method to-json(Bool :$skip-null, Bool :$sorted-keys, Bool :$pretty = True ) 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.) If the C adverb is provided this will eventually be passed to the JSON generation and will cause the keys in the JSON object to be sorted in the output. The adverb C is true by default, if you want to suppress I formatting of the output (that is no un-necessary white space,) then you can supply C<:!pretty>. =end pod use JSON::Unmarshal:ver<0.14+>; use JSON::Marshal:ver<0.0.25+>; my package EXPORT::DEFAULT { OUR::{'&trait_mod:'} := &trait_mod:; } role JSON::Class:ver<0.0.21>:auth[Bool :$opt-in = False] { method from-json(Str $json --> JSON::Class ) { my $ret = unmarshal($json, self); if $ret !~~ JSON::Class { $ret does JSON::Class; } $ret; } method to-json(Bool :$skip-null, Bool :$sorted-keys = False, Bool :$pretty = True --> Str ) { marshal(self, :$skip-null, :$sorted-keys, :$pretty, :$opt-in); } } # vim: expandtab shiftwidth=4 ft=raku raku-json-class-0.0.21/t/000077500000000000000000000000001446641302200150535ustar00rootroot00000000000000raku-json-class-0.0.21/t/010-use.t000066400000000000000000000002051446641302200163270ustar00rootroot00000000000000#!raku use v6; use Test; use-ok('JSON::Class', 'Can load "JSON::Class" ok'); done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-class-0.0.21/t/030-roundtrip.t000066400000000000000000000021671446641302200175740ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Class; class Inner { has %.hash; has Rat $.rat; } class Outer does JSON::Class { has Bool $.bool-attr; has Str $.string; has Str @.str-array; has Int $.int; has Inner $.inner; has $!private = 'private'; } my $outer = Outer.new(bool-attr => True, string => "string", str-array => , int => 42, inner => Inner.new(rat => 4.2, hash => { A => 1, B => 2 })); my $ret; lives-ok { $ret = $outer.to-json }, "marshal object with to-json"; my Outer $new; lives-ok { $new = Outer.from-json($ret) }, "unmarshall to object with from-json"; ok $new.defined, "it's defined"; isa-ok $new, Outer, "its the right kind of object"; is $new.bool-attr, $outer.bool-attr, "bool right"; is $new.string, $outer.string, "string right"; is $new.int, $outer.int, "int right"; is $new.str-array, $outer.str-array, "arrays are the same"; is $new.inner.rat, $outer.inner.rat, "inner class rat the same"; is $new.inner.hash, $outer.inner.hash, "inner hash 1"; is $new.inner.hash, $outer.inner.hash, "inner hash 2"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-class-0.0.21/t/040-traits.t000066400000000000000000000014131446641302200170460ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Fast; use JSON::Class; class TraitTest does JSON::Class { has Version $.version is marshalled-by('Str') is unmarshalled-by('new'); } my $obj-orig; lives-ok { $obj-orig = TraitTest.new(version => Version.new("0.0.1")) }, "create new object to test"; my $json; lives-ok { $json = $obj-orig.to-json }, "to-json with traits"; is from-json($json), "0.0.1", "serialised JSON about right"; my $obj-new; lives-ok { $obj-new = TraitTest.from-json($json) }, "from-json with traits"; isa-ok $obj-new.version, Version, "version is a Version"; is $obj-new.version.Str, "0.0.1", "and it stringies how we want"; is $obj-new.version, $obj-orig.version, "and the two compare the same"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-class-0.0.21/t/050-array.t000066400000000000000000000014631446641302200166640ustar00rootroot00000000000000#!raku use v6; use Test; use JSON::Class; class TestObject does JSON::Class { has Str $.string; } constant TestObjects = (Array[TestObject] but JSON::Class); my $json = '[{ "string" : "one" }, { "string" : "two" }]'; my $obj; lives-ok { $obj = TestObjects.from-json($json) }, "from-json with array data"; does-ok $obj, JSON::Class, "and the return does the role"; isa-ok $obj[0], TestObject, "and the items are the right type"; my $json2; lives-ok { $json2 = $obj.to-json() }, "to-json on the array typed thing"; my $obj2; lives-ok { $obj2 = TestObjects.from-json($json2) }, "back from-json again"; is $obj2[0].string, $obj[0].string, "got the first item back correctly"; is $obj2[1].string, $obj[1].string, "got the second item back correctly"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-class-0.0.21/t/060-associative.t000066400000000000000000000005651446641302200200630ustar00rootroot00000000000000#!/usr/bin/env raku use v6; use Test; use JSON::Class; class C is JSON::Class { has Str %.bla{subset :: of Str where any("ble", "blob")} }; my $res; lives-ok { $res = C.from-json('{"bla": {"ble": "bli"}}'); is $res.bla, 'bli', "and get the right value"; }, "from-json with shaped associative works"; done-testing; # vim: expandtab shiftwidth=4 ft=raku raku-json-class-0.0.21/t/070-opt-in.t000066400000000000000000000010611446641302200167500ustar00rootroot00000000000000#!/usr/bin/env raku use Test; use JSON::Class; use JSON::OptIn; use JSON::Name; use JSON::Fast; class TestOptIn does JSON::Class[:opt-in] { has Str $.secret = "secret"; has Str $.public is json = "public"; has Str $.named is json-name('Named') = "named"; } my $obj = TestOptIn.new; my $json = $obj.to-json; my %data = from-json($json); is %data, 'public', 'got opted-in attribute'; is %data, 'named', 'got implicit attribute'; ok not %data:exists, "Don't have the not opted in attribute"; done-testing; # vim: ft=raku