{"id":16788631,"url":"https://github.com/breml/errchkjson","last_synced_at":"2025-10-06T19:05:08.477Z","repository":{"id":42971420,"uuid":"425186966","full_name":"breml/errchkjson","owner":"breml","description":"Go linter that checks types that are json encoded - reports unsupported types and unnecessary error checks","archived":false,"fork":false,"pushed_at":"2025-03-12T19:32:04.000Z","size":103,"stargazers_count":41,"open_issues_count":1,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T11:05:58.788Z","etag":null,"topics":["go","json","linter"],"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/breml.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":"2021-11-06T07:54:07.000Z","updated_at":"2025-03-12T19:31:32.000Z","dependencies_parsed_at":"2024-05-28T13:03:51.005Z","dependency_job_id":"ef9360c2-979d-443c-92b3-e79bb22873af","html_url":"https://github.com/breml/errchkjson","commit_stats":{"total_commits":50,"total_committers":7,"mean_commits":7.142857142857143,"dds":0.5,"last_synced_commit":"9ca157bc37516fa4da06e987592e4579cb008108"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breml%2Ferrchkjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breml%2Ferrchkjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breml%2Ferrchkjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/breml%2Ferrchkjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/breml","download_url":"https://codeload.github.com/breml/errchkjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247847608,"owners_count":21006099,"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":["go","json","linter"],"created_at":"2024-10-13T08:18:34.006Z","updated_at":"2025-10-06T19:05:03.434Z","avatar_url":"https://github.com/breml.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# errchkjson\n\n[![Test Status](https://github.com/breml/errchkjson/actions/workflows/ci.yml/badge.svg)](https://github.com/breml/errchkjson/actions/workflows/ci.yml) [![Go Report Card](https://goreportcard.com/badge/github.com/breml/errchkjson)](https://goreportcard.com/report/github.com/breml/errchkjson) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\nChecks types passed to the json encoding functions. Reports unsupported types and reports occurrences where the check for the returned error can be omitted.\n\nConsider this [http.Handler](https://pkg.go.dev/net/http#Handler):\n\n```Go\nfunc JSONHelloWorld(w http.ResponseWriter, r *http.Request) {\n\tresponse := struct {\n\t\tMessage string\n\t\tCode    int\n\t}{\n\t\tMessage: \"Hello World\",\n\t\tCode:    200,\n\t}\n\n\tbody, err := json.Marshal(response)\n\tif err != nil {\n\t\tpanic(err) // unreachable, because json encoding of a struct with just a string and an int will never return an error.\n\t}\n\n\tw.Write(body)\n}\n```\n\nBecause the `panic` is not possible to happen, one might refactor the code like this:\n\n```Go\nfunc JSONHelloWorld(w http.ResponseWriter, r *http.Request) {\n\tresponse := struct {\n\t\tMessage string\n\t\tCode    int\n\t}{\n\t\tMessage: \"Hello World\",\n\t\tCode:    200,\n\t}\n\n\tbody, _ := json.Marshal(response)\n\n\tw.Write(body)\n}\n```\n\nThis is ok, as long as the struct is not altered in such a way, that could potentially lead\nto `json.Marshal` returning an error.\n\n`errchkjson` allows you to lint your code such that the above error returned from `json.Marshal`\ncan be omitted while still staying safe, because as soon as an unsafe type is added to the\nresponse type, the linter will warn you.\n\n## Installation\n\nDownload `errchkjson` from the [releases](https://github.com/breml/errchkjson/releases) or get the latest version from source with:\n\n```shell\ngo install github.com/breml/errchkjson/cmd/errchkjson@latest\n```\n\n## Usage\n\n### Shell\n\nCheck everything:\n\n```shell\nerrchkjson ./...\n```\n\n`errchkjson` also recognizes the following command-line options:\n\nThe `-omit-safe` flag disables checking for safe returns of errors from json.Marshal\n\n## Types\n\n### Safe\n\nThe following types are safe to use with [json encoding functions](https://pkg.go.dev/encoding/json), that is, the encoding to JSON can not fail:\n\nSafe basic types:\n\n* `bool`\n* `int`, `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`, `uintptr`\n* `string`\n* Pointer type of the above listed basic types\n\nComposed types (struct, map, slice, array) are safe, if the type of the value is\nsafe. For structs, only exported fields are relevant. For maps, the key needs to be either an integer type or a string.\n\n### Unsafe\n\nThe following types are unsafe to use with [json encoding functions](https://pkg.go.dev/encoding/json), that is, the encoding to JSON can fail (return an error):\n\nUnsafe basic types:\n\n* `float32`, `float64`\n* `interface{}`\n* Pointer type of the above listed basic types\n\nAny composed types (struct, map, slice, array) containing an unsafe basic type.\n\nIf a type implements the `json.Marshaler` or `encoding.TextMarshaler` interface (e.g. `json.Number`).\n\n### Forbidden\n\nForbidden basic types:\n\n* `complex64`, `complex128`\n* `chan`\n* `func`\n* `unsafe.Pointer`\n\nAny composed types (struct, map, slice, array) containing a forbidden basic type. Any map\nusing a key with a forbidden type (`bool`, `float32`, `float64`, `struct`).\n\n## Accepted edge case\n\nFor `encoding/json.MarshalIndent`, there is a (pathological) edge case, where this\nfunction could [return an error](https://cs.opensource.google/go/go/+/refs/tags/go1.18:src/encoding/json/scanner.go;drc=refs%2Ftags%2Fgo1.18;l=181) for an otherwise safe argument, if the argument has\na nesting depth larger than [`10000`](https://cs.opensource.google/go/go/+/refs/tags/go1.18:src/encoding/json/scanner.go;drc=refs%2Ftags%2Fgo1.18;l=144) (as of Go 1.18).\n\n## Bugs found during development\n\nDuring the development of `errcheckjson`, the following issues in package `encoding/json` of the Go standard library have been found and PR have been merged:\n\n* [Issue #34154: encoding/json: string option (struct tag) on string field with SetEscapeHTML(false) escapes anyway](https://github.com/golang/go/issues/34154)\n* [PR #34127: encoding/json: fix and optimize marshal for quoted string](https://github.com/golang/go/pull/34127)\n* [Issue #34268: encoding/json: wrong encoding for json.Number field with string option (struct tag)](https://github.com/golang/go/issues/34268)\n* [PR #34269: encoding/json: make Number with the ,string option marshal with quotes](https://github.com/golang/go/pull/34269)\n* [PR #34272: encoding/json: validate strings when decoding into Number](https://github.com/golang/go/pull/34272)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbreml%2Ferrchkjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbreml%2Ferrchkjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbreml%2Ferrchkjson/lists"}