https://github.com/murakmii/cborb
Pure ruby decoder for CBOR(RFC 7049)
https://github.com/murakmii/cborb
cbor gem ruby
Last synced: 5 months ago
JSON representation
Pure ruby decoder for CBOR(RFC 7049)
- Host: GitHub
- URL: https://github.com/murakmii/cborb
- Owner: murakmii
- License: mit
- Created: 2018-07-07T14:09:24.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-11-10T12:56:51.000Z (over 7 years ago)
- Last Synced: 2024-12-18T02:17:11.971Z (over 1 year ago)
- Topics: cbor, gem, ruby
- Language: Ruby
- Homepage:
- Size: 79.1 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Cborb
[](https://badge.fury.io/rb/cborb)
[](https://circleci.com/gh/murakmii/cborb/tree/master)
Cborb is a pure ruby decoder for CBOR([RFC 7049](https://tools.ietf.org/html/rfc7049))
```rb
require "cborb"
decoder = Cborb::Decoding::Decoder.new
decoder.decode("\x83\x01")
decoder.finished? # => false
decoder.decode("\x02\x03\x04")
decoder.finished? # => true
decoder.result # => [1, 2, 3]
decoder.remaining_bytes # => "\x04"
# Shorthand
Cborb.decode("\x83\x01\x02\x03") # => [1, 2, 3]
```
## Handling indefinite-length data
Cborb can handle indefinite-length data(array, map, byte string, unicode string).
```rb
Cborb.decode("\x9F\x01\x82\x02\x03\x9F\x04\x05\xFF\xFF") # => [1, [2, 3], [4, 5]]
```
## Handling tags
If Cborb encounters a tag, generates instance of `Cborb::Decoding::TaggedValue`.
That contains tag number and original value.
```rb
Cborb.decode "\xC0" + "\x71" + "1970-01-01T00:00Z"
# => #
```
## Handling simple values
Cborb can handle simple values(true, false, nil, undefined).
```rb
Cborb.decode "\xF5" # => true
```
"Undefined" doesn't exist in Ruby.
So, Cborb converts "undefined" to `nil`.
```rb
Cborb.decode "\xF7" # => nil
```
If Cborb encounters unassigned simple value, generates instance of `Cborb::Decoding::UnassignedSimpleValue`.
That contains simple value number.
```rb
Cborb.decode "\xEF"
# => #
```
## Handling concatenated CBOR
By default, when decoder received concatenated CBOR, that raises error.
```rb
Cborb.decode("\x83\x01\x02\x03\x04") # => Cborb::InvalidByteSequenceError
```
If you want to decode concatenated CBOR, set `concatenated` option to `true`.
Decoder decodes whole of concatenated CBOR and returns instance of `Cborb::Decoding::Concatenated`.
```rb
results = Cborb.decode("\x83\x01\x02\x03\x04", concatenated: true)
# => #
results.first # => [1, 2, 3]
results.last # => 4
```
## Development
```bash
# Clone
git clone git@github.com:murakmii/cborb && cd cborb
# Setup
bin/setup
# Edit code...
# Run test
bundle exec rspec
```
## Contribution
Contributions are always welcome :kissing_heart: