serde-json-core icon indicating copy to clipboard operation
serde-json-core copied to clipboard

Draft - De-Escaping strings using a provided buffer

Open sammhicks opened this issue 1 year ago • 0 comments
trafficstars

Here's a partial implementation of the design suggested in https://github.com/rust-embedded-community/serde-json-core/issues/79#issuecomment-2053622260_

How about the following design?:

  • The deserializer takes a shared &str, as per the design before this pull request.
  • When deserializing JSON strings, the deserializer scans for escape sequences
    • If there are no escape sequences, it calls visitor.visit_borrowed_str(v), which will allow zero-copy deserialization
    • If there are escape sequences, it decodes the escape sequence using a provided buffer, and calls visitor.visit_str(v)
  • serde_json_core has a EscapedString newtype struct which contains an escaped &str, with utility methods to iterator over it, where the iterator returns either a &str of characters with no escape sequences, or an unescaped char
    • EscapedString is the only structure that is allowed to borrow escaped string data, and uses a special constant to signal to the deserializer that it's special.

I believe that this design will also solve #74

An example usage of the new design can be seen at https://github.com/sammhicks/picoserve/tree/json-parse-demo

sammhicks avatar Apr 16 '24 11:04 sammhicks