Add JSON.json(Vector{UInt8}, x) to write JSON to a byte vector - #494
Merged
Merged
Conversation
The new method returns the output as a regular, growable
`Vector{UInt8}` rather than a `String`. Getting bytes from
`Vector{UInt8}(JSON.json(x))` copies the whole output; the new method
does not. On Julia 1.13 with a small String => String Dict:
julia> x = Dict("a" => "b", "c" => "d");
julia> @Btime Vector{UInt8}(JSON.json($x));
193.190 ns (10 allocations: 896 bytes)
julia> @Btime (io = IOBuffer(); JSON.json(io, $x); take!(io));
196.597 ns (11 allocations: 976 bytes)
julia> @Btime JSON.json(Vector{UInt8}, $x);
156.349 ns (7 allocations: 784 bytes)
Option construction and validation shared by the `json` entry points
now lives in `writeoptions`.
This changes the behavior of `JSON.json(Vector{UInt8}, x)` calls.
Previously, an integer `x` hit the pre-1.0 `json(x, indent)` compat
path and serialized the type itself:
julia> JSON.json(Vector{UInt8}, 1)
"\"Vector{UInt8}\""
It now returns the bytes of `1`. Any other `x` previously threw an
`AssertionError` ("filename must be a string").
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
JSON.json(Vector{UInt8}, x) to write JSON to a byte vector
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #494 +/- ##
==========================================
+ Coverage 92.09% 92.12% +0.03%
==========================================
Files 7 7
Lines 1784 1791 +7
==========================================
+ Hits 1643 1650 +7
Misses 141 141 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The new method returns the output as a
Vector{UInt8}rather than aString. Getting bytes fromVector{UInt8}(JSON.json(x))copies the whole output; the new method does not. On Julia 1.13 with a small String => String Dict:Option construction and validation shared by the
jsonentry points now lives inwriteoptions.This changes the behavior of
JSON.json(Vector{UInt8}, x)calls. Previously, an integerxhit the pre-1.0json(x, indent)compat path and serialized the type itself:It now returns the bytes of
1. Any otherxpreviously threw anAssertionError("filename must be a string").Question: Is this behavior change breaking, and if so should this method instead be a new function?