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

Handle quote escapes in keys and string values #4

Open
kaleidawave opened this issue Jan 13, 2025 · 3 comments · May be fixed by #6
Open

Handle quote escapes in keys and string values #4

kaleidawave opened this issue Jan 13, 2025 · 3 comments · May be fixed by #6
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@kaleidawave
Copy link
Owner

In JSON you can include a double quote character in either keys or values by prefixing it with a backslash. This unfortunately means that the source deviates from content. Currently these are un-accounted for.

Processing the strings could

  1. Be overhead, might require allocating all strings
  2. Break the ability to pattern match on them

One method is just to bear this in mind when using the library. If it is important then leave the user to handle them on their end (same way that numbers are not processed into f64 or another form)

Some questions to consider

  • Is it worth making things more verbose or slower to cater for an edge case
  • How prevalent are " in JSON keys and values?
  • What other characters can be escaped
  • Is supporting this via a feature flag a good compromise or confusing?
@kaleidawave kaleidawave added help wanted Extra attention is needed question Further information is requested labels Jan 13, 2025
@devongovett
Copy link

You could use Cow for this potentially. That way you only allocate when there are escaped characters. Example where I did something similar (actually the opposite): https://github.com/parcel-bundler/parcel/blob/f86f5f27c3a6553e70bd35652f19e6ab8d8e4e4a/crates/dev-dep-resolver/src/lib.rs#L368-L380

@kaleidawave
Copy link
Owner Author

Interesting! I did not know that Cow has a special AddAssign implementation for str so that you can do += without it allocating character bytes. Will figure out a way to reverse that for encoding/un-escaping.

@kaleidawave
Copy link
Owner Author

kaleidawave commented Jan 16, 2025

That can handle the JSON string values. As for keys, not sure about changing the keys as wouldn't work with the current pattern matching examples. Maybe can look if/when match_deref for strings is a feature (hopefully it can work across Cow). However in the meantime there could be another API using a function, which can internally equate the strings handling escapes (ignore backslashes).

if let Some(value) = key_chain.matches(&["a-key \"something\""]) {
	// ...
}

@kaleidawave kaleidawave linked a pull request Jan 19, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants