Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add indents for JSON files for readbility #167

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

if indent_json
JSON.print(b,att,4)
else
JSON.print(b,att)
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)

if indent_json
JSON.print(met,m,4)
else
JSON.print(met,m)
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=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="",
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)

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

Expand Down
Loading