Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/picatz/cbor
🌀 Concise Binary Object Representation (CBOR)
https://github.com/picatz/cbor
cbor decoder encoder golang
Last synced: 12 days ago
JSON representation
🌀 Concise Binary Object Representation (CBOR)
- Host: GitHub
- URL: https://github.com/picatz/cbor
- Owner: picatz
- License: mpl-2.0
- Created: 2023-01-01T01:03:23.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-02T18:01:23.000Z (about 2 years ago)
- Last Synced: 2024-11-10T01:42:39.907Z (2 months ago)
- Topics: cbor, decoder, encoder, golang
- Language: Go
- Homepage: https://pkg.go.dev/github.com/picatz/cbor
- Size: 56.6 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CBOR
[Concise Binary Object Representation](https://www.rfc-editor.org/rfc/rfc8949.html) (CBOR) is a schemaless data format
designed to be small and extensible. This package implements a minimal CBOR encoder and decoder in a similar style to
the [`encoding/json`](https://pkg.go.dev/encoding/json) package.> **Warning**: this is a work in progress, mostly for learning purposes. See [`github.com/fxamacker/cbor`](https://github.com/fxamacker/cbor) for a more robust implementation.
```go
package mainimport (
"bytes"
"fmt""github.com/picatz/cbor"
)// This code shows how to encode and decode the CBOR data format.
func main() {
// CBOR encoding of the value {"hello": "world"}.
const data = "\xA1\x65\x68\x65\x6C\x6C\x6F\x65\x77\x6F\x72\x6C\x64"// Create a new cbor.Decoder using bytes.NewBufferString(data) as its source,
// and then Decode the CBOR data into the value map[string]string.
var value map[string]string
err := cbor.NewDecoder(bytes.NewBufferString(data)).Decode(&value)
if err != nil {
panic(err)
}// Output: world
fmt.Println(value["hello"])// Encode the value map[string]string using the cbor.NewEncoder.
//
// Note: this currently doesn't encode the data exactly the same
// as it got it (non-canonical).
var buf = bytes.NewBuffer(nil)
err = cbor.NewEncoder(buf).Encode(value)
if err != nil {
panic(err)
}// Output: b80278036f6e6501780374776f02
fmt.Printf("%x\n", buf.Bytes())
}
```