{"id":13413248,"url":"https://github.com/cocoonspace/dynjson","last_synced_at":"2025-03-14T19:31:40.424Z","repository":{"id":46455144,"uuid":"261679779","full_name":"cocoonspace/dynjson","owner":"cocoonspace","description":"Client-customizable JSON formats for dynamic APIs","archived":false,"fork":false,"pushed_at":"2023-04-12T06:38:51.000Z","size":44,"stargazers_count":16,"open_issues_count":1,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-30T20:43:28.718Z","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/cocoonspace.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}},"created_at":"2020-05-06T07:10:02.000Z","updated_at":"2023-09-26T22:57:53.000Z","dependencies_parsed_at":"2024-01-08T15:34:38.216Z","dependency_job_id":null,"html_url":"https://github.com/cocoonspace/dynjson","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoonspace%2Fdynjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoonspace%2Fdynjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoonspace%2Fdynjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cocoonspace%2Fdynjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cocoonspace","download_url":"https://codeload.github.com/cocoonspace/dynjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243635283,"owners_count":20322910,"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-07-30T20:01:36.056Z","updated_at":"2025-03-14T19:31:40.115Z","avatar_url":"https://github.com/cocoonspace.png","language":"Go","readme":"# dynjson [![PkgGoDev](https://pkg.go.dev/badge/github.com/cocoonspace/dynjson)](https://pkg.go.dev/github.com/cocoonspace/dynjson) [![Build Status](https://app.travis-ci.com/cocoonspace/dynjson.svg?branch=master)](https://app.travis-ci.com/github/cocoonspace/dynjson) [![Coverage Status](https://coveralls.io/repos/github/cocoonspace/dynjson/badge.svg?branch=master)](https://coveralls.io/github/cocoonspace/dynjson?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/cocoonspace/dynjson)](https://goreportcard.com/report/github.com/cocoonspace/dynjson)\n\nClient-customizable JSON formats for dynamic APIs.\n\n## Introduction\n\ndynjson allow APIs to return only fields selected by the API client:\n\n```\nGET https://api.example.com/v1/foos\n[{\"id\":1,foo\":1,\"bar\":2,\"baz\":3}]\n\nGET https://api.example.com/v1/foos?select=foo\n[{\"foo\":1}]\n\nGET https://api.example.com/v1/foos/1?select=foo\n{\"foo\":1}\n```\n\ndynjson mimicks the original struct using the original types and json tags.\nThe field order is the same as the select parameters.\n\n## Installation\n\ngo get github.com/cocoonspace/dynjson\n\n## Examples\n\n```go\ntype APIResult struct {\n    Foo int     `json:\"foo\"`\n    Bar string  `json:\"bar\"`\n}\n\nf := dynjson.NewFormatter()\n\nres := \u0026APIResult{Foo:1, Bar:\"bar\"}\no, err := f.Format(res, dynjson.FieldsFromRequest(r))\nif err != nil {\n    // handle error\n}\nerr = json.NewEncoder(w).Encode(o) // {\"foo\": 1}\n```\n\nWith struct fields:\n\n\n```go\ntype APIResult struct {\n    Foo int          `json:\"foo\"`\n    Bar APIIncluded  `json:\"bar\"`\n}\n\ntype APIIncluded struct {\n    BarFoo int    `json:\"barfoo\"`\n    BarBar string `json:\"barbar\"`\n}\n\nf := dynjson.NewFormatter()\n\nres := \u0026APIResult{Foo: 1, Bar: APIIncluded{BarFoo:1, BarBar: \"bar\"}}\no, err := f.Format(res, []string{\"foo\", \"bar.barfoo\"})\nif err != nil {\n    // handle error\n}\nerr = json.NewEncoder(w).Encode(o) // {\"foo\": 1, \"bar\":{\"barfoo\": 1}}\n```\n\nWith slices:\n\n```go\ntype APIResult struct {\n    Foo int     `json:\"foo\"`\n    Bar string  `json:\"bar\"`\n}\n\nf := dynjson.NewFormatter()\n\nres := []APIResult{{Foo: 1, Bar: \"bar\"}}\no, err := f.Format(res, []string{\"foo\"})\nif err != nil {\n    // handle error\n}\nerr = json.NewEncoder(w).Encode(o) // [{\"foo\": 1}]\n```\n\n\n```go\ntype APIResult struct {\n    Foo int        `json:\"foo\"`\n    Bar []APIItem  `json:\"bar\"`\n}\n\ntype APIItem struct {\n    BarFoo int    `json:\"barfoo\"`\n    BarBar string `json:\"barbar\"`\n}\n\nf := dynjson.NewFormatter()\n\nres := \u0026APIResult{Foo: 1, Bar: []APIItem{{BarFoo: 1, BarBar: \"bar\"}}}\no, err := f.Format(res, []string{\"foo\", \"bar.barfoo\"})\nif err != nil {\n    // handle error\n}\nerr = json.NewEncoder(w).Encode(o) // {\"foo\": 1, \"bar\":[{\"barfoo\": 1}]}\n```\n\n## Limitations\n\n* Anonymous fields without a json tag (embedded by the Go JSON encoder in the enclosing struct) are not supported,\n* Maps are copied as is, you cannot filter map contents using `map_field_name.map_key`.\n\n## Performance impact\n\n```\nBenchmarkFormat_Fields\nBenchmarkFormat_Fields-8     \t 2466639\t       480 ns/op\t     184 B/op\t       7 allocs/op\nBenchmarkFormat_NoFields\nBenchmarkFormat_NoFields-8   \t 5255031\t       232 ns/op\t      32 B/op\t       1 allocs/op\nBenchmarkRawJSON\nBenchmarkRawJSON-8           \t 5351313\t       223 ns/op\t      32 B/op\t       1 allocs/op\n```\n\n## Contribution guidelines\n\nContributions are welcome, as long as:\n* unit tests \u0026 comments are included,\n* no external package is used.\n\n## License\n\nMIT - See LICENSE","funding_links":[],"categories":["JSON","Go","Relational Databases"],"sub_categories":["Search and Analytic Databases","Advanced Console UIs","SQL 查询语句构建库","检索及分析资料库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocoonspace%2Fdynjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcocoonspace%2Fdynjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcocoonspace%2Fdynjson/lists"}