Nemo157 / coap-browse

Apache License 2.0
1 stars 0 forks source link

Render CBOR diagnostic notation #1

Open Nemo157 opened 6 years ago

Nemo157 commented 6 years ago

Currently CBOR is being displayed using the debug output for serde_cbor::Value. This is not great,

Instead it would be good to show the CBOR diagnostic notation, described in RFC 7049 §6. Maybe this could be done via a new serde_cbor_diag crate?

As an example, the trivial message:

a1 66 73 74 61 74 75 73 f5

when output currently looks like

Object(
    {
        String(
            "status"
        ): Bool(
            true
        )
    }
)

but encoded to CBOR diagnostic notation would appear as:

{
    "status": true
}
Nemo157 commented 6 years ago

A trivial way to improve this for now could be to output to JSON, CBOR diagnostic format is largely based on JSON anyway, it would just miss out on some of the features it doesn't support like non-string keys, tagged items, etc.

Nemo157 commented 6 years ago

Also interesting would be a commented hex form, similar to what cbor.me supports

Nemo157 commented 6 years ago

This is now using serde_cbor-diag for rendering. Still not great though, since serde isn't capable of representing the exact object model that CBOR uses. Looking at alternative libraries there is also cbor which would allow getting the tagged information through. I don't think it supports getting whether a value was encoded with an indefinite encoding either.

Maybe it will be best to just write a custom &[u8] -> String function.