cabo / cbor-ruby

CBOR (RFC 7049) extension for Ruby
45 stars 12 forks source link

Fails when trying to encode Date #7

Closed twigbranch closed 5 years ago

twigbranch commented 7 years ago

NoMethodError: undefined methodto_cbor' for Fri, 30 Aug 2013:Date`

Shouldn't Date be supported?

cabo commented 7 years ago

Yes! There are a number of CBOR tags that aren't very well meshed with the data types available in Ruby yet. We are actually in the process of filling that matrix in at this very moment, please see https://github.com/cbor-wg/CBORbis/wiki/Implementation-matrix

(Question: Would you prefer CBOR tag 0 or tag 1 for objects of Ruby type Date?)

twigbranch commented 7 years ago

I'll have to think about the question (new to CBOR). But, yes, ultimately, it would be great if it could act as a drop in replacement for to_json.

mcr commented 6 years ago

I would prefer CBOR tag 1 in most cases, but for the cases where I'd prefer the string (tag 0), I don't know how I'd say so.

cabo commented 6 years ago

Basically, you add a to_cbor method to the object that you want to encode in CBOR. This can be done by adding the method to its class (Date or Time), or by adding a method just to the object (e.g., by extending it with a Module that has the desired method). I probably should write some example code into the docs...

cabo commented 6 years ago

https://gist.github.com/cabo/fb2f6ea30f1158b63e6c5649beabfc75 has some examples.