rust-embedded-community / serde-json-core

`serde-json` for `no_std` programs
Apache License 2.0
161 stars 59 forks source link

Deserializing strings in place. #79

Closed sammhicks closed 7 months ago

sammhicks commented 10 months ago

De-escaping JSON strings will always produce a shorter (or equal length) string, so it's safe to de-escape strings in place, thus allowing types to borrow plaintext (not escaped) strings from the buffer after deserialization.

This is a semver breaking change as it requires the JSON input to be passed in mutably to allow for the de-escaping in place. This is safe because once the serialization has passed the string, it never reads that part of the buffer again.

sammhicks commented 10 months ago

Not to nag, but I've now fixed the tests, they run successfully on my fork, and thus it should now be ready to test and merge. Sorry for the repeated sync, and absolutely no rush :)

sammhicks commented 7 months ago

How about the following design?:

I believe that this design will also solve #74

ryan-summers commented 7 months ago

That definitely sounds like a nice approach to me! It may be useful to try and look at some cases where string escaping is useful to see if this design is onerous for the end user. Do you have a sample use case in mind? That may be helpful in figuring out if this is a good approach.

sammhicks commented 7 months ago

I've written a bare-metal HTTP server framework, and would like it to be able to deserialize JSON encoded POST bodies into arbitary data structures, and on soundness grounds and to have separation of concerns, would like all decoding and unescaping to happen before the data is passed to the request handler.

ryan-summers commented 7 months ago

Is this open-sourced and/or something I could look at to get an idea of how you're interested in using it? What I'm really trying to figure out is what this API would look like in a real-world example with actual code

sammhicks commented 5 months ago

In case the message got lost in the post (hurrah for email), I've closed this pull request and opened a new one at https://github.com/rust-embedded-community/serde-json-core/pull/83 with my new design.