github.com/woodruffw/cbor.cr
0.1.1 / published Jun 4, 2020 / repository
Crystal bindings for libcbor
CBOR.cr
Crystal bindings for libcbor.
Status
- [x] Serialization (
to_cbor) - [ ] Deserialization (
from_cbor) - [ ]
JSON.mapping-style API - [ ] Nice error handling
- [ ] Unit tests
Installation
-
Make sure you have
libcborinstalled. Your package manager probably provides it. -
Add the dependency to your
shard.yml:dependencies: CBOR: github: woodruffw/cbor.cr -
Run
shards install
Usage
require "CBOR"
# Note: These print binary data!
puts -1.to_cbor
puts nil.to_cbor
puts true.to_cbor
puts 3.14.to_cbor
puts "abc".to_cbor
puts [1, 2, 3, "four"].to_cbor
puts Hash{"key" => "value"}.to_cbor
puts Time.now.to_cbor
CBOR.cr does not currently provide a mapping-style API, like the core JSON or YAML modules.
To serialize custom objects to CBOR, define #to_h on them and use to_h.to_cbor.
Contributing
- Fork it (https://github.com/woodruffw/CBOR/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- William Woodruff - creator and maintainer
API
- Array(T)
An
Arrayis an ordered, integer-indexed collection of objects of type T. - Bool
Bool has only two possible values:
trueandfalse. - CBOR
TODO Write documentation for
CBOR - Enum
Enum is the base type of all enums.
- Float
Float is the base type of all floating point numbers.
- Hash(K, V)
A
Hashrepresents a collection of key-value mappings, similar to a dictionary. - Int
Int is the base type of all integer types.
- NamedTuple(**T)
A named tuple is a fixed-size, immutable, stack-allocated mapping of a fixed set of keys to values.
- Nil
The
Niltype has only one possible value:nil. - Object
Objectis the base type of all Crystal objects. - Set(T)
Setimplements a collection of values with no duplicates. - Slice(T)
A
Sliceis aPointerwith an associated size. - String
A
Stringrepresents an immutable sequence of UTF-8 characters. - Symbol
A symbol is a constant that is identified by a name without you having to give it a numeric value.
- Time
Timerepresents a date-time instant in incremental time observed in a specific time zone. - Tuple(*T)
A tuple is a fixed-size, immutable, stack-allocated sequence of values of possibly different types.