{"id":15041044,"url":"https://github.com/acrazing/cheapjson","last_synced_at":"2025-04-14T19:36:11.029Z","repository":{"id":57488305,"uuid":"97942691","full_name":"acrazing/cheapjson","owner":"acrazing","description":"A fast arbitrary JSON parser for golang","archived":false,"fork":false,"pushed_at":"2024-09-06T20:59:10.000Z","size":322,"stargazers_count":26,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-28T08:01:43.262Z","etag":null,"topics":["arbitrary-json","golang","json","simplejson"],"latest_commit_sha":null,"homepage":"http://godoc.org/github.com/acrazing/cheapjson","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/acrazing.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":"2017-07-21T11:51:31.000Z","updated_at":"2025-01-08T17:26:49.000Z","dependencies_parsed_at":"2024-09-25T01:32:51.693Z","dependency_job_id":null,"html_url":"https://github.com/acrazing/cheapjson","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acrazing%2Fcheapjson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acrazing%2Fcheapjson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acrazing%2Fcheapjson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/acrazing%2Fcheapjson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/acrazing","download_url":"https://codeload.github.com/acrazing/cheapjson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248946827,"owners_count":21187582,"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":["arbitrary-json","golang","json","simplejson"],"created_at":"2024-09-24T20:45:27.030Z","updated_at":"2025-04-14T19:36:11.009Z","avatar_url":"https://github.com/acrazing.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [Cheap JSON](https://godoc.org/github.com/acrazing/cheapjson) \u0026middot; [![GoDoc](https://godoc.org/github.com/acrazing/cheapjson?status.svg)](https://godoc.org/github.com/acrazing/cheapjson) [![Build Status](https://travis-ci.org/acrazing/cheapjson.svg?branch=master)](https://travis-ci.org/acrazing/cheapjson)\n\nA arbitrary JSON parser for golang.\n\n- **Standalone**: implement the parser independently for `ECMA-404 The JSON Data Interchange Standard`.\n- **Fast**: about two times faster than the package `go-simplejson` which use native `encoding/json` library.\n- **Lightweight**: only about 500 rows code for parser include UTF-16 pairs covert to UTF-8 bytes.\n\n## Install\n\n```bash\ngo get github.com/acrazing/cheapjson\n```\n\n## Usage Example\n\n```go\npackage main\n\nimport (\n  \"github.com/acrazing/cheapjson\"\n  \"encoding/json\"\n)\n\nfunc main()  {\n  // Unmarshal a bytes slice\n  value, err := cheapjson.Unmarshal([]byte(\"{\\\"hello\\\":\\\"world\\\", \\\"int\\\":12345}\"))\n  if err != nil {\n    panic(err)\n  }\n  // type check\n  if !value.IsObject() {\n    panic(\"parse error\")\n  }\n  // get a child field\n  str := value.Get(\"hello\")\n  \n  // get as string\n  println(str.String()) // world\n  \n  // get as int\n  println(value.Get(\"int\").Int()) // 12345\n  \n  // And any else you can do:\n  _ = value.Float() // returns float64\n  _ = value.Array() // returns []*Value\n  _ = value.Object() // returns map[string]*Value\n  \n  // WARNING: any of the upon value extract operate\n  // need to check the type at first as follow:\n  if value.IsObject() {\n    // value is a object, and then you can operate:\n    _ = value.Object()\n  }\n  // And there are more type checks\n  _ = value.IsObject()\n  _ = value.IsArray()\n  _ = value.IsNumber() // if is float or int, returns true\n  _ = value.IsInt() // just check is int\n  _ = value.IsTrue()\n  _ = value.IsFalse()\n  _ = value.IsBool()\n  _ = value.IsNull()\n  _ = value.IsString()\n  \n  // And you can manipulate a value\n  value = cheapjson.NewValue()\n  value.AsObject(nil) // set as a object\n  _ = value.AddField(\"hello\") // if a value is a object, you can call this, else will panic\n  value.AsArray(nil) // set as a array\n  elem := value.AddElement() // if a value is a array, yu can call this, else will panic\n  elem.AsInt(12) // as a int\n  elem.AsFloat(232)\n  elem.AsBool(true)\n  elem.AsNull()\n  \n  // And you can get a deep path by:\n  field := elem.Get(\"hello\", \"world\", \"deep\", \"3\")\n  _ = field.Value()\n  // Or set a deep path\n  // The different between Get and Ensure is that the Get\n  // just returns the exists field, if the path does not exist\n  // will return nil, and it will covert the path to integer\n  // if the node is an array, and the Ensure will force the\n  // path to be an object, and if the target path does not exist\n  // will auto generate it as a empty node.\n  value.Ensure(\"hello\", \"world\", \"deep\", \"3\").AsInt(3)\n  \n  // And you can dump a value to raw struct\n  data := value.Value()\n  // and this could be json marshal\n  _, _ = json.Marshal(data)\n}\n```\n\n## Benchmark\n\nSee [parser_test.go](./parser_test.go), compare with [go-simplejson](https://github.com/bitly/go-simplejson), which\nuse the native `encoding/json` library to unmarshal a json. The result is:\n\n- NormalInput(small): about 1.6 times faster\n- BigInput: about 4.4 times faster\n- DeepInput: about 7 times faster\n\n```bash\ngo test -bench=. -v ./parser_test.go\n\n# 2017/07/22 12:48:45 big input size: 92338772, normal input size: 763, deep input size: 33976002\n# === RUN   TestUnmarshal\n# --- PASS: TestUnmarshal (0.00s)\n# === RUN   TestSimpleJson\n# --- PASS: TestSimpleJson (1.62s)\n# BenchmarkUnmarshalBigInput-4                   5         358595392 ns/op\n# BenchmarkSimpleJsonBigInput-4                  1        1560047078 ns/op\n# BenchmarkUnmarshalNormalInput-4           200000              5372 ns/op\n# BenchmarkSimpleJsonNormalInput-4          200000              8593 ns/op\n# BenchmarkUnmarshalDeepInput-4                 30          42870590 ns/op\n# BenchmarkSimpleJsonDeepInput-4                 5         305351224 ns/op\n# PASS\n# ok      command-line-arguments  18.314s\n\n```\n\n## License\n\nMIT\n\n\n## TODO\n\n- [x] more unit test.\n- [ ] test the performance about make buffer before handle a string, (will walk the string twice).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facrazing%2Fcheapjson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Facrazing%2Fcheapjson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Facrazing%2Fcheapjson/lists"}