From 2ae3c2a088094e1c1f57d8bea84a68a09e56c7fe Mon Sep 17 00:00:00 2001 From: Calvin Chai Date: Fri, 6 Dec 2024 09:06:25 -0500 Subject: [PATCH] Add indents for JSON files for readbility (#167) * Add indents for .zgroup file * Add indents for .zattrs and .zarray * Make indenting JSON an optional kargs * Update src/ZGroup.jl Co-authored-by: Anshul Singhvi * Update src/Storage/Storage.jl Co-authored-by: Anshul Singhvi * Update src/Storage/Storage.jl Co-authored-by: Anshul Singhvi * Update src/ZArray.jl Co-authored-by: Anshul Singhvi * Fix syntax error --------- Co-authored-by: Anshul Singhvi --- 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 e54fa94..46c819a 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) + + if indent_json + JSON.print(b,att,4) + else + JSON.print(b,att) + 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) + + if indent_json + JSON.print(met,m,4) + else + JSON.print(met,m) + end + s[p,".zarray"] = take!(met) m end diff --git a/src/ZArray.jl b/src/ZArray.jl index acbf23e..b095568 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=false` determines if indents are added to format the json files `.zarray` and `.zattrs`. This makes them more readable, but increases file size. """ 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 acecf2b..35515ed 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) + + if indent_json + JSON.print(b,d,4) + else + JSON.print(b,d) + 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