{"id":23391428,"url":"https://github.com/ahrefs/ppx_deriving_atd","last_synced_at":"2025-04-08T14:47:44.829Z","repository":{"id":265797177,"uuid":"852986182","full_name":"ahrefs/ppx_deriving_atd","owner":"ahrefs","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-05T20:07:37.000Z","size":43,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T00:17:04.025Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ahrefs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2024-09-05T19:19:44.000Z","updated_at":"2024-09-05T19:20:46.000Z","dependencies_parsed_at":"2024-12-01T01:02:41.743Z","dependency_job_id":null,"html_url":"https://github.com/ahrefs/ppx_deriving_atd","commit_stats":null,"previous_names":["ahrefs/ppx_deriving_atd"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahrefs%2Fppx_deriving_atd","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahrefs%2Fppx_deriving_atd/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahrefs%2Fppx_deriving_atd/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahrefs%2Fppx_deriving_atd/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahrefs","download_url":"https://codeload.github.com/ahrefs/ppx_deriving_atd/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247866127,"owners_count":21009238,"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":[],"created_at":"2024-12-22T04:17:27.519Z","updated_at":"2025-04-08T14:47:44.812Z","avatar_url":"https://github.com/ahrefs.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ppx\\_deriving\\_atd\n\n## Overview\nThis package introduces a PPX (Preprocessor Extension) for OCaml that automates the generation of serialization and deserialization functions for your OCaml types using [ATD](https://github.com/ahrefs/atd) without creating an ATD file.  \n\nCurrently supporting JSON derivation only:\n- `json_string_of_t`: Serializes an OCaml value to a JSON string.\n- `t_of_json_string`: Deserializes a JSON string back into an OCaml value.\n\n## Usage\n\n```ocaml\ntype my_type = {\n  field1: string;\n  field2: int;\n} [@@deriving atd_j]\n\n(* Serialization *)\nlet my_value = { field1 = \"value1\"; field2 = 123 }\nlet json_string = json_string_of_my_type my_value (* {\"field1\":\"value1\",\"field2\":123} *)\n\n(* Deserialization *)\nlet deserialized_value = my_type_of_json_string json_string\n\n```\n\n### Supported Annoations\n#### default\nAnnotating a record field with `[@default v]` will allow the generated reader to give default value for the field (and so the field no longer have an optional type).  Note `v` must be in a stringify form: `\"999\"` instead of `999`.\n\n```ocaml\ntype my_type = {\n  field1: string;\n  field2: int [@default \"999\"];\n} [@@deriving atd_j]\n\n\n(* Deserialization *)\nlet deserialized_value = my_type_of_json_string {|{\"field1\":\"value1\"}|}\n(* { field1 = \"value1\"; field2 = 999 } *)\n\n```\n\n#### required\nAll option typed fields are \"optional\" by default, i.e. if the value is `None`, it will not be included when serialization and when deserializing, ATD will assume the value is `None` if the field is not present.  \n\nAnnotating a record field with `[@required]` will always keep the optional value in the serialized record and ATD will keep looking for it when deserializing the records.\n\n```ocaml\ntype my_type = {\n  field1: string option;\n  field2: int option [@required];\n} [@@deriving atd_j]\n\n(* Serialization *)\nlet my_value = { field1 = None; field2 = None }\nlet json_string = json_string_of_my_type my_value (* {\"field2\":\"None\"} *)\n\n(* Deserialization *)\nlet deserialized_value = my_type_of_json_string \"{}\"\n(* Fatal error: exception Atdgen_runtime.Oj_run.Error(\"Line 1:\\nMissing record field field2\") *)\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahrefs%2Fppx_deriving_atd","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahrefs%2Fppx_deriving_atd","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahrefs%2Fppx_deriving_atd/lists"}