{"id":26826698,"url":"https://github.com/hslam/codec","last_synced_at":"2025-10-10T22:17:21.337Z","repository":{"id":55599189,"uuid":"220077655","full_name":"hslam/codec","owner":"hslam","description":"Package codec implements encoding and decoding of multiple codecs","archived":false,"fork":false,"pushed_at":"2024-03-13T21:12:28.000Z","size":91,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-21T03:16:23.248Z","etag":null,"topics":["codec","go","golang"],"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/hslam.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}},"created_at":"2019-11-06T19:50:59.000Z","updated_at":"2021-10-12T00:25:21.000Z","dependencies_parsed_at":"2022-08-15T04:01:08.208Z","dependency_job_id":null,"html_url":"https://github.com/hslam/codec","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/hslam%2Fcodec","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hslam%2Fcodec/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hslam%2Fcodec/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hslam%2Fcodec/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hslam","download_url":"https://codeload.github.com/hslam/codec/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246313614,"owners_count":20757446,"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":["codec","go","golang"],"created_at":"2025-03-30T11:30:36.747Z","updated_at":"2025-10-10T22:17:16.314Z","avatar_url":"https://github.com/hslam.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# codec\n[![GoDoc](https://godoc.org/github.com/hslam/codec?status.svg)](https://godoc.org/github.com/hslam/codec)\n[![Build Status](https://github.com/hslam/codec/workflows/build/badge.svg)](https://github.com/hslam/codec/actions)\n[![codecov](https://codecov.io/gh/hslam/codec/branch/master/graph/badge.svg)](https://codecov.io/gh/hslam/codec)\n[![Go Report Card](https://goreportcard.com/badge/github.com/hslam/codec)](https://goreportcard.com/report/github.com/hslam/codec)\n[![GitHub release](https://img.shields.io/github/release/hslam/codec.svg)](https://github.com/hslam/codec/releases/latest)\n[![LICENSE](https://img.shields.io/github/license/hslam/codec.svg?style=flat-square)](https://github.com/hslam/codec/blob/master/LICENSE)\n\nPackage codec implements encoding and decoding of multiple codecs\n\n## Feature\n* bytes\n* [code](https://github.com/hslam/code \"code\")\n* [gencode](https://github.com/andyleap/gencode \"gencode\")\n* [gogopb](https://github.com/gogo/protobuf \"gogopb\")\n* [msgp](https://github.com/tinylib/msgp \"msgp\")\n* [pb](github.com/golang/protobuf \"pb\")\n* json\n* xml\n* gob\n\n\n## Get started\n\n### Install\n```\ngo get github.com/hslam/codec\n```\n### Import\n```\nimport \"github.com/hslam/codec\"\n```\n### Usage\n#### Example\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"github.com/hslam/codec\"\n\t\"github.com/hslam/codec/example/code\"\n\t\"github.com/hslam/codec/example/codepb\"\n\t\"github.com/hslam/codec/example/gencode\"\n\t\"github.com/hslam/codec/example/gogopb\"\n\t\"github.com/hslam/codec/example/model\"\n\t\"github.com/hslam/codec/example/msgp\"\n\t\"github.com/hslam/codec/example/pb\"\n\tcodecpb \"github.com/hslam/codec/pb\"\n)\n\nfunc main() {\n\tBYTES()\n\tCODE()\n\tGENCODE()\n\tCODEPB()\n\tMSGP()\n\tGOGOPB()\n\tPB()\n\tJSON()\n\tXML()\n\tGOB()\n}\n\n//BYTES Example\nfunc BYTES() {\n\tvar obj = []byte{128, 8, 128, 8, 195, 245, 72, 64, 74, 216, 18, 77, 251, 33, 9, 64, 10, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100, 1, 1, 255, 2, 1, 128, 1, 255}\n\tc := \u0026codec.BYTESCodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"bytes Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy []byte\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"bytes Decode：\", objCopy)\n}\n\n//CODE Example\nfunc CODE() {\n\tvar obj = code.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}\n\tc := \u0026codec.CODECodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"code Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy code.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"code Decode：\", objCopy)\n}\n\n//GENCODE Example\nfunc GENCODE() {\n\tvar obj = gencode.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}\n\tc := \u0026codec.CODECodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"gencode Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy gencode.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"gencode Decode：\", objCopy)\n}\n\n//CODEPB Example\nfunc CODEPB() {\n\tvar obj = codepb.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}\n\tc := \u0026codec.CODECodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"codepb Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy codepb.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"codepb Decode：\", objCopy)\n}\n\n//MSGP Example\nfunc MSGP() {\n\tvar obj = msgp.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}\n\tc := \u0026codec.MSGPCodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"msgp Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy msgp.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"msgp Decode：\", objCopy)\n}\n\n//GOGOPB Example\nfunc GOGOPB() {\n\tvar obj = gogopb.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}\n\tc := \u0026codec.GOGOPBCodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"gogopb Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy gogopb.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"gogopb Decode：\", objCopy)\n}\n\n//PB Example\nfunc PB() {\n\tvar obj = pb.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}\n\tc := \u0026codecpb.Codec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"pb Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy pb.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"pb Decode：\", objCopy)\n}\n\n//JSON Example\nfunc JSON() {\n\tvar obj = model.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}\n\tc := \u0026codec.JSONCodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"json Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy model.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"json Decode：\", objCopy)\n}\n\n//XML Example\nfunc XML() {\n\tvar obj = model.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true}\n\tc := \u0026codec.XMLCodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"xml Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy model.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"xml Decode：\", objCopy)\n}\n\n//GOB Example\nfunc GOB() {\n\tvar obj = model.Object{A: 1024, B: 1024, C: 3.14, D: 3.1415926, E: \"HelloWorld\", F: true, G: []byte{255}, H: [][]byte{{128}, {255}}}\n\tc := \u0026codec.GOBCodec{}\n\tvar buf = make([]byte, 512)\n\tdata, _ := c.Marshal(buf, \u0026obj)\n\tfmt.Printf(\"gob Encode：length-%d,hex-%x\\n\", len(data), data)\n\tvar objCopy model.Object\n\tc.Unmarshal(data, \u0026objCopy)\n\tfmt.Println(\"gob Decode：\", objCopy)\n}\n```\n\n### Output\n```\nbytes Encode：length-35,hex-80088008c3f548404ad8124dfb2109400a48656c6c6f576f726c640101ff02018001ff\nbytes Decode： [128 8 128 8 195 245 72 64 74 216 18 77 251 33 9 64 10 72 101 108 108 111 87 111 114 108 100 1 1 255 2 1 128 1 255]\ncode Encode：length-35,hex-80088008c3f548404ad8124dfb2109400a48656c6c6f576f726c640101ff02018001ff\ncode Decode： {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}\ngencode Encode：length-43,hex-000400000004000000000000c3f548404ad8124dfb2109400a48656c6c6f576f726c640101ff02018001ff\ngencode Decode： {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}\ncodepb Encode：length-43,hex-0880081080081dc3f54840214ad8124dfb2109402a0a48656c6c6f576f726c6430013a01ff4201804201ff\ncodepb Decode： {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}\nmsgp Encode：length-59,hex-88a141cd0400a142cd0400a143ca4048f5c3a144cb400921fb4d12d84aa145aa48656c6c6f576f726c64a146c3a147c401ffa14892c40180c401ff\nmsgp Decode： {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}\ngogopb Encode：length-43,hex-0880081080081dc3f54840214ad8124dfb2109402a0a48656c6c6f576f726c6430013a01ff4201804201ff\ngogopb Decode： {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}\npb Encode：length-43,hex-0880081080081dc3f54840214ad8124dfb2109402a0a48656c6c6f576f726c6430013a01ff4201804201ff\npb Decode： {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]] {} [] 0}\njson Encode：length-99,hex-7b2241223a313032342c2242223a313032342c2243223a332e31342c2244223a332e313431353932362c2245223a2248656c6c6f576f726c64222c2246223a747275652c2247223a222f773d3d222c2248223a5b2267413d3d222c222f773d3d225d7d\njson Decode： {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}\nxml Encode：length-94,hex-3c4f626a6563743e3c413e313032343c2f413e3c423e313032343c2f423e3c433e332e31343c2f433e3c443e332e313431353932363c2f443e3c453e48656c6c6f576f726c643c2f453e3c463e747275653c2f463e3c2f4f626a6563743e\nxml Decode： {1024 1024 3.14 3.1415926 HelloWorld true [] []}\ngob Encode：length-146,hex-45ff81030101064f626a65637401ff82000108010141010600010142010600010143010800010144010800010145010c00010146010200010147010a0001014801ff8400000017ff83020101095b5d5b5d75696e743801ff8400010a000033ff8201fe040001fe040001fb60b81e094001f84ad8124dfb210940010a48656c6c6f576f726c6401010101ff0102018001ff00\ngob Decode： {1024 1024 3.14 3.1415926 HelloWorld true [255] [[128] [255]]}\n```\n\n### Benchmark\ngo test -v -run=\"none\" -bench=. -benchtime=1s\n```\ngoos: darwin\ngoarch: amd64\npkg: github.com/hslam/codec\nBenchmarkMarshalBYTES-4       \t1000000000\t         0.394 ns/op\nBenchmarkMarshalCODE-4        \t18543157\t        63.9 ns/op\nBenchmarkMarshalGENCODE-4     \t22040894\t        53.5 ns/op\nBenchmarkMarshalCODEPB-4      \t17600244\t        66.0 ns/op\nBenchmarkMarshalMSGP-4        \t13804621\t        86.7 ns/op\nBenchmarkMarshalGOGOPB-4      \t11963642\t        98.8 ns/op\nBenchmarkMarshalPB-4          \t 3721976\t       311 ns/op\nBenchmarkMarshalJSON-4        \t 1268824\t       947 ns/op\nBenchmarkMarshalXML-4         \t  289130\t      4086 ns/op\nBenchmarkMarshalGOB-4         \t  205197\t      5849 ns/op\nBenchmarkUnmarshalBYTES-4     \t1000000000\t         0.960 ns/op\nBenchmarkUnmarshalCODE-4      \t23269940\t        50.6 ns/op\nBenchmarkUnmarshalGENCODE-4   \t16862955\t        69.9 ns/op\nBenchmarkUnmarshalCODEPB-4    \t16749501\t        70.0 ns/op\nBenchmarkUnmarshalMSGP-4      \t 3930744\t       305 ns/op\nBenchmarkUnmarshalGOGOPB-4    \t 2558491\t       445 ns/op\nBenchmarkUnmarshalPB-4        \t 2313656\t       516 ns/op\nBenchmarkUnmarshalJSON-4      \t  418756\t      2880 ns/op\nBenchmarkUnmarshalXML-4       \t  143637\t      8256 ns/op\nBenchmarkUnmarshalGOB-4       \t   47180\t     25421 ns/op\nBenchmarkRoundtripBYTES-4     \t1000000000\t         1.10 ns/op\nBenchmarkRoundtripCODE-4      \t10084428\t       117 ns/op\nBenchmarkRoundtripGENCODE-4   \t 9428412\t       126 ns/op\nBenchmarkRoundtripCODEPB-4    \t 8580951\t       139 ns/op\nBenchmarkRoundtripMSGP-4      \t 2873107\t       416 ns/op\nBenchmarkRoundtripGOGOPB-4    \t 2705097\t       474 ns/op\nBenchmarkRoundtripPB-4        \t 1378863\t       868 ns/op\nBenchmarkRoundtripJSON-4      \t  291931\t      4071 ns/op\nBenchmarkRoundtripXML-4       \t   93944\t     12529 ns/op\nBenchmarkRoundtripGOB-4       \t   37376\t     32642 ns/op\nPASS\nok  \tgithub.com/hslam/codec\t42.864s\n```\n\n### License\nThis package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)\n\n### Author\ncodec was written by Meng Huang.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhslam%2Fcodec","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhslam%2Fcodec","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhslam%2Fcodec/lists"}