From 29e2781c6c5d74bf71019d19cb12e0f1bf853aa6 Mon Sep 17 00:00:00 2001 From: Kaidong Chai Date: Thu, 10 Oct 2024 10:51:12 -0400 Subject: [PATCH] Make indenting JSON an optional kargs --- src/Storage/Storage.jl | 20 ++++++++++++++++---- src/ZArray.jl | 6 ++++-- src/ZGroup.jl | 12 +++++++++--- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/Storage/Storage.jl b/src/Storage/Storage.jl index a7bde78..db9e2c1 100644 --- a/src/Storage/Storage.jl +++ b/src/Storage/Storage.jl @@ -95,9 +95,15 @@ function getattrs(s::AbstractStore, p) JSON.parse(replace(String(maybecopy(atts)),": NaN,"=>": \"NaN\",")) end end -function writeattrs(s::AbstractStore, p, att::Dict) +function writeattrs(s::AbstractStore, p, att::Dict; indent_json::Bool= false) b = IOBuffer() - JSON.print(b,att,4) + + if !indent_json + JSON.print(b,att) + else + JSON.print(b,att,4) + end + s[p,".zattrs"] = take!(b) att end @@ -110,9 +116,15 @@ isinitialized(s::AbstractStore, p, i) = isinitialized(s,_concatpath(p,i)) isinitialized(s::AbstractStore, i) = s[i] !== nothing getmetadata(s::AbstractStore, p,fill_as_missing) = Metadata(String(maybecopy(s[p,".zarray"])),fill_as_missing) -function writemetadata(s::AbstractStore, p, m::Metadata) +function writemetadata(s::AbstractStore, p, m::Metadata; indent_json::Bool= false) met = IOBuffer() - JSON.print(met,m,4) + + if !indent_json + JSON.print(met,m) + else + JSON.print(met,m,4) + end + s[p,".zarray"] = take!(met) m end diff --git a/src/ZArray.jl b/src/ZArray.jl index acbf23e..0cae43e 100644 --- a/src/ZArray.jl +++ b/src/ZArray.jl @@ -310,6 +310,7 @@ Creates a new empty zarr array with element type `T` and array dimensions `dims` * `compressor=BloscCompressor()` compressor type and properties * `attrs=Dict()` a dict containing key-value pairs with metadata attributes associated to the array * `writeable=true` determines if the array is opened in read-only or write mode +* `indent_json` determines if indents are added to format the json files """ function zcreate(::Type{T}, dims::Integer...; name="", @@ -334,6 +335,7 @@ function zcreate(::Type{T},storage::AbstractStore, filters = filterfromtype(T), attrs=Dict(), writeable=true, + indent_json=false ) where T length(dims) == length(chunks) || throw(DimensionMismatch("Dims must have the same length as chunks")) @@ -353,9 +355,9 @@ function zcreate(::Type{T},storage::AbstractStore, isemptysub(storage,path) || error("$storage $path is not empty") - writemetadata(storage, path, metadata) + writemetadata(storage, path, metadata, indent_json=indent_json) - writeattrs(storage, path, attrs) + writeattrs(storage, path, attrs, indent_json=indent_json) ZArray{T2, N, typeof(compressor), typeof(storage)}( metadata, storage, path, attrs, writeable) diff --git a/src/ZGroup.jl b/src/ZGroup.jl index 5473f5e..dfd1c27 100644 --- a/src/ZGroup.jl +++ b/src/ZGroup.jl @@ -128,13 +128,19 @@ end Create a new zgroup in the store `s` """ -function zgroup(s::AbstractStore, path::String=""; attrs=Dict()) +function zgroup(s::AbstractStore, path::String=""; attrs=Dict(); indent_json::Bool= false) d = Dict("zarr_format"=>2) isemptysub(s, path) || error("Store is not empty") b = IOBuffer() - JSON.print(b,d,4) + + if !indent_json + JSON.print(b,d) + else + JSON.print(b,d,4) + end + s[path,".zgroup"]=take!(b) - writeattrs(s,path,attrs) + writeattrs(s,path,attrs, indent_json=indent_json) ZGroup(s, path, Dict{String,ZArray}(), Dict{String,ZGroup}(), attrs,true) end