Skip to content

Add JSON.json(Vector{UInt8}, x) to write JSON to a byte vector - #494

Merged
quinnj merged 1 commit into
masterfrom
nz/write-Vector
Oct 2, 2026
Merged

quinnj merged 1 commit into
masterfrom
nz/write-Vector

Conversation

@nhz2

@nhz2 nhz2 commented Oct 1, 2026

Copy link
Copy Markdown
Member

The new method returns the output as a 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").

Question: Is this behavior change breaking, and if so should this method instead be a new function?

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>
@nhz2 nhz2 changed the title Add JSON.json(Vector{UInt8}, x) to write JSON to a byte vector Add JSON.json(Vector{UInt8}, x) to write JSON to a byte vector Oct 1, 2026
@codecov

codecov Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.12%. Comparing base (a69c082) to head (d53dfb1).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@quinnj quinnj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this sounds fine to me.

@quinnj
quinnj merged commit 3d34308 into master Oct 2, 2026
12 checks passed
@quinnj
quinnj deleted the nz/write-Vector branch October 2, 2026 13:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants