{"id":14966018,"url":"https://github.com/jonathanstowe/cro-http-bodyparser-jsonclass","last_synced_at":"2026-01-21T04:32:40.145Z","repository":{"id":45241106,"uuid":"417523243","full_name":"jonathanstowe/Cro-HTTP-BodyParser-JSONClass","owner":"jonathanstowe","description":"Parse and deserialise application/json HTTP body to a specified JSON::Class","archived":false,"fork":false,"pushed_at":"2022-08-08T17:07:05.000Z","size":20,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-15T09:35:21.264Z","etag":null,"topics":["json","object","parser","raku"],"latest_commit_sha":null,"homepage":"","language":"Raku","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"artistic-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonathanstowe.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-15T14:10:24.000Z","updated_at":"2021-12-28T18:46:36.000Z","dependencies_parsed_at":"2022-08-28T11:20:28.182Z","dependency_job_id":null,"html_url":"https://github.com/jonathanstowe/Cro-HTTP-BodyParser-JSONClass","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanstowe%2FCro-HTTP-BodyParser-JSONClass","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanstowe%2FCro-HTTP-BodyParser-JSONClass/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanstowe%2FCro-HTTP-BodyParser-JSONClass/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonathanstowe%2FCro-HTTP-BodyParser-JSONClass/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonathanstowe","download_url":"https://codeload.github.com/jonathanstowe/Cro-HTTP-BodyParser-JSONClass/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248060494,"owners_count":21041168,"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":["json","object","parser","raku"],"created_at":"2024-09-24T13:35:42.211Z","updated_at":"2026-01-21T04:32:40.120Z","avatar_url":"https://github.com/jonathanstowe.png","language":"Raku","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cro::HTTP::BodyParser::JSONClass\n\nParse and deserialise application/json HTTP body to a specified JSON::Class\n\n[![CI](https://github.com/jonathanstowe/Cro-HTTP-BodyParser-JSONClass/actions/workflows/main.yml/badge.svg)](https://github.com/jonathanstowe/Cro-HTTP-BodyParser-JSONClass/actions/workflows/main.yml)\n\n## Synopsis\n\n```raku\nuse Cro::HTTP::Router;\nuse Cro::HTTP::Server;\nuse JSON::Class;\nuse Cro::HTTP::BodyParser::JSONClass;\n\n\nclass HelloClass does JSON::Class {\n    has Str $.firstname;\n    has Str $.lastname;\n\n    method hello(--\u003e Str ) {\n        \"Hello, $.firstname() $.lastname()\";\n    }\n\n}\n\n# This intermediate class is only necessary in older rakudo, as of\n# 2021.09 the parameterised role can be use directly\nclass SomeBodyParser does Cro::HTTP::BodyParser::JSONClass[HelloClass] { \n}\n\nmy $app = route {\n    body-parser SomeBodyParser;\n    post -\u003e 'hello' {\n        request-body -\u003e $hello {\n            content 'text/plain', $hello.hello;\n        }\n    }\n};\n\nmy Cro::Service $service = Cro::HTTP::Server.new(:host\u003c127.0.0.1\u003e, :port\u003c7798\u003e, application =\u003e $app);\n\n$service.start;\n\nreact  { whenever signal(SIGINT) { $service.stop; exit; } }\n```\n\n```raku\nuse Cro::HTTP::Client;\nuse JSON::Class;\nuse Cro::HTTP::BodyParser::JSONClass;\n\nmy $client1 = Cro::HTTP::Client.new: body-parsers =\u003e [Cro::HTTP::BodyParser::JSONClass[HelloClass]];\nmy $obj1 = await $client1.get-body: 'https://jsonclass.free.beeceptor.com/hello';\nsay $obj1.raku;\n=output HelloClass.new(firstname =\u003e \"fname\", lastname =\u003e \"lname\")␤\n\n# Setting the JSON class after creating an instance of Cro::HTTP::Client\nmy $body-parser = Cro::HTTP::BodyParser::JSONClass.new;\nmy $client2 = Cro::HTTP::Client.new: body-parsers =\u003e [$body-parser];\n$body-parser.set-json-class: HelloClass;\nmy $obj2 = await $client2.get-body: 'https://jsonclass.free.beeceptor.com/hello';\nsay $obj2.raku;\n=output HelloClass.new(firstname =\u003e \"fname\", lastname =\u003e \"lname\")␤\n```\n\n## Description\n\nThis provides a specialised [Cro::BodyParser](https://cro.services/docs/reference/cro-http-router#Adding_custom_request_body_parsers) that will parse a JSON ('application/json') request body to the specified\n[JSON::Class](https://github.com/jonathanstowe/JSON-Class) type.  This is useful if you have `JSON::Class` classes that you want to create from HTTP data, and will lead to less code and perhaps better\nabstraction.\n\nThe BodyParser is implemented as a Parameterised Role with the target class as the parameter.  Because this will basically over-ride the Cro's builtin JSON parsing it probably doesn't want to be installed at the\ntop level in the Cro::HTTP instance, but rather in a specific `route` block with the `body-parser` helper, also because it is specialised to a single class it may want to be isolated to its own `route`\nblock so other routes keep the default behaviour or have parsers parameterised to different classes, so you may want to do something like:\n\n```raku\nmy $app = route {\n    delegate 'hello' =\u003e route {\n        body-parser SomeBodyParser;\n        post -\u003e  {\n            request-body -\u003e $hello {\n                content 'text/plain', $hello.hello;\n            }\n        }\n    }\n};\n```\n\nThe test as to whether this body parser should be used (defined in the method `is-applicable` ) is generalised to the `application/json` content type, (hence the caveat above regarding reducing the scope.)\nIf you want to make a more specific test (or even if the Content-Type supplied *isn't* `application/json`,) then you can compose this to a new class the over-rides the `is-applicable`:\n\n```raku\nclass SomeBodyParser does Cro::HTTP::BodyParser::JSONClass[HelloClass] {\n   method is-applicable(Cro::HTTP::Message $message --\u003e Bool) {\n      $message.header('X-API-Message-Type').defined \u0026\u0026 $message.header('X-API-Message-Type') eq 'Hello';\n   }\n}\n```\n\nAnd then use `SomeBodyParser` in place of `Cro::HTTP::BodyParser::JSONClass`.\n\nThe BodyParser has a `set-json-class` method which can be used to set the `JSON::Class` class to another class whenever needed.\n\n## Installation\n\nAssuming you have a working installation of rakudo you should be able to install this with *zef* :\n\n   zef install Cro::HTTP::BodyParser::JSONClass\n\nOr from a local clone of the repository:\n\n   zef install .\n\n## Support\n\nPlease direct any patches, suggestions or feedback to [Github](https://github.com/jonathanstowe/Cro-HTTP-BodyParser-JSONClass/issues).\n\n# Licence and copyright\n\nThis is free software, please see the [LICENCE](LICENCE) file in the distribution.\n\n© Jonathan Stowe 2021\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathanstowe%2Fcro-http-bodyparser-jsonclass","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonathanstowe%2Fcro-http-bodyparser-jsonclass","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonathanstowe%2Fcro-http-bodyparser-jsonclass/lists"}