An open API service providing package, version and dependency metadata of many open source software ecosystems and registries.

github.com/jba/codec

Package codec implements an encoder for Go values. It relies on code generation rather than reflection, so it is significantly faster than reflection-based encoders like gob. It can also preserve sharing among pointers (but not other forms of sharing, like sub-slices). Encodings with maps are not deterministic, due to the non-deterministic order of map iteration. The package supports Go built-in types (int, string and so on) out of the box, but for any other type you must generate code by calling GenerateFile. This can be done with a small program in your project's directory: Code will be generated for each type listed and for all types they contain. So this program will generate code for []mypkg.Type1, mypkg.Type1, *mypkg.Type2, and mypkg.Type2. The "//+build ignore" tag prevents the program from being compiled as part of your package. Instead, invoke it directly with "go run". Use "go generate" to do so if you like: On subsequent runs, the generator reads the generated file to get the names and order of all struct fields. It uses this information to generate correct code when fields are moved or added. Make sure the old generated files remain available to the generator, or changes to your structs may result in existing encoded data being decoded incorrectly. Create an Encoder by passing it an io.Writer: Then use it to encode one or more values: To decode, pass an io.Reader to NewDecoder, and call Decode: By default, if two pointers point to the same value, that value will be duplicated upon decoding. If there is a cycle, where a value directly or indirectly points to itself, then the encoder will crash by exceeding available stack space. This is the same behavior as encoding/gob and many other encoders. Set EncodeOptions.TrackPointers to true to preserve pointer sharing and cycles, at the cost of slower encoding. Other forms of memory sharing are not preserved. For example, if two slices refer to the same underlying array during encoding, they will refer to separate arrays after decoding. Struct tags in the style of encoding/json are supported, under the name "codec". You can easily generate code for structs designed for the encoding/json package by changing the name to "json" in an option to GenerateFile. An example: Here, field A will use the name "B" and field C will be omitted. There is no need for the omitempty option because the encoder always omits zero values. Since the encoding uses numbers for fields instead of names, renaming a field doesn't actually affect the encoding. It does matter if subsequent changes are made to the struct, however. For example, say that originally T was but you rename the field to "B": The generator will treat "B" as a new field. Data encoded with "A" will not be decoded into "B". So you should use a tag to express that it is a renaming:

Ecosystem
proxy.golang.org
Latest Release
v0.4.1
about 5 years ago
Versions
6
Dependent Repos
1
Links
Registry proxy.golang.org
Source Repository
Docs Documentation
JSON API View JSON
CodeMeta codemeta.json
Package Details
PURL pkg:golang/github.com/jba/codec
spec
License BSD-3-Clause
Namespace github.com/jba
First Release about 5 years ago
Last Synced 3 days ago
Repository
Stars 11 on GitHub
Forks 0 on GitHub
Rankings on proxy.golang.org
Dependent repos Top 4.7%