Skip to content

Commit 2ae3c2a

Browse files
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 <[email protected]> * Update src/Storage/Storage.jl Co-authored-by: Anshul Singhvi <[email protected]> * Update src/Storage/Storage.jl Co-authored-by: Anshul Singhvi <[email protected]> * Update src/ZArray.jl Co-authored-by: Anshul Singhvi <[email protected]> * Fix syntax error --------- Co-authored-by: Anshul Singhvi <[email protected]>
1 parent 21d59e6 commit 2ae3c2a

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/Storage/Storage.jl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ function getattrs(s::AbstractStore, p)
9595
JSON.parse(replace(String(maybecopy(atts)),": NaN,"=>": \"NaN\","))
9696
end
9797
end
98-
function writeattrs(s::AbstractStore, p, att::Dict)
98+
function writeattrs(s::AbstractStore, p, att::Dict; indent_json::Bool= false)
9999
b = IOBuffer()
100-
JSON.print(b,att)
100+
101+
if indent_json
102+
JSON.print(b,att,4)
103+
else
104+
JSON.print(b,att)
105+
end
106+
101107
s[p,".zattrs"] = take!(b)
102108
att
103109
end
@@ -110,9 +116,15 @@ isinitialized(s::AbstractStore, p, i) = isinitialized(s,_concatpath(p,i))
110116
isinitialized(s::AbstractStore, i) = s[i] !== nothing
111117

112118
getmetadata(s::AbstractStore, p,fill_as_missing) = Metadata(String(maybecopy(s[p,".zarray"])),fill_as_missing)
113-
function writemetadata(s::AbstractStore, p, m::Metadata)
119+
function writemetadata(s::AbstractStore, p, m::Metadata; indent_json::Bool= false)
114120
met = IOBuffer()
115-
JSON.print(met,m)
121+
122+
if indent_json
123+
JSON.print(met,m,4)
124+
else
125+
JSON.print(met,m)
126+
end
127+
116128
s[p,".zarray"] = take!(met)
117129
m
118130
end

src/ZArray.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ Creates a new empty zarr array with element type `T` and array dimensions `dims`
310310
* `compressor=BloscCompressor()` compressor type and properties
311311
* `attrs=Dict()` a dict containing key-value pairs with metadata attributes associated to the array
312312
* `writeable=true` determines if the array is opened in read-only or write mode
313+
* `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.
313314
"""
314315
function zcreate(::Type{T}, dims::Integer...;
315316
name="",
@@ -334,6 +335,7 @@ function zcreate(::Type{T},storage::AbstractStore,
334335
filters = filterfromtype(T),
335336
attrs=Dict(),
336337
writeable=true,
338+
indent_json=false
337339
) where T
338340

339341
length(dims) == length(chunks) || throw(DimensionMismatch("Dims must have the same length as chunks"))
@@ -353,9 +355,9 @@ function zcreate(::Type{T},storage::AbstractStore,
353355

354356
isemptysub(storage,path) || error("$storage $path is not empty")
355357

356-
writemetadata(storage, path, metadata)
358+
writemetadata(storage, path, metadata, indent_json=indent_json)
357359

358-
writeattrs(storage, path, attrs)
360+
writeattrs(storage, path, attrs, indent_json=indent_json)
359361

360362
ZArray{T2, N, typeof(compressor), typeof(storage)}(
361363
metadata, storage, path, attrs, writeable)

src/ZGroup.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,19 @@ end
128128
129129
Create a new zgroup in the store `s`
130130
"""
131-
function zgroup(s::AbstractStore, path::String=""; attrs=Dict())
131+
function zgroup(s::AbstractStore, path::String=""; attrs=Dict(), indent_json::Bool= false)
132132
d = Dict("zarr_format"=>2)
133133
isemptysub(s, path) || error("Store is not empty")
134134
b = IOBuffer()
135-
JSON.print(b,d)
135+
136+
if indent_json
137+
JSON.print(b,d,4)
138+
else
139+
JSON.print(b,d)
140+
end
141+
136142
s[path,".zgroup"]=take!(b)
137-
writeattrs(s,path,attrs)
143+
writeattrs(s,path,attrs, indent_json=indent_json)
138144
ZGroup(s, path, Dict{String,ZArray}(), Dict{String,ZGroup}(), attrs,true)
139145
end
140146

0 commit comments

Comments
 (0)