{"id":16674169,"url":"https://github.com/byrnedo/pjson","last_synced_at":"2025-04-09T20:15:40.259Z","repository":{"id":48156302,"uuid":"516414969","full_name":"byrnedo/pjson","owner":"byrnedo","description":"Helps to easily JSON marshal / unmarshal tagged unions in go","archived":false,"fork":false,"pushed_at":"2023-04-11T11:27:24.000Z","size":45,"stargazers_count":20,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-09T20:15:35.147Z","etag":null,"topics":["discriminated-unions","discriminator","generics","go","json","polymorphic-deserialization","tagged-unions"],"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/byrnedo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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":"2022-07-21T14:57:31.000Z","updated_at":"2025-03-27T17:12:21.000Z","dependencies_parsed_at":"2024-06-20T06:07:36.344Z","dependency_job_id":null,"html_url":"https://github.com/byrnedo/pjson","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byrnedo%2Fpjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byrnedo%2Fpjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byrnedo%2Fpjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/byrnedo%2Fpjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/byrnedo","download_url":"https://codeload.github.com/byrnedo/pjson/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103872,"owners_count":21048245,"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":["discriminated-unions","discriminator","generics","go","json","polymorphic-deserialization","tagged-unions"],"created_at":"2024-10-12T12:29:36.054Z","updated_at":"2025-04-09T20:15:40.237Z","avatar_url":"https://github.com/byrnedo.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pjson\n\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/byrnedo/pjson/blob/master/LICENSE.txt)\n[![Go Reference](https://pkg.go.dev/badge/github.com/byrnedo/pjson.svg)](https://pkg.go.dev/github.com/byrnedo/pjson)\n[![Go Coverage](https://github.com/byrnedo/pjson/wiki/coverage.svg)](https://raw.githack.com/wiki/byrnedo/pjson/coverage.html)\n[![Go Report Card](https://goreportcard.com/badge/github.com/byrnedo/pjson)](https://goreportcard.com/report/github.com/byrnedo/pjson)\n\nHelp to easily JSON marshal / unmarshal tagged unions in go\n\nA tagged union / discriminating type is, for instance with the following JSON:\n\n```json\n[\n  {\n    \"type\": \"a\",\n    \"a_name\": \"AName\",\n    \"a_foo\": \"FOO\"\n  },\n  {\n    \"type\": \"b\",\n    \"b_name\": \"BName\",\n    \"b_goo\": \"GOO\"\n  }\n]\n```\n\nThe `type` field denotes which type the object is. So many object share a common discriminating field.\nIn some languages this is supported, but not in go.\n\n**Pjson** gives us a helper `pjson.Tagged` type to create these pseudo tagged unions that can be automatically\nserialized and deserialized to and from JSON.\n\n## Usage\n\n```go\npackage readme\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"reflect\"\n\n\t\"github.com/byrnedo/pjson\"\n)\n\ntype Foo struct {\n\tA string `json:\"a\"`\n}\n\n// set it's tag value\nfunc (a Foo) Variant() string {\n\treturn \"foo\"\n}\n\ntype Bar struct {\n\tB string `json:\"b\"`\n}\n\nfunc (b Bar) Variant() string {\n\treturn \"bar\"\n}\n\n// specify the union\ntype FooBarUnion struct{}\n\nfunc (u FooBarUnion) Field() string { return \"type\" }\n\nfunc (u FooBarUnion) Variants() []pjson.Variant {\n\treturn []pjson.Variant{\n\t\tFoo{}, Bar{},\n\t}\n}\n\nfunc ExampleReadme() {\n\t// now that we have our types we can use Tagged\n\to := pjson.Tagged[FooBarUnion]{}\n\n\tbytes := []byte(`{\"type\": \"foo\", \"a\": \"AAAA\"}`)\n\n\terr := json.Unmarshal(bytes, \u0026o)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(reflect.TypeOf(o.Value), o.Value)\n\n\tbytes, _ = json.Marshal(o)\n\tfmt.Println(string(bytes))\n\n\t// Output: *pjson_test.Foo \u0026{AAAA}\n\t// {\"a\":\"AAAA\",\"type\":\"foo\"}\n}\n```\n\n## Benchmarks\n\nMacbook Pro M1 2022\n\n```\nBenchmark/unmarshal_with_pjson\nBenchmark/unmarshal_with_pjson-10         \t  867177\t      1356 ns/op\nBenchmark/unmarshal_without_pjson\nBenchmark/unmarshal_without_pjson-10      \t 1793629\t       670.6 ns/op\nBenchmark/marshal_with_pjson\nBenchmark/marshal_with_pjson-10           \t 2415705\t       488.7 ns/op\nBenchmark/marshal_without_pjson\nBenchmark/marshal_without_pjson-10        \t10956252\t       109.8 ns/op\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyrnedo%2Fpjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbyrnedo%2Fpjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbyrnedo%2Fpjson/lists"}