Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ake-persson/encoding
Go package provides a generic interface to encoders and decoders
https://github.com/ake-persson/encoding
decoding encoding go
Last synced: 2 months ago
JSON representation
Go package provides a generic interface to encoders and decoders
- Host: GitHub
- URL: https://github.com/ake-persson/encoding
- Owner: ake-persson
- License: apache-2.0
- Created: 2018-04-06T20:48:00.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-12T13:29:42.000Z (about 5 years ago)
- Last Synced: 2024-04-22T22:16:09.070Z (8 months ago)
- Topics: decoding, encoding, go
- Language: Go
- Homepage:
- Size: 1.54 MB
- Stars: 10
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - encoding - 04-06T20:48:00Z|2019-11-12T13:29:42Z| (Bot Building / Parsers/Encoders/Decoders)
README
[![GoDoc](https://godoc.org/github.com/mickep76/encoding?status.svg)](https://godoc.org/github.com/mickep76/encoding)
[![codecov](https://codecov.io/gh/mickep76/encoding/branch/master/graph/badge.svg)](https://codecov.io/gh/mickep76/encoding)
[![Build Status](https://travis-ci.org/mickep76/encoding.svg?branch=master)](https://travis-ci.org/mickep76/encoding)
[![Go Report Card](https://goreportcard.com/badge/github.com/mickep76/encoding)](https://goreportcard.com/report/github.com/mickep76/encoding)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/mickep76/mlfmt/blob/master/LICENSE)
[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go#text-processing)# encoding
Package provides a generic interface to encoders and decoders
## Example
```go
package main
import (
"flag"
"fmt"
"log"
"strings""github.com/mickep76/encoding"
_ "github.com/mickep76/encoding/json"
_ "github.com/mickep76/encoding/toml"
_ "github.com/mickep76/encoding/yaml"
)type Message struct {
Name, Text string
}type Messages struct {
Messages []*Message
}func main() {
codec := flag.String("codec", "json", fmt.Sprintf("Codecs: [%s].", strings.Join(encoding.Codecs(), ", ")))
indent := flag.String("indent", "", "Indent encoding (only supported by JSON codec)")
flag.Parse()in := Messages{
Messages: []*Message{
&Message{Name: "Ed", Text: "Knock knock."},
&Message{Name: "Sam", Text: "Who's there?"},
&Message{Name: "Ed", Text: "Go fmt."},
&Message{Name: "Sam", Text: "Go fmt who?"},
&Message{Name: "Ed", Text: "Go fmt yourself!"},
},
}var opts []encoding.Option
if *indent != "" {
opts = append(opts, encoding.WithIndent(*indent))
}
c, err := encoding.NewCodec(*codec, opts...)
if err != nil {
log.Fatal(err)
}b, err := c.Encode(in)
if err != nil {
log.Fatal(err)
}fmt.Printf("Codec: %s\n", *codec)
fmt.Printf("Encoded:\n%s\n", string(b))out := Messages{}
if err := c.Decode(b, &out); err != nil {
log.Fatal(err)
}fmt.Println("Decoded:")
for _, m := range out.Messages {
fmt.Printf("%s: %s\n", m.Name, m.Text)
}
}
```