{"id":24071004,"url":"https://github.com/markrosemaker/jsonutil","last_synced_at":"2026-05-15T09:32:28.807Z","repository":{"id":264023064,"uuid":"892126127","full_name":"MarkRosemaker/jsonutil","owner":"MarkRosemaker","description":"Provides custom JSON v2 marshaling and unmarshaling functions for specific data types, e.g. JSON encoding and decoding for types like time.Duration and url.URL.","archived":false,"fork":false,"pushed_at":"2024-12-28T16:04:00.000Z","size":581,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-09T16:24:16.792Z","etag":null,"topics":["golang","json","json-v2","marshal","marshaller","time-duration","unmarshal","unmarshaller","url"],"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/MarkRosemaker.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}},"created_at":"2024-11-21T14:56:01.000Z","updated_at":"2024-12-28T16:04:03.000Z","dependencies_parsed_at":"2024-12-15T22:21:35.008Z","dependency_job_id":"eed155ae-c94e-462f-8334-9e6eaff169c9","html_url":"https://github.com/MarkRosemaker/jsonutil","commit_stats":null,"previous_names":["markrosemaker/jsonutil"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkRosemaker%2Fjsonutil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkRosemaker%2Fjsonutil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkRosemaker%2Fjsonutil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MarkRosemaker%2Fjsonutil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MarkRosemaker","download_url":"https://codeload.github.com/MarkRosemaker/jsonutil/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240935616,"owners_count":19881169,"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":["golang","json","json-v2","marshal","marshaller","time-duration","unmarshal","unmarshaller","url"],"created_at":"2025-01-09T16:24:13.006Z","updated_at":"2026-05-15T09:32:28.801Z","avatar_url":"https://github.com/MarkRosemaker.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON v2 Utilities\n[![Go Reference](https://pkg.go.dev/badge/github.com/MarkRosemaker/jsonutil.svg)](https://pkg.go.dev/github.com/MarkRosemaker/jsonutil)\n[![Go Report Card](https://goreportcard.com/badge/github.com/MarkRosemaker/jsonutil)](https://goreportcard.com/report/github.com/MarkRosemaker/jsonutil)\n![Code Coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)\n\u003cp align=\"center\"\u003e\n  \u003cimg alt=\"jsonutil logo: cute golang gopher in JSON brackets with gear in the background and text saying v2\" src=logo.jpg width=300\u003e\n\u003c/p\u003e\n\n`jsonutil` is a Go package that provides custom [JSON v2](https://pkg.go.dev/encoding/json/v2) marshaling and unmarshaling functions for specific data types.\n\nThis package is particularly useful when you need to handle JSON encoding and decoding for types like `time.Duration` and `url.URL` in a customized manner.\n\n## Features\n\n* Custom marshaler and unmarshaler for `url.URL`:\n  * `URLMarshal` marshals `url.URL` as a string.\n  * `URLUnmarshal` unmarshals `url.URL` from a string.\n* Custom marshaler and unmarshaler for `time.Duration`:\n  * `DurationMarshalIntSeconds` marshals `time.Duration` as an integer representing seconds.\n  * `DurationUnmarshalIntSeconds` unmarshals `time.Duration` from an integer assuming it represents seconds.\n* Custom marshaler for maps with ordered keys:\n  * `OrderedMapMarshal[M ~map[K]V, K cmp.Ordered, V any]` marshals `M` so that the keys are sorted.\n* Custom marshaler for `http.Header`:\n  * `HTTPHeaderMarshal` marshals the values of `http.Header` as single strings.\n  * `HTTPHeaderUnmarshal` unmarshals the values of `http.Header` from single strings.\n\n## Installation\n\nTo install the library, use the following command:\n\n```shell\ngo get github.com/MarkRosemaker/jsonutil\n```\n\n## Usage\n\n### Custom Marshaling and Unmarshaling for `url.URL`\n\nTo use the custom marshaler and unmarshaler for `url.URL`, you can import the package and use the provided functions:\n\n```go\npackage main\n\nimport (\n\t\"encoding/json/v2\"\n\t\"fmt\"\n\t\"net/url\"\n\n\t\"github.com/MarkRosemaker/jsonutil\"\n)\n\nvar jsonOpts = json.JoinOptions(\n\tjson.WithMarshalers(json.NewMarshalers(json.MarshalToFunc(jsonutil.URLMarshal))),\n\tjson.WithUnmarshalers(json.NewUnmarshalers(json.UnmarshalFromFunc(jsonutil.URLUnmarshal))),\n)\n\ntype MyStruct struct {\n\tFoo  string  `json:\"foo\"`\n\tLink url.URL `json:\"link\"`\n}\n\nfunc main() {\n\tout := \u0026MyStruct{}\n\tif err := json.Unmarshal([]byte(`{\"foo\":\"bar\",\"link\":\"https://example.com/\"}`), out, jsonOpts); err != nil {\n\t\tpanic(err)\n\t}\n\n\tif out.Link.String() != \"https://example.com/\" {\n\t\tpanic(\"something went wrong\")\n\t}\n\n\tres, err := json.Marshal(MyStruct{\n\t\tFoo:  \"baz\",\n\t\tLink: url.URL{Scheme: \"http\", Host: \"example.com\", Path: \"/some-path\"},\n\t}, jsonOpts)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(string(res))\n\t// Output: {\"foo\":\"baz\",\"link\":\"http://example.com/some-path\"}\n}\n```\n\n### Custom Marshaling and Unmarshaling for `time.Duration`\n\nTo use the custom marshaler and unmarshaler for `time.Duration`, you can import the package and use the provided functions:\n\n```go\npackage main\n\nimport (\n\t\"encoding/json/v2\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/MarkRosemaker/jsonutil\"\n)\n\nvar jsonOpts = json.JoinOptions(\n\tjson.WithMarshalers(json.MarshalToFunc(jsonutil.DurationMarshalIntSeconds)),\n\tjson.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.DurationUnmarshalIntSeconds)),\n)\n\ntype MyStruct struct {\n\tFoo      string        `json:\"foo\"`\n\tDuration time.Duration `json:\"duration\"`\n}\n\nfunc main() {\n\tout := \u0026MyStruct{}\n\tif err := json.Unmarshal([]byte(`{\"foo\":\"bar\",\"duration\":3}`), out, jsonOpts); err != nil {\n\t\tpanic(err)\n\t}\n\n\tif out.Duration != 3*time.Second {\n\t\tpanic(\"something went wrong\")\n\t}\n\n\tres, err := json.Marshal(MyStruct{\n\t\tFoo:      \"baz\",\n\t\tDuration: time.Minute,\n\t}, jsonOpts)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(string(res))\n\t// Output: {\"foo\":\"baz\",\"duration\":60}\n}\n```\n\n### Custom Marshaling for Maps with Ordered Keys\n\nTo use the custom marshaler for `M` where `[M ~map[K]V, K cmp.Ordered, V any]`, you can import the package and use the provided functions:\n\n```go\npackage main\n\nimport (\n\t\"encoding/json/v2\"\n\t\"fmt\"\n\n\t\"github.com/MarkRosemaker/jsonutil\"\n)\n\ntype myMap map[string]int\n\nvar jsonOpts = json.JoinOptions(\n\tjson.WithMarshalers(json.MarshalToFunc(jsonutil.OrderedMapMarshal[myMap])),\n)\n\nfunc main() {\n\tres, err := json.Marshal(myMap{\n\t\t\"foo\": 1,\n\t\t\"bar\": 2,\n\t}, jsonOpts)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(string(res))\n\t// Output: {\"bar\":2,\"bar\":1}\n}\n```\n\n### Custom Marshaling and Unmarshaling for `http.Header`\n\nTo use the custom marshaler and unmarshaler for `http.Header`, you can import the package and use the provided functions:\n\n```go\npackage main\n\nimport (\n\t\"encoding/json/v2\"\n\t\"fmt\"\n\t\"net/http\"\n\n\t\"github.com/MarkRosemaker/jsonutil\"\n)\n\nvar jsonOpts = json.JoinOptions(\n\tjson.WithMarshalers(json.MarshalToFunc(jsonutil.HTTPHeaderMarshal)),\n\tjson.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.HTTPHeaderUnmarshal)),\n)\n\nfunc main() {\n\tout := \u0026http.Header{}\n\tif err := json.Unmarshal([]byte(`{\"foo\":\"bar\",\"baz\":\"quux\"}`), out, jsonOpts); err != nil {\n\t\tpanic(err)\n\t}\n\n\tres, err := json.Marshal(http.Header{\n\t\t\"foo\": []string{\"bar\"},\n\t\t\"baz\": []string{\"quux\"},\n\t}, jsonOpts)\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\n\tfmt.Println(string(res))\n\t// Output: {\"Foo\":\"bar\",\"Baz\":\"quux\"}\n}\n```\n\n## Contributing\n\nIf you have any contributions to make, please submit a pull request or open an issue on the [GitHub repository](https://github.com/MarkRosemaker/jsonutil).\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkrosemaker%2Fjsonutil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkrosemaker%2Fjsonutil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkrosemaker%2Fjsonutil/lists"}