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

readObject without allocating for each key? #54

Open
Panke opened this issue Dec 12, 2020 · 0 comments
Open

readObject without allocating for each key? #54

Panke opened this issue Dec 12, 2020 · 0 comments

Comments

@Panke
Copy link

Panke commented Dec 12, 2020

stdx.data.json.parser.readObject issues a callback for each key in an object. The type for the callback is scope void delegate(string key) @safe del).

For my use case it would be sufficient to get the raw key string, e.g. use a delegate with type void delegate(JSONString!String key) to safe on allocations and unescaping the string. The next best solution would be to avoid string and get a const(char)[] as a key. Currently I cannot do this:

void main()
{
	const(char)[] json = q{{"key": "val"}}.dup;
	auto nodes = parseJSONStream!(LexOptions.useLong, const(char)[])(json);

	void delegate(const (char)[] k) @safe del = (const(char)[] x) { writeln("x"); };
	nodes.readObject(del);
}

Error: delegate del(string key) is not callable using argument types (const(char)[])

Looking at the code, a possible work around would be.

  1. cast the input to immutable (although it isn't)
  2. use string throughout, the lexing code will not allocate if the input is immutable and does not need unescaping
  3. be careful to cast everything back / correctly handle the immutable values that aren't.

It will still check if the keys are escaped, although I don't need that. Thinking about it, maybe it's enough to change the signature to void delegate(String key) and also provide a slice if the buffer is not immutable, but const.

What do you think?

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

No branches or pull requests

1 participant