{"id":15008678,"url":"https://github.com/jaguar-dart/jaguar_json","last_synced_at":"2025-05-08T04:43:24.355Z","repository":{"id":56833192,"uuid":"92301217","full_name":"Jaguar-dart/jaguar_json","owner":"Jaguar-dart","description":"jaguar_serializer based JSON interceptors for Jaguar","archived":false,"fork":false,"pushed_at":"2018-02-02T16:26:29.000Z","size":39,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-08T04:42:57.190Z","etag":null,"topics":["dartlang","http","jaguar","json","marshall","rest-api","serialize","serializer","server"],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Jaguar-dart.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-05-24T14:25:36.000Z","updated_at":"2021-12-10T08:23:39.000Z","dependencies_parsed_at":"2022-09-13T08:40:32.938Z","dependency_job_id":null,"html_url":"https://github.com/Jaguar-dart/jaguar_json","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaguar-dart%2Fjaguar_json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaguar-dart%2Fjaguar_json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaguar-dart%2Fjaguar_json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jaguar-dart%2Fjaguar_json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jaguar-dart","download_url":"https://codeload.github.com/Jaguar-dart/jaguar_json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253002844,"owners_count":21838637,"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":["dartlang","http","jaguar","json","marshall","rest-api","serialize","serializer","server"],"created_at":"2024-09-24T19:19:59.222Z","updated_at":"2025-05-08T04:43:24.282Z","avatar_url":"https://github.com/Jaguar-dart.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jaguar_json\n\nThis package provides jaguar_serializer based interceptors, utitity methods and utility classes for Jaguar to make\nencoding and decoding JSON easier.\n\n# Interceptors\n\njaguar_json exposes interceptors to encode and decode Dart objects to and from JSON using two methods:\n\n1. Using `Serializer`\n2. Using `JsonRepo`\n\n## Using `Serializer`\n\n`Encode`, `Decode` and `Codec` interceptors are provided to encode and decode json. Both these interceptors\naccept an instance of the `Serializer\u003cModelType\u003e` class that it internally uses to serialize and\ndeserialize.\n\n`Encode` automatically serializes result returned from the route method. Care must be taken that the\nroute returns an object of type `ModelType`.\n\n`Decode` automatically de-serializes JSON body in the request to `ModelType`. The de-serialized dart\nobject can be obtained in the route method using `ctx.getInterceptorResult(Decode)`.\n\n```dart\n@Api(path: '/api/book')\nclass BookRoutes {\n  static json.Decode decoder(_) =\u003e new json.Decode(bookSerializer);\n\n  static json.Encode\u003cBook\u003e encoder(_) =\u003e new json.Encode\u003cBook\u003e(bookSerializer);\n\n  @Post()\n  @Wrap(const [decoder, encoder])\n  Book one(Context ctx) {\n    final Book book = ctx.getInterceptorResult(json.Decode);\n    return book;\n  }\n\n  @Post(path: '/many')\n  @Wrap(const [decoder, encoder])\n  List\u003cBook\u003e list(Context ctx) =\u003e ctx.getInterceptorResult(json.Decode);\n}\n```\n\n## Using `JsonRepo`\n\nUsing `JsonRepo` simplifies the encoding and decoding. First task is to create a `JsonRepo` object\nand add all the required serializers to it. \n\n`EncodeRepo`, `DecodeRepo` and `CodecRepo` interceptors are provided to encode and decode json using repo. Both these \ninterceptors accept an instance of `JsonRepo` that it internally uses to find the appropriate serializer \nto serialize and deserialize. For the decoder to find the right serializer, the incoming JSON body must have a type\nfield. The type field can be set in the `DecodeRepo` using `typeKey` argument.\n\n```dart\nfinal JsonRepo repo = new JsonRepo(\n    serializers: [personSerializer, bookSerializer], withType: true);\n\njson.CodecRepo codec(_) =\u003e new json.CodecRepo(repo);\n\n@Api(path: '/api/book')\nclass BookRoutes {\n  @Get()\n  @WrapOne(codec)\n  Book get(Context ctx) =\u003e new Book.fromNum(5);\n\n  @Post()\n  @WrapOne(codec)\n  Book post(Context ctx) {\n    final book = ctx.getInterceptorResult\u003cBook\u003e(json.CodecRepo);\n    return book;\n  }\n}\n```\n\n# `JsonRoutes` utility class\n\n`Api`s can extend `JsonRoutes` class to get access to `toJson` and `fromJson` methods that using `JsonRepo` to make\nencoding and decoding JSON easier.\n\n```dart\n@Api(path: '/api/book')\nclass BookRoutes extends Object with json.JsonRoutes {\n  JsonRepo get repo =\u003e models.repo;\n\n  @Get()\n  Response\u003cString\u003e get(Context ctx) =\u003e toJson(new Book.fromNum(5));\n\n  @Post()\n  Future\u003cResponse\u003cString\u003e\u003e post(Context ctx) async =\u003e\n      toJson(await fromJson(ctx));\n}\n```\n\n# Utility functions\n\nGlobal functions `serialize` and `deserialize` helps in encoding and decoding JSON using `Serializer`.\n\n```dart\n@Api(path: '/api/book')\nclass BookRoutes {\n  @Get()\n  Response\u003cString\u003e get(Context ctx) =\u003e\n      json.serialize(bookSerializer, new Book.fromNum(5));\n\n  @Post()\n  Future\u003cResponse\u003cString\u003e\u003e post(Context ctx) async =\u003e json.serialize(\n      bookSerializer, await json.deserialize(bookSerializer, ctx));\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaguar-dart%2Fjaguar_json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaguar-dart%2Fjaguar_json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaguar-dart%2Fjaguar_json/lists"}