kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

CBOR untaggedNullValueTags configuration option (#2966)

Open travishaagen opened this issue 3 months ago • 1 comments

Introduces a new CBOR configuration option called untaggedNullValueTags. When enabled, CBOR value-tags will not be serialized for null values, and deserialization will not fail validation. This is allowed by the CBOR spec as mentioned in this issue.

When untaggedNullValueTags=true, the encoding for the provided unit test is https://cbor.me/?bytes=bf6161a0d8226162f6d8386163f66164f6ff where the tags shown below are key-tags,

BF          # map(*)
   61       # text(1)
      61    # "a"
   A0       # map(0)
   D8 22    # tag(34)
      61    # text(1)
         62 # "b"
   F6       # primitive(22)
   D8 38    # tag(56)
      61    # text(1)
         63 # "c"
   F6       # primitive(22)
   61       # text(1)
      64    # "d"
   F6       # primitive(22)
   FF       # primitive(*)

For comparison, when untaggedNullValueTags=false, the encoding is https://cbor.me/?bytes=bf6161cca0d8226162f6d8386163d84ef66164d85accf6ff

BF          # map(*)
   61       # text(1)
      61    # "a"
   CC       # tag(12)
      A0    # map(0)
   D8 22    # tag(34)
      61    # text(1)
         62 # "b"
   F6       # primitive(22)
   D8 38    # tag(56)
      61    # text(1)
         63 # "c"
   D8 4E    # tag(78)
      F6    # primitive(22)
   61       # text(1)
      64    # "d"
   D8 5A    # tag(90)
      CC    # tag(12)
         F6 # primitive(22)
   FF       # primitive(*)

Note however, that Kotlinx Serialization cannot currently deserialize the above anyhow.

This change is needed for a real world use-case, where "untagged null values" are a requirement for value-tags. Thank you!

travishaagen avatar Sep 06 '25 18:09 travishaagen

@fzhinkin Hello! Thanks for taking a look. How can I help move this forward?

travishaagen avatar Sep 16 '25 11:09 travishaagen