ciborium icon indicating copy to clipboard operation
ciborium copied to clipboard

[Bug]: Roundtripping Option<UnitStruct> always results in None

Open tekacs opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Current Behaviour

Given the type:

pub struct Test;

Some(Test), roundtripped through encode/decode comes back as None. It appears that it's actually being encoded as None (F6 / null in CBOR).

Expected Behaviour

The value Some(Test) should roundtrip as Some(Test) and be encoded in a fashion which can be recovered.

Environment Information

ciborium = "0.2.1"
serde = { version = "1", features = ["derive"] }

Steps To Reproduce

There's a minimal reproduction of this issue here:

https://www.rustexplorer.com/b/3cig4b

tekacs avatar Jan 08 '24 07:01 tekacs

Having now tried this with serde_json in the same reproduction, I find that this issue occurs there too, so... not a ciborium thing?

https://www.rustexplorer.com/b/f3jizk

Apologies. I'll try to figure out why this happens first.

tekacs avatar Jan 08 '24 07:01 tekacs

So it looks like this is an issue with serde_json as well... but I guess there's an open question as to whether this is solvable in ciborium?

https://github.com/serde-rs/serde/issues/2462

Reopening in case there's any appetite or suggestions to resolve this here.

tekacs avatar Jan 08 '24 07:01 tekacs

If you look at https://serde.rs/json.html

struct X(i32, i32);
let x = X(0, 0); // Represented as `[0,0]`

struct Y(i32);
let y = Y(0); // Represented as just the inner value `0`

struct Z;
let z = Z; // Represented as `null`

This could've easily been [0,0], [0] and []. RON for example, does distinguish None from ().

ciborium could change this but it'd be a breaking change, so not sure if people want to do that. Or it could there could be an option.

serde_derive supports #[serde(transparent)] for [0] to 0. It could have something similar for having [] turn into null, i.e. serialize_none().

ahmedcharles avatar Feb 19 '24 12:02 ahmedcharles