{"id":18621283,"url":"https://github.com/sofastack/sofa-hessian-go","last_synced_at":"2026-01-08T04:32:11.628Z","repository":{"id":51250945,"uuid":"497770020","full_name":"sofastack/sofa-hessian-go","owner":"sofastack","description":null,"archived":false,"fork":false,"pushed_at":"2023-12-25T00:40:47.000Z","size":192,"stargazers_count":1,"open_issues_count":1,"forks_count":2,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-03T10:46:05.388Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sofastack.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":"2022-05-30T03:00:48.000Z","updated_at":"2024-04-22T16:15:45.000Z","dependencies_parsed_at":"2025-02-03T10:42:42.803Z","dependency_job_id":"a7b57c93-3260-45f3-8fc3-803c424b7124","html_url":"https://github.com/sofastack/sofa-hessian-go","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofastack%2Fsofa-hessian-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofastack%2Fsofa-hessian-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofastack%2Fsofa-hessian-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sofastack%2Fsofa-hessian-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sofastack","download_url":"https://codeload.github.com/sofastack/sofa-hessian-go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246122239,"owners_count":20726822,"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-11-07T04:10:10.326Z","updated_at":"2026-01-08T04:32:11.589Z","avatar_url":"https://github.com/sofastack.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 目录\n\n-   [概要](#概要)\n-   [类型系统](#类型系统)\n    -   [基础类型](#基础类型)\n    -   [复合类型](#复合类型)\n    -   [引用类型](#引用类型)\n-   [编码规范](#编码规范)\n-   [解码规范](#解码规范)\n-   [JSON 转换](#json转换)\n-   [API](#api)\n    -   [encode](#encode)\n    -   [decode](#decode)\n    -   [json](#json)\n-   [CLI](#cli)\n    -   [install](#install)\n    -   [decode](#decode)\n    -   [fromjson](#fromjson)\n-   [性能测试](#性能测试)\n-   [TODO](#todo)\n\n# 概要\n\n`sofa-hessian-go` 是 [hessian 2.0/1.0 serialization protocol](http://hessian.caucho.com/doc/hessian-serialization.html) 的 Golang 实现，包括 1.0 协议以及 2.0 协议的 `java3.x` 和 `java4.x` 版本，同时还提供了 [JSON](https://json.org) 到 [hessian](http://hessian.caucho.com/doc/hessian-serialization.html) 类型系统互相转换的设计。\n\n在此非常感谢 [node-modules/hessian.js](https://github.com/node-modules/hessian.js) 提供的 golden files(测试数据), 两者底层复用了同样的测试数据集。\n\n# 类型系统\n\nhessian 的类型系统由 8 种基础类型和 3 种复合类型，以及 3 种 引用类型组成。\n\n## 基础类型\n\n-   binary\n-   bool\n-   string\n-   int32\n-   int64\n-   float64\n-   null\n-   date (64bit)\n\n## 复合类型\n\n-   list\n-   map\n-   object\n\n## 引用类型\n\n-   class reference: represents the definition of class\n-   type reference: represents the name of class\n-   object reference: represents the instance of object or list or map.\n\n# 编码规范\n\nsofa-hessian-go 遵循以下编码规范，将 go 的类型转化为 hessian 的类型。\n\n-   uint8/int8 =\u003e int32\n-   uint16/int =\u003e int32\n-   uint32/int32 =\u003e int32\n-   uint64/int64 =\u003e int64\n-   uint/int =\u003e int64\n-   bool =\u003e bool\n-   string =\u003e string\n-   []byte =\u003e binary\n-   nil =\u003e null\n-   time.Time =\u003e date\n-   map[interface{}]interface{} =\u003e map\n-   []interface{} =\u003e list\n-   struct =\u003e object\n\n# 解码规范\n\nsofa-hessian-go 遵循以下解码规范，将 hessian 的类型转化为 go 的类型。\n\n-   int32 =\u003e int32\n-   int64 =\u003e int64\n-   bool =\u003e bool\n-   string =\u003e string\n-   null =\u003e nil\n-   date =\u003e time.Time\n-   map\n\n    -   untyped map =\u003e map[interface{}]interface{}\n    -   typed map =\u003e \\*JavaMap{class: \"balaba\", map[interface{}]interface{}}\n\n-   list\n\n    -   untyped list =\u003e []interface{}\n    -   typed list =\u003e \\*JavaList{class: \"balaba\", []interface{}}\n\n-   object =\u003e\n    -   concrete object =\u003e go concrete struct\n    -   generic object =\u003e \\*JavaObject{class: \"balaba\", JavaObject{}}\n\n# json 转换\n\nJSON 的类型系统比 HESSIAN 的类型系统更精简，从理论上是可以在一定人为约束条件下做到 JSON 和 HESSIAN 的类型转换。\n\n## JSON 到 HESSIAN 的类型转换\n\n给定一个 HESSIAN 类型总是存在一种 JSON 类型可以等价描述出来。但是在实际实现中只有一种情况例外，即 HESSIAN 可以以较小的代价描述循环引用的数据结构，JSON 虽然可以描述，但通常导致的结果就是无限递归导致爆栈。 JSON 天然无法处理循环引用的数据结构，不过 sofa-hessian-go 在实际的实现中以较小的代价跟踪循环引用的问题，以不完整的数据结构代替了爆栈。\n\n### 基础类型\n\n#### NULL 类型\n\n给定一个 hessian null 类型总是可以用 json null 类型描述即 `json.null =\u003e hessian.bool`\n\n#### bool 类型\n\n给定一个 hessian bool 类型总是可以用 json bool 类型描述即 `json.bool =\u003e hessian.bool`\n\n#### int 类型\n\n给定一个 hessian int 类型无法直接用 json number 类型描述，但是我们可以通过 json object 包装来表示即\n\n```\njson.object {\n\t\"$class\": \"int\", // or \"java.lang.Integer\"\n\t\"$\": number\n} =\u003e hessian.int\n```\n\n#### long 类型\n\n给定一个 hessian long 类型总是可以用 json number 类型描述即 `json.number =\u003e hessian.long`\n\n#### double 类型\n\n给定一个 hessian double 类型总是可以用 json object 类型描述即\n\n```\njson.object {\n\t\"$class\": \"double\", // or \"java.lang.Double\"\n\t\"$\": number\n} =\u003e hessian.double\n```\n\n#### binary 类型\n\n给定一个 hessian binary 类型总是可以用 json object 类型描述即\n\n```\njson.object {\n\t\"$class\": \"bytes\",\n\t\"$\": \"base64 encoding string\"\n} =\u003e hessian.binary\n```\n\n#### date 类型\n\n给定一个 hessian date 类型总是可以用 json object 类型描述即\n\n```\njson.object {\n\t\"$class\": \"date\", // or \"java.util.Date\"\n\t\"$\": number\n} =\u003e hessian.date\n```\n\n### 复合类型\n\n#### map 类型\n\n给定一个 hessian map 类型总是可以用 json object 类型描述即 `json.object =\u003e hessian.map`\n\n#### list 类型\n\n给定一个 hessian map 类型总是可以用 json array 类型描述即 `json.array =\u003e hessian.list`\n\n#### object 类型\n\n给定一个 hessian object 类型总是可以用 json object 类型描述即\n\n```\njson.object {\n\t\"$class\": \"classname\",\n\t\"$\": json element\n} =\u003e hessian.object\n```\n\n## HESSIAN 到 JSON 的类型转换\n\n给定一个 JSON 类型总是存在一种 HESSIAN 类型可以等价描述出来.\n\n### 基础类型\n\n#### number\n\n给定一个 json number 类型总是可以用 hessian int/long/double 类型描述即 `hessian.int|long/double =\u003e json.number`\n\n#### string\n\n给定一个 json string 类型总是可以用 hessian string 类型描述即 `hessian.string =\u003e json.string`\n\n#### bool\n\n给定一个 json bool 类型总是可以用 hessian bool 类型描述即 `hessian.bool =\u003e json.bool`\n\n#### null\n\n给定一个 json null 类型总是可以用 hessian null 类型描述即 `hessian.null =\u003e json.null`\n\n### 复合类型\n\n#### object\n\n给定一个 json object 类型总是可以用 hessian map 类型描述即 `hessian.map =\u003e json.object`\n\n#### array\n\n给定一个 json array 类型总是可以用 hessian array 类型描述即 `hessian.array =\u003e json.array`\n\n## 附录\n\n### JSON 类型系统\n\n```\njson\n    element\n\nvalue\n    object\n    array\n    string\n    number\n    \"true\"\n    \"false\"\n    \"null\"\n\n```\n\n### HESSIAN 类型系统\n\n```\n#starting production\ntop        ::= value\n\n#main production\nvalue      ::= null\n           ::= binary\n           ::= boolean\n           ::= class-def value\n           ::= date\n           ::= double\n           ::= int\n           ::= list\n           ::= long\n           ::= map\n           ::= object\n           ::= ref\n           ::= string\n```\n\n# API\n\n## encode\n\n查看 [examples/sofahessian_examples_test.go](/examples/sofahessian_examples_test.go#L13)\n\n## decode\n\n查看 [examples/sofahessian_examples_test.go](/examples/sofahessian_examples_test.go#L14)\n\n## json\n\n查看 [examples/sofahessian_examples_test.go](/examples/sofahessian_examples_test.go#L68)\n\n# CLI\n\n## install\n\nmake hessian\n\n## decode\n\n```bash\nbin/hessian decode 4fbc636f6d2e616c697061792e736f66612e7270632e636f72652e726571756573742e536f666152657175657374950d7461726765744170704e616d650a6d6574686f644e616d651774617267657453657276696365556e697175654e616d650c7265717565737450726f70730d6d6574686f64417267536967736f904e0873617948656c6c6f1048656c6c6f536572766963653a312e304d0870726f746f636f6c04626f6c74117270635f74726163655f636f6e746578744d09736f6661527063496401300473616d700566616c73650b73797350656e4174747273000d736f666143616c6c6572496463000c736f666143616c6c65724970000b736f6661547261636549641e3061306665383633313537313034363337383735383130303138363232300c736f666150656e4174747273000e736f666143616c6c65725a6f6e65000d736f666143616c6c6572417070007a7a567400075b737472696e676e02106a6176612e6c616e672e537472696e67046c6f6e677a05776f726c64e1 --version=3\n\n\u0026hessian.JavaObject{\n  class: \"com.alipay.sofa.rpc.core.request.SofaRequest\",\n  names: []string{\n    \"targetAppName\",\n    \"methodName\",\n    \"targetServiceUniqueName\",\n    \"requestProps\",\n    \"methodArgSigs\",\n  },\n  values: []interface {}{\n    nil,\n    \"sayHello\",\n    \"HelloService:1.0\",\n    map[interface {}]interface {}{\n      \"protocol\":          \"bolt\",\n      \"rpc_trace_context\": map[interface {}]interface {}{\n        \"sofaCallerZone\": \"\",\n        \"sofaCallerApp\":  \"\",\n        \"sofaRpcId\":      \"0\",\n        \"sysPenAttrs\":    \"\",\n        \"sofaCallerIdc\":  \"\",\n        \"sofaPenAttrs\":   \"\",\n        \"samp\":           \"false\",\n        \"sofaCallerIp\":   \"\",\n        \"sofaTraceId\":    \"0a0fe8631571046378758100186220\",\n      },\n    },\n    \u0026hessian.JavaList{\n      class: \"[string\",\n      value: []interface {}{\n        \"java.lang.String\",\n        \"long\",\n      },\n    },\n  },\n}\n\"world\"\n1\n```\n\n## fromjson\n\n```bash\nbin/hessian fromjson '{\"$class\": \"com.alipay.sofa.rpc.core.request.SofaRequest\", \"$\": {\"1\": \"2\"}}' --format hex\n\n43302c636f6d2e616c697061792e736f66612e7270632e636f72652e726571756573742e536f666152657175657374910131600132\n\nbin/hessian decode 43302c636f6d2e616c697061792e736f66612e7270632e636f72652e726571756573742e536f666152657175657374910131600132\n\u0026sofahessian.JavaObject{\n  class: \"com.alipay.sofa.rpc.core.request.SofaRequest\",\n  names: []string{\n    \"1\",\n  },\n  values: []interface {}{\n    \"2\",\n  },\n}\n```\n\n# 性能测试\n\n```\n─λ make bench\ngo test -benchmem -run=\"^$\" -bench ^Benchmark ./...\n?   \tgithub.com/sofastack/sofa-hessian-go/sofahessianv1\t[no test files]\ngoos: darwin\ngoarch: amd64\npkg: github.com/sofastack/sofa-hessian-go/sofahessianv2\nBenchmarkDecodeBinary-8             \t  558362\t      2180 ns/op\t15041.85 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkDecodeBool-8               \t33203718\t        35.8 ns/op\t  27.91 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkDecodeDate-8               \t14283891\t        73.1 ns/op\t 123.15 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkDecodeFloat64-8            \t15707289\t        74.1 ns/op\t  40.49 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkDecodeInt32-8              \t15519128\t        82.9 ns/op\t  60.31 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkDecodeInt64-8              \t15663241\t        78.2 ns/op\t 115.09 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkDecodeList-8               \t  751725\t      2969 ns/op\t   6.06 MB/s\t     478 B/op\t      19 allocs/op\nBenchmarkDecodeMap-8                \t  217478\t      5185 ns/op\t  14.08 MB/s\t     949 B/op\t      30 allocs/op\nBenchmarkDecodeNil-8                \t27432326\t        42.6 ns/op\t  23.46 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkDecodeObject-8             \t  965149\t      1498 ns/op\t  22.03 MB/s\t     548 B/op\t      13 allocs/op\nBenchmarkDecodeString-8             \t     818\t   1333529 ns/op\t 147.44 MB/s\t    1370 B/op\t       0 allocs/op\nBenchmarkEncodeBinary-8             \t  392306\t      2989 ns/op\t21944.34 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkEncodeBool-8               \t143134738\t         8.50 ns/op\t 117.64 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkEncodeFloat64-8            \t100000000\t        11.0 ns/op\t 819.48 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkEncodeInt64-8              \t100000000\t        10.2 ns/op\t 490.08 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkEncodeInt32-8              \t100000000\t        10.7 ns/op\t 465.65 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkEncodeList-8               \t 7413168\t       161 ns/op\t  12.45 MB/s\t      40 B/op\t       2 allocs/op\nBenchmarkEncodeMap-8                \t13931235\t        82.9 ns/op\t  24.12 MB/s\t       8 B/op\t       1 allocs/op\nBenchmarkEncodeNil-8                \t197458849\t         6.12 ns/op\t 163.33 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkEncodeObject-8             \t 3964602\t       303 ns/op\t  46.15 MB/s\t      64 B/op\t       3 allocs/op\nBenchmarkEncodeRef-8                \t88544038\t        13.5 ns/op\t 222.70 MB/s\t       0 B/op\t       0 allocs/op\nBenchmarkEncodeString-8             \t     716\t   1662180 ns/op\t 160.43 MB/s\t      16 B/op\t       1 allocs/op\nBenchmarkEncodeAndDecodeJSON-8      \t   51471\t     22695 ns/op\t  44.68 MB/s\t    2913 B/op\t      37 allocs/op\nBenchmarkEncodeAndDecodeHessianV2-8   \t  139880\t      7979 ns/op\t  56.77 MB/s\t    3104 B/op\t      57 allocs/op\nPASS\nok  \tgithub.com/sofastack/sofa-hessian-go/sofahessianv2\t34.443s\n?   \tgithub.com/sofastack/sofa-hessian-go/javaobject\t[no test files]\n```\n\n# TODO\n\n1. hessian-generator: 通过编译时代码生成 Encode 和 Decode 方法，减少反射带来的负担。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsofastack%2Fsofa-hessian-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsofastack%2Fsofa-hessian-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsofastack%2Fsofa-hessian-go/lists"}