-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
JSON WriteStream functionality regression #16519
Comments
If anyone else runs into this issue, one hack that seems to work, is calling If you change the pub fn jsonStringify(self: Name, out: anytype) !void {
try out.writePreformatted("");
return std.json.encodeJsonString(self.first, .{}, out.stream);
} It'll work. |
Are you looking to write long strings in streaming chunks? is that the use case? I'd like to better understand the actual use case. The example code in the post above includes a working line commented out, so presumably that example wasn't complex enough to demonstrate the real issue, right? I'd like to see if your use case can be properly supported rather than being hacked in by reaching into the state or abusing the preformatted function. |
I wanted explicit control over how floats and integers are serialized. I know both these are documented, but integers being serialized as strings in some cases, and the way floats are treated (e..g Before this change, I could just use |
Ok that makes sense! So doing something like |
Yes, that's what we've done for the time being. |
How about an API like this: /// An alternative to calling `write` that formats the value with `std.fmt`.
/// This function does the usual punctuation and indentation formatting
/// assuming the resulting formatted string represents a single complete value.
/// This function is useful for doing your own number formatting.
pub fn print(self: *Self, comptime fmt: []const u8, args: anytype) Error!void {
try self.valueStart();
try self.stream.print(fmt, args);
self.valueDone();
} |
And then, it's not clear why we'd need |
That would solve my specific case, yes. |
I'm going to declare this fixed by #16543. I'm happy to re-open if anyone wants to elaborate on the use case that is still not addressed here. |
Zig Version
0.11.0-dev.4191+1bf16b172
Steps to Reproduce and Observed Behavior
The issue with the new WriteStream API introduced in #16405 is that you can no longer compose values, trying to force this by reaching into
ws.stream
will crash.Here's code that crashes:
Maybe it's wrong to use
WriteStream.stream
directly, but without this functionality, it's hard to have fine-grained controlled over the output.writePreformatted
is the only available option, but now you need to add another writer to format your value...WriteStream isn't really a "stream" API.Expected Behavior
I expect WriteStream to continue to expose an io.Writer interface.
The text was updated successfully, but these errors were encountered: