{"id":16307568,"url":"https://github.com/kfly8/p5-json-typeencoder","last_synced_at":"2026-05-19T10:05:01.225Z","repository":{"id":141959543,"uuid":"153040512","full_name":"kfly8/p5-JSON-TypeEncoder","owner":"kfly8","description":"serialize JSON using type information","archived":false,"fork":false,"pushed_at":"2018-11-21T22:31:24.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T20:34:16.623Z","etag":null,"topics":["json","perl"],"latest_commit_sha":null,"homepage":"","language":"Perl 6","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kfly8.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-15T02:14:01.000Z","updated_at":"2018-11-21T02:46:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"5b6b6f0e-abbb-488b-81ff-4379b96a7f00","html_url":"https://github.com/kfly8/p5-JSON-TypeEncoder","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/kfly8/p5-JSON-TypeEncoder","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-TypeEncoder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-TypeEncoder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-TypeEncoder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-TypeEncoder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kfly8","download_url":"https://codeload.github.com/kfly8/p5-JSON-TypeEncoder/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-TypeEncoder/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272340970,"owners_count":24917470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-27T02:00:09.397Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","perl"],"created_at":"2024-10-10T21:14:36.418Z","updated_at":"2026-05-19T10:05:01.183Z","avatar_url":"https://github.com/kfly8.png","language":"Perl 6","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/kfly8/p5-JSON-TypeEncoder.svg?branch=master)](https://travis-ci.org/kfly8/p5-JSON-TypeEncoder) [![Coverage Status](https://img.shields.io/coveralls/kfly8/p5-JSON-TypeEncoder/master.svg?style=flat)](https://coveralls.io/r/kfly8/p5-JSON-TypeEncoder?branch=master) [![MetaCPAN Release](https://badge.fury.io/pl/JSON-TypeEncoder.svg)](https://metacpan.org/release/JSON-TypeEncoder)\n# NAME\n\nJSON::TypeEncoder - serialize JSON using type information\n\n# SYNOPSIS\n\n```perl\nuse JSON::TypeEncoder;\nuse Types::Standard -types;\n\nmy $type = Dict[a =\u003e Str, b =\u003e Int, c =\u003e Bool];\n\nmy $jsont = JSON::TypeEncoder-\u003enew;\nmy $encode = $jsont-\u003eencoder($type);\n\n$encode-\u003e({ a =\u003e 'foo', b =\u003e 30, c =\u003e !!1 });\n# =\u003e {\"a\":\"foo\",\"b\":30,\"c\":true}\n```\n\n# DESCRIPTION\n\nJSON::TypeEncoder serialize Perl data structures to JSON using type information.\nThis module goal is to be **correct** and **fast**.\n\n## FEATURES\n\n### Correct\n\nThis module encodes according to the specified type information.\nFor example, it encodes as following:\n\n```perl\nuse JSON::TypeEncoder;\nuse Types::Standard -types;\n\nmy $jsont = JSON::TypeEncoder-\u003enew;\n\nmy $s = $jsont-\u003eencoder(Dict[a =\u003e Str]);\n$s-\u003e({a =\u003e 123}); # =\u003e {\"a\":\"123\"}\n\nmy $i = $jsont-\u003eencoder(Dict[a =\u003e Int]);\n$i-\u003e({a =\u003e 123}); # =\u003e {\"a\":123}\n\nmy $b = $jsont-\u003eencoder(Dict[a =\u003e Bool]);\n$b-\u003e({a =\u003e !!0}); # =\u003e {\"a\":false}\n```\n\nThis will prevent unintended encoding.\n\n### Fast\n\nEncoding performance is improved by string eval using type information.\nYou can get speed comparable to JSON::XS. The results of a simple benchmark is as following.\n\nFirst comes a comparison using a very short single-line JSON string (also available at http://dist.schmorp.de/misc/json/short.json).\n\n```perl\n{\"method\": \"handleMessage\", \"params\": [\"user1\", \"we were just talking\"], \"id\": null, \"array\":[1,11,234,-5,1e5,1e7, 1,  0]}\n```\n\nIt shows the number of encodes per second. Higher is better:\n\n```\n#                       Rate JSON::PP JSON::XS w/ Types JSON::TypeEncoder JSON::XS\n# JSON::PP           39739/s       --              -83%              -90%     -95%\n# JSON::XS w/ Types 234056/s     489%                --              -42%     -72%\n# JSON::TypeEncoder 405736/s     921%               73%                --     -51%\n# JSON::XS          825118/s    1976%              253%              103%       --\n```\n\nUsing a longer test string (roughly 18KB, generated from Yahoo! Locals search API (http://dist.schmorp.de/misc/json/long.json).\n\n```\n#                      Rate          JSON::PP          JSON::XS JSON::TypeEncoder\n# JSON::PP            984/s                --              -96%              -97%\n# JSON::XS          26946/s             2639%                --              -13%\n# JSON::TypeEncoder 30919/s             3043%               15%                --\n```\n\n### Pure Perl\n\nThis module is written by pure Perl. So you can easily install it.\n\n## **NOT** FEATURES\n\nThis module NOT supports decode JSON to Perl data structures.\nYou should use other modules like JSON::XS to decode.\n\n## TYPE SPECIFICATION\n\n[Types::Standard](https://metacpan.org/pod/Types::Standard) is used for type specification.\nThe basic types are as follows, you can specify the type of JSON by this combination.\n\n### Basic Types\n\n- `Str`\n\n    Subtype of `Str` encodes always to string.\n\n    ```perl\n    my $encode = $jsont-\u003eencoder(Str);\n    $encode-\u003e(123) # =\u003e \"123\"\n    ```\n\n- `Num`\n\n    Subtype of `Num` encodes always to number.\n\n    ```perl\n    my $encode = $jsont-\u003eencoder(Num);\n    $encode-\u003e(123) # =\u003e 123\n    ```\n\n- `Bool`\n\n    Subtype of `Bool` encodes always to boolean.\n\n    ```perl\n    my $encode = $jsont-\u003eencoder(Bool);\n    $encode-\u003e(123) # =\u003e true\n    ```\n\n- `Dict[...]`\n\n    Subtype of `Dict[...]` encodes always to map.\n\n    ```perl\n    my $encode = $jsont-\u003eencoder(Dict[a =\u003e Int]);\n    $encode-\u003e({ a =\u003e 123 });   # =\u003e {\"a\":123}\n    $encode-\u003e({ a =\u003e '123' }); # =\u003e {\"a\":123}\n    ```\n\n- `Tuple[...]`\n\n    Subtype of `Tuple[A, B]` encodes always to list of type A and B.\n\n    ```perl\n    my $encode = $jsont-\u003eencoder(Tuple[Int, Str]);\n    $encode-\u003e([123, 456]); # =\u003e [123,\"456\"]\n    ```\n\n- `` ArrayRef[`a] ``\n\n    Subtype of `ArrayRef[A]` encodes always to list of all elements type A.\n\n    ```perl\n    my $encode = $jsont-\u003eencoder(ArrayRef[Bool]);\n    $encode-\u003e([1,0,undef,!!0,\\0]); # =\u003e [true,false,false,false,true]\n    ```\n\n- `Maybe`\n\n    `Maybe` encodes to undef if value is undef.\n\n    ```perl\n    my $encode = $jsont-\u003eencoder(Maybe[Str]);\n    $encode-\u003e('hello'); # =\u003e \"hello\"\n    $encode-\u003e(undef); # =\u003e null\n    ```\n\n- `` Optional[`a] ``\n\n    `Dict[name =\u003e Str, id =\u003e Optional[Int]]` allows `{ name =\u003e \"Bob\" }`\n    but not `{ name =\u003e \"Bob\", id =\u003e \"BOB\" }`.\n\n    ```perl\n    my $encode = $jsont-\u003eencoder(Dict[a =\u003e Optional[Str]]);\n    $encode-\u003e({a =\u003e 'foo'}); # =\u003e {\"a\":\"foo\"}\n    $encode-\u003e({}); # =\u003e {}\n    ```\n\n### More Example\n\n```perl\nuse Types::Standard -types;\nuse JSON::TypeEncoder;\n\nmy $type = ArrayRef[\n    Dict[\n        name =\u003e Str,\n        fg =\u003e Bool,\n        foo =\u003e Optional[Str],\n        bar =\u003e Maybe[Num]\n    ]\n];\n\nmy $encode = JSON::TypeEncoder-\u003enew-\u003eencoder($type);\n$encode-\u003e(\n    [\n        { name =\u003e 'a', fg =\u003e !!1, foo =\u003e '1', bar =\u003e '10' },\n        { name =\u003e 'b', fg =\u003e !!0,             bar =\u003e '11' },\n        { name =\u003e 'c', fg =\u003e !!1, foo =\u003e '2', bar =\u003e undef },\n        { name =\u003e 'd', fg =\u003e !!0,             bar =\u003e undef },\n    ]\n);\n\n# =\u003e\n# [\n#   {\"bar\":10,\"fg\":true,\"foo\":\"1\",\"name\":\"a\"},\n#   {\"bar\":11,\"fg\":false,\"name\":\"b\"},\n#   {\"bar\":null,\"fg\":true,\"foo\":\"2\",\"name\":\"c\"},\n#   {\"bar\":null,\"fg\":false,\"name\":\"d\"}\n# ]\n```\n\n# SEE ALSO\n\n[JSON::XS](https://metacpan.org/pod/JSON::XS)\n\n[JSON::Types](https://metacpan.org/pod/JSON::Types)\n\n[Types::Standard](https://metacpan.org/pod/Types::Standard)\n\n# LICENSE\n\nCopyright (C) kfly8.\n\nThis library is free software; you can redistribute it and/or modify\nit under the same terms as Perl itself.\n\n# AUTHOR\n\nkfly8 \u003ckfly@cpan.org\u003e\n\n# CONTRIBUTORS\n\n- karupanerura\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfly8%2Fp5-json-typeencoder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkfly8%2Fp5-json-typeencoder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfly8%2Fp5-json-typeencoder/lists"}