{"id":37112039,"url":"https://github.com/pennocktech/swallowjson","last_synced_at":"2026-01-14T13:15:56.050Z","repository":{"id":50626903,"uuid":"68067097","full_name":"PennockTech/swallowjson","owner":"PennockTech","description":"Golang: Support for preserving unknown fields from JSON when unmarshalling instead of discarding","archived":false,"fork":false,"pushed_at":"2025-05-28T00:55:13.000Z","size":28,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-05T21:20:32.770Z","etag":null,"topics":["encoding","golang","json","json-fields","reflect","swallow"],"latest_commit_sha":null,"homepage":null,"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/PennockTech.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":"2016-09-13T02:35:29.000Z","updated_at":"2025-05-28T00:55:16.000Z","dependencies_parsed_at":"2023-10-11T00:17:34.540Z","dependency_job_id":"58bd04bc-4343-40b7-b798-faf261d63946","html_url":"https://github.com/PennockTech/swallowjson","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/PennockTech/swallowjson","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PennockTech%2Fswallowjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PennockTech%2Fswallowjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PennockTech%2Fswallowjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PennockTech%2Fswallowjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PennockTech","download_url":"https://codeload.github.com/PennockTech/swallowjson/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PennockTech%2Fswallowjson/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28420928,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T10:47:48.104Z","status":"ssl_error","status_checked_at":"2026-01-14T10:46:19.031Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["encoding","golang","json","json-fields","reflect","swallow"],"created_at":"2026-01-14T13:15:55.164Z","updated_at":"2026-01-14T13:15:56.036Z","avatar_url":"https://github.com/PennockTech.png","language":"Go","readme":"swallowjson\n===========\n\n[![Continuous Integration](https://github.com/PennockTech/swallowjson/actions/workflows/pushes.yaml/badge.svg)](https://github.com/PennockTech/swallowjson/actions/workflows/pushes.yaml)\n[![Documentation](https://godoc.org/go.pennock.tech/swallowjson?status.svg)](https://godoc.org/go.pennock.tech/swallowjson)\n[![Coverage Status](https://coveralls.io/repos/github/PennockTech/swallowjson/badge.svg?branch=main)](https://coveralls.io/github/PennockTech/swallowjson?branch=main)\n\nThe `swallowjson` Golang library provides a simple-to-use support function to\nuse to implement an interface method in your type, to aid in JSON decoding.\n\nWhen decoding JSON into a struct, the default Golang handling is to discard\nthe JSON fields encountered which have not been declared as struct fields.\nThe `UnmarshalWith` function lets you implement the `UnmarshalJSON` method as\na one-liner, declaring a map within the struct which should swallow all JSON\nfields not otherwise accepted by the struct.\n\nA simple example:\n\n```go\ntype MyType struct {\n\tFoo  string                 `json:\"foo\"`\n\tBar  int                    `json:\"bar\"`\n\tRest map[string]interface{} `json:\"-\"`\n}\n\nfunc (mt *MyType) UnmarshalJSON(raw []byte) error {\n\treturn swallowjson.UnmarshalWith(mt, \"Rest\", raw)\n}\n```\n\nYou can then decode as normal for Golang, letting the JSON decoder dispatch to\nyour overridden `UnmarshalJSON` method whenever it expects to decode a\n`MyType` (whether at the top-level, or nested inside other types, etc):\n\n```go\nvar myData MyType\nif err := json.Unmarshal(rawBytes, \u0026myData); err != nil {\n\tprocessError(err)\n}\n```\n\nWhen invoked on `mt`, which should already exist as a struct,\n`swallowjson.UnmarshalWith` will populate `Foo` and `Bar` from JSON fields\n`foo` and `bar` respectively, per normal Golang decoding rules.  But if the\nJSON also contains fields `baz` and `bat` then those will end up as keys,\nholding their child data, in the `Rest` map.\n\nThis library was written as a fairly quick proof-of-concept for a friend and\nlater gained sufficient tests as to be something which can be relied upon as a\ncore component, with no other dependencies.\nThe library is released in the hopes that it might prove useful to others.\n\nBehavior notes:\n\n* The Golang [encoding/json][] library has a bunch of legacy\n  case-insensitivity handling in the default unmarshaller; while swallowjson\n  builds upon that library (it uses `json.NewDecoder()` under the hood) our\n  API is a new API, implemented in new code and is not a drop-in replacement.\n  Thus there is no need to preserve backwards behavior here, so I didn't\n  implement that case-insensitivity.\n* The `Rest` map will be created on-demand; if no unexpected keys are seen and\n  the map is `nil` going in, then it will still be `nil` afterwards.\n* The `Rest` map can have arbitrary value types, but if the content won't\n  parse then you'll get an error.  Sensible choices for generic usage include\n  `interface{}` and `json.RawMessage`.\n\nCanonical import path is: `go.pennock.tech/swallowjson`\n\n---\nCopyright © 2016-2020 Pennock Tech, LLC  \nLicensed per [LICENSE.txt](./LICENSE.txt)\n\n[encoding/json]: https://golang.org/pkg/encoding/json/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpennocktech%2Fswallowjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpennocktech%2Fswallowjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpennocktech%2Fswallowjson/lists"}