{"id":22711606,"url":"https://github.com/polyfloyd/gopolyjson","last_synced_at":"2025-10-03T23:59:37.892Z","repository":{"id":41386862,"uuid":"400292100","full_name":"polyfloyd/gopolyjson","owner":"polyfloyd","description":"Moved to https://codeberg.org/polyfloyd/go-polyjson","archived":true,"fork":false,"pushed_at":"2025-09-01T18:09:55.000Z","size":54,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-03T23:59:36.965Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/polyfloyd.png","metadata":{"files":{"readme":"README.md","changelog":null,"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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-08-26T20:04:51.000Z","updated_at":"2025-09-02T18:05:58.000Z","dependencies_parsed_at":"2023-02-17T08:20:17.415Z","dependency_job_id":"4be5acbb-ab30-48d2-a424-a8dbbdcc7354","html_url":"https://github.com/polyfloyd/gopolyjson","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/polyfloyd/gopolyjson","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyfloyd%2Fgopolyjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyfloyd%2Fgopolyjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyfloyd%2Fgopolyjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyfloyd%2Fgopolyjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/polyfloyd","download_url":"https://codeload.github.com/polyfloyd/gopolyjson/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/polyfloyd%2Fgopolyjson/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278245393,"owners_count":25955016,"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-10-03T02:00:06.070Z","response_time":53,"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":[],"created_at":"2024-12-10T13:07:33.723Z","updated_at":"2025-10-03T23:59:37.861Z","avatar_url":"https://github.com/polyfloyd.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"Go PolyJSON\n===========\n\n[![Build Status](https://github.com/polyfloyd/gopolyjson/workflows/CI/badge.svg)](https://github.com/polyfloyd/gopolyjson/actions)\n\nGo Code generator of JSON marshalers/unmarshalers for polymorphic\ndata structures.\n\n\n## Usage\n```\ngo install -v github.com/polyfloyd/gopolyjson/cmd/polyjson@latest\n```\n\nThe program targets a single package and generates marshalers for one or more\npolymorphic types along with unmarshalers for structures that use such\npolymorphic types in fields.\n\nThe recommended usage is to define an interface denoting the polymorphic types\nwith at least a method that:\n* Takes no arguments\n* Returns nothing\n* Is private (starts with a lowercase letter)\n\nThe program will automatically discover associated variants that implement this\ninterface.\n```go\npackage shapes\n\ntype Shape interface {\n\tiShape()\n}\n\nfunc (Triangle) iShape() {}\nfunc (Square) iShape()   {}\n\ntype Square struct {\n\tTopLeft       [2]int\n\tWidth, Height int\n}\n\ntype Triangle struct {\n\tP0 [2]int\n\tP1 [2]int\n\tP2 [2]int\n}\n```\nInvoke the generator by specifying the name of the interface with `-type`:\n```\npolyjson -type Shape -package ./shapes\n```\nThis will place a `polyjsongen.go` file in the package specified containing the\n(un)marshalers.\n\nNow, you can encode and decode your data like this:\n```go\ninputShape := Square{TopLeft: [2]int{1, 2}, Width: 4, Height: 4}\n\nb, err := json.Marshal(inputShape)\nfmt.Printf(\"%s\\n\", b) // {\"kind\": \"Square\", \"TopLeft\": [1, 2], \"Width\": 4, \"Height\": 4}\n\ndecodedShape, err := UnmarshalShapeJSON(b)\nfmt.Printf(\"%T\\n\", decodedShape) // Square\n````\n\n\n## Reference\n\n### Discriminant\nEach struct that is encoded to JSON that is a variant of a polymorphic type\ngains an additional field that holds the name of the type. It is placed in\nbetween the other fields of the struct. The default name of this field is\n`kind` and it can be altered by setting the `-discriminant` flag.\n\n### Specifying types\nThe `-type` argument accepts a polymorphic type specification in the following forms:\n* `-type Shape` Interface only\n* `-type Shape:Triangle,Square` Interface with explicitly named variants\n* `-type Shape:Triangle=triangle_shape,Square=square_shape` Interface with\n  explicitly named variants and their JSON counterparts\n\n### Usage in Go\nTo encode a struct, just use `json.Marshal` like you normally would.\n\nTo decode a polymorphic type, use the generated `Unmarshal\u003ctype\u003eJSON`, e.g.\n`UnmarshalShapeJSON`. This will probe the JSON object for the variant kind and\nunmarshal into the associated struct.\n\n### Interoperability with Rust and Serde\nPolyJSON was initially made for exchanging data between Go and Rust services.\nRust in combination with [serde_json](https://crates.io/crates/serde-json) can\nunderstand the format generated by this tool like so:\n\n```rust\n#[derive(Serialize, Deserialize)]\n#[serde(tag = \"kind\")]\npub enum Shape {\n\tSquare {\n\t\ttop_left: [i32; 2],\n\t\twidth: i32,\n\t\theight: i32,\n\t},\n\tTriangle {\n\t\tp0: [i32; 2],\n\t\tp1: [i32; 2],\n\t\tp2: [i32; 2],\n\t},\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolyfloyd%2Fgopolyjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolyfloyd%2Fgopolyjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolyfloyd%2Fgopolyjson/lists"}