Skip to content

Conversation

@nhz2
Copy link
Member

@nhz2 nhz2 commented Oct 3, 2025

This PR adds a JSON.write_json function with the same functionality and keyword arguments as the new JSON.json function. This function is provided for backward compatibility when upgrading from
older versions of JSON.jl where the json function signature differed.

The idea is that JSON.write_json will be backported to v0.21.5

EDIT: Changed the name from writefile to write_json

@codecov
Copy link

codecov bot commented Oct 3, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.20%. Comparing base (ac46743) to head (f25c33a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #381      +/-   ##
==========================================
+ Coverage   90.10%   90.20%   +0.10%     
==========================================
  Files           7        7              
  Lines        1334     1348      +14     
==========================================
+ Hits         1202     1216      +14     
  Misses        132      132              

☔ View full report in Codecov by Sentry.
📢 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.

src/JSON.jl Outdated
"""
function writefile end

function writefile(::Type{Vector{UInt8}}, x; pretty::Union{Integer,Bool}=false, kw...)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not following why we want this or the 2nd definitions? writefile that doesn't write to a file and just returns a byte vector?

Copy link
Member Author

Choose a reason for hiding this comment

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

I want to avoid extra conversions between String and Vector{UInt8} for use with Zarr.jl and SmallZarrGroups.jl

I'm also trying to match the style of the writefile function in Parquet2.jl https://expandingman.gitlab.io/Parquet2.jl/#Writing-Data

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, looking at this again, writefile isn't a good name for the function, so I changed it to write_json.

Copy link
Member Author

Choose a reason for hiding this comment

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

Here is a benchmark on the cost of doing the extra String to Vector{UInt8} conversion:

julia> using Chairmarks, JSON, Random

julia> @be randstring(10000) Vector{UInt8}(JSON.json(_)) seconds=2
Benchmark: 52143 samples with 4 evaluations
 min    4.807 μs (7 allocs: 19.852 KiB)
 median 4.874 μs (7 allocs: 19.852 KiB)
 mean   5.566 μs (7 allocs: 19.856 KiB, 0.82% gc time)
 max    15.658 ms (7 allocs: 19.883 KiB, 99.92% gc time)

julia> @be randstring(10000) JSON.write_json(Vector{UInt8}, _) seconds=2
Benchmark: 50390 samples with 7 evaluations
 min    2.971 μs (4 allocs: 9.914 KiB)
 median 3.006 μs (4 allocs: 9.914 KiB)
 mean   3.394 μs (4 allocs: 9.919 KiB, 0.82% gc time)
 max    8.692 ms (4 allocs: 9.945 KiB, 99.92% gc time)

@nhz2 nhz2 marked this pull request as draft November 8, 2025 03:46
@nhz2 nhz2 changed the title Add writefile to improve compatibility Add write_json to improve compatibility Nov 9, 2025
@nhz2 nhz2 marked this pull request as ready for review November 9, 2025 21:40
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

Successfully merging this pull request may close these issues.

3 participants