Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBOR - Default generator generates object with map(*) instead of map(2) #376

Open
x-devel opened this issue Apr 28, 2023 · 2 comments
Open

Comments

@x-devel
Copy link

x-devel commented Apr 28, 2023

Hey there! I'm using the CBOR jackson library with Kotlin and I'm having some issues when performing a CBOR encoding of an object.
In this example I'm serializing the Person class to a CBOR byte array and then visualizing it in its hex form with an extension function on the ByteArray class.

val person = Person("John", "Doe")
val mapper = CBORMapper().registerKotlinModule()
val cborHex = mapper.writeValueAsBytes(person).toHex()
println(cborHex)

Consider that the Person class is mapped in this particular way

data class Person(
    @JsonProperty("0")
    val name: String,
    @JsonProperty("1")
    val surname: String
)

The output that I'm getting with the code that I've just shown is: bf6130644a6f686e613163446f65ff - 15 bytes but looking at the CBOR playground website I should instead be getting the optimal output which is: A26130644A6F686E613163446F65 - 14 bytes.
I think that the issue is caused by the default parser that considers maps and arrays as collections of indefinite size when parsing them instead of starting object and arrays indicating their respective sizes.

So, is there a solution on what to do to get the optimal output?

@cowtowncoder
Copy link
Member

(I assume you mean "generator" instead of "parser" in above).

Yes, the current CBORGenerator encodes Objects and Maps using indefinite length marker (and end marker). This is legal output and is a trade-off (encoding is easier when there is no requirement to ensure specific length before output).
There is currently no way to make output use length-prefix for Objects (there is for Arrays); issue #3 is for adding such support: it would be easier to add for Java Maps than POJOs (mostly due to possible JSON Filters for POJOs).

@cowtowncoder cowtowncoder changed the title CBOR - Default parser parses object with map(*) instead of map(2) CBOR - Default generator generates object with map(*) instead of map(2) Jun 19, 2023
@cowtowncoder
Copy link
Member

Realized this is duplicate of #329 (or even #3), closing as such.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants