{"id":21152688,"url":"https://github.com/darjun/json-gen","last_synced_at":"2025-07-09T10:33:03.271Z","repository":{"id":108932960,"uuid":"213635989","full_name":"darjun/json-gen","owner":"darjun","description":"generate json string by one memory allocation","archived":false,"fork":false,"pushed_at":"2019-10-09T03:25:11.000Z","size":7,"stargazers_count":28,"open_issues_count":0,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-06-19T05:38:02.365Z","etag":null,"topics":["go","json"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/darjun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-10-08T12:28:03.000Z","updated_at":"2024-03-05T06:44:53.000Z","dependencies_parsed_at":"2023-03-22T06:35:26.764Z","dependency_job_id":null,"html_url":"https://github.com/darjun/json-gen","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/darjun%2Fjson-gen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darjun%2Fjson-gen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darjun%2Fjson-gen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/darjun%2Fjson-gen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/darjun","download_url":"https://codeload.github.com/darjun/json-gen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225534164,"owners_count":17484459,"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"],"created_at":"2024-11-20T10:45:36.866Z","updated_at":"2024-11-20T10:45:37.463Z","avatar_url":"https://github.com/darjun.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-gen\n\n[![Build Status](https://travis-ci.org/darjun/json-gen.svg?branch=master)](https://travis-ci.org/darjun/json-gen)\n\n## 起源\n\n游戏服务端的很多操作（包括玩家的和非玩家的）需要传给公司中台收集汇总，根据运营的需求分析数据。中台那边要求传过去的数据为 JSON 格式。开始使用 Golang 标准库中的`encoding/json`，发现性能不够理想（因为序列化使用了反射，涉及多次内存分配）。由于数据原始格式都是`map[string]interface{}`，且需要自己一个字段一个字段构造，于是我想可以在构造过程中就计算出最终 JSON 串的长度，那么就只需要一次内存分配了。\n\n## 使用\n\n下载：\n\n```\n$ go get github.com/darjun/json-gen\n```\n\n引入：\n\n```\nimport (\n  jsongen \"github.com/darjun/json-gen\"\n)\n```\n\n构造数组或映射：\n\n```\n// 数组\na := jsongen.NewArray()\na.AppendUint(123)\na.AppendInt(-456)\ndata := a.Serialize(nil)\n// string(data) == \"[123,-456]\"\n\n// 映射\nm := jsongen.NewMap()\nm.PutUint(\"uintkey\", 123)\nm.PutInt(\"intkey\", -456)\ndata := m.Serialize(nil)\n// string(data) == `{\"uintkey\":123,\"intkey\":-456}`\n```\n\n当然类型可以无限嵌套：\n\n```\nsubm := jsongen.NewMap()\nsubm.PutString(\"stringkey\", \"test string\")\n\nm := jsongen.NewMap()\nm.PutUint(\"uintkey\", 123)\nm.PutUintArray(\"uintarray\", []uint64{123,456,789})\nm.PutMap(\"subm\", subm)\ndata := m.Serialize(nil)\n// string(data) == `{\"uintkey\":123,\"uintarray\":[123,456,789],\"subm\":{\"stringkey\":\"test string\"}}`\n```\n\n## Benchmark\n\n| Library | Time/op(ns) |   B/op   | allocs/op |\n|---------|---------|----------|-----------|\n| encoding/json | 22209 | 6673 | 127 |\n| darjun/json-gen | 3300 | 1152 | 1 |\n\n通常情况下，`json-gen`生成 JSON 串的性能是标准 JSON 库的**10**。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarjun%2Fjson-gen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdarjun%2Fjson-gen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdarjun%2Fjson-gen/lists"}