Skip to content

Commit

Permalink
Make indenting JSON an optional kargs
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinchai committed Oct 10, 2024
1 parent f4e1675 commit 29e2781
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
20 changes: 16 additions & 4 deletions src/Storage/Storage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/ZArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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="",
Expand All @@ -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"))
Expand All @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions src/ZGroup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 29e2781

Please sign in to comment.