{"id":16307583,"url":"https://github.com/kfly8/p5-json-unblessobject","last_synced_at":"2025-04-12T19:57:33.796Z","repository":{"id":38327579,"uuid":"497604786","full_name":"kfly8/p5-JSON-UnblessObject","owner":"kfly8","description":"unbless object using JSON spec like Cpanel::JSON::XS::Type","archived":false,"fork":false,"pushed_at":"2022-06-07T00:33:39.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T19:57:30.263Z","etag":null,"topics":["perl"],"latest_commit_sha":null,"homepage":"","language":"Perl","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}},"created_at":"2022-05-29T13:50:59.000Z","updated_at":"2023-04-14T23:47:24.000Z","dependencies_parsed_at":"2022-08-24T02:41:12.228Z","dependency_job_id":null,"html_url":"https://github.com/kfly8/p5-JSON-UnblessObject","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-UnblessObject","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-UnblessObject/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-UnblessObject/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kfly8%2Fp5-JSON-UnblessObject/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kfly8","download_url":"https://codeload.github.com/kfly8/p5-JSON-UnblessObject/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625510,"owners_count":21135513,"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","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":["perl"],"created_at":"2024-10-10T21:14:37.812Z","updated_at":"2025-04-12T19:57:33.773Z","avatar_url":"https://github.com/kfly8.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Actions Status](https://github.com/kfly8/p5-JSON-UnblessObject/actions/workflows/test.yml/badge.svg)](https://github.com/kfly8/p5-JSON-UnblessObject/actions) [![Coverage Status](http://codecov.io/github/kfly8/p5-JSON-UnblessObject/coverage.svg?branch=main)](https://codecov.io/github/kfly8/p5-JSON-UnblessObject?branch=main) [![MetaCPAN Release](https://badge.fury.io/pl/JSON-UnblessObject.svg)](https://metacpan.org/release/JSON-UnblessObject)\n# NAME\n\nJSON::UnblessObject - unbless object using JSON spec like Cpanel::JSON::XS::Type\n\n# SYNOPSIS\n\n```perl\nuse JSON::UnblessObject qw(unbless_object);\n\nuse Cpanel::JSON::XS::Type;\n\npackage SomeEntity {\n    sub new {\n        my ($class, %args) = @_;\n        return bless \\%args, $class\n    }\n    sub a { shift-\u003e{a} }\n    sub b { shift-\u003e{b} }\n}\n\nmy $entity = SomeEntity-\u003enew(a =\u003e 123, b =\u003e 'HELLO');\n\nunbless_object($entity, { a =\u003e JSON_TYPE_INT });\n# =\u003e { a =\u003e 123 }\n\nunbless_object($entity, { b =\u003e JSON_TYPE_STRING });\n# =\u003e { b =\u003e 'HELLO' }\n\nunbless_object($entity, { a =\u003e JSON_TYPE_INT, b =\u003e JSON_TYPE_STRING });\n# =\u003e { a =\u003e 123, b =\u003e 'HELLO' }\n```\n\n# DESCRIPTION\n\nJSON::UnblessObject is designed to assist with JSON encode.\nFor example, an blessed object can be encoded using JSON spec:\n\n```perl\nmy $json = Cpanel::JSON::XS-\u003enew-\u003ecanonical;\nsub encode_json {\n    my ($data, $spec) = @_;\n\n    $data = unbless_object($data, $spec) if blessed $data;\n    $json-\u003eencode($data, $spec)\n}\n\nencode_json($entity, { a =\u003e JSON_TYPE_INT });\n# =\u003e {\"a\":123}\n\nencode_json($entity, { b =\u003e JSON_TYPE_STRING });\n# =\u003e {\"b\":\"HELLO\"}\n\nencode_json($entity, { a =\u003e JSON_TYPE_INT, b =\u003e JSON_TYPE_STRING }),\n# =\u003e {\"a\":123,\"b\":\"HELLO\"}\n```\n\n## RESOLVERS\n\nThe unbless\\_object function performs a resolver for a given object type.\n\n- resolve\\_arrayref($object, $spec)\n\n    When `$spec` is `ARRAYREF`, executes this function.\n    `$object` must either have `@{}` overload or be an iterator with `next` method.\n    If `$spec` is `[JSON_TYPE_STRING, JSON_TYPE_STRING]`, then resolve like this `list($object)-\u003e[0], list($object)-\u003e[1]`. `list` function is an internal utility function that converts `$object` to arrayref.\n\n- resolve\\_hashref($object, $spec)\n\n    When `$spec` is `HASHREF`, executes this function.\n    If `$spec` is `{ foo =\u003e JSON_TYPE_STRING, bar =\u003e JSON_TYPE_STRING }`, then resolve like this `{ foo =\u003e $object-\u003efoo, bar =\u003e $object-\u003ebar }`.\n\n- resolve\\_json\\_type\\_arrayof($object, $spec)\n\n    When `$spec` is `Cpanel::JSON::XS::Type::ArrayOf`, executes this function.\n    `$object` must either have `@{}` overload or be an iterator with `next` method.\n\n- resolve\\_json\\_type\\_hashof($object, $spec)\n\n    When `$spec` is `Cpanel::JSON::XS::Type::HashOf`, executes this function.\n    `$object` requires `JSON_KEYS` function. `JSON_KEYS` method is a whitelist of `$object`\n    that are allowed to be published as JSON.\n\n    ```perl\n    package SomeEntity {\n        sub new {\n            my ($class, %args) = @_;\n            return bless \\%args, $class\n        }\n\n        sub secret { shift-\u003e{secret} }\n\n        sub a { shift-\u003e{a} }\n        sub b { shift-\u003e{b} }\n\n        # Do not include keys that cannot be published like `secret`\n        sub JSON_KEYS { qw/a b/ }\n    }\n\n    my $entity = SomeEntity-\u003enew(a =\u003e 1, b =\u003e 2, secret =\u003e 'XXX');\n    unbless_object($entity, json_type_hashof(JSON_TYPE_STRING))\n    # =\u003e { a =\u003e 1, b =\u003e 2 }\n    ```\n\n- resolve\\_json\\_type\\_anyof($object, $spec)\n\n    When `$spec` is `Cpanel::JSON::XS::Type::AnyOf`, executes this function.\n    If `$object` is available as array, it is resolved as array; if it is available as hash, it is resolved as hash; otherwise, it is resolved as scalar.\n\n# SEE ALSO\n\n[Cpanel::JSON::XS::Type](https://metacpan.org/pod/Cpanel%3A%3AJSON%3A%3AXS%3A%3AType)\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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfly8%2Fp5-json-unblessobject","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkfly8%2Fp5-json-unblessobject","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkfly8%2Fp5-json-unblessobject/lists"}