Skip to content

Commit

Permalink
Add json_value for AbstractArray (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliza-eliza authored Aug 20, 2024
1 parent 1e15159 commit b678f31
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Serde"
uuid = "db9b398d-9517-45f8-9a95-92af99003e0e"
version = "3.3.0"
version = "3.4.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
12 changes: 12 additions & 0 deletions src/Ser/SerJson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,18 @@ function json_value!(buf::IOBuffer, f::Function, val::AbstractSet; l::Int64, kw.
return print(buf, indent(l - 1), "]")
end

function json_value!(buf::IOBuffer, f::Function, A::AbstractArray{<:Any,n}; l::Int64, kw...)::Nothing where {n}
print(buf, "[", indent(l))
newdims = ntuple(_ -> :, n - 1)
first = true
for j in axes(A, n)
first || print(buf, ",", indent(l))
first = false
json_value!(buf, f, view(A, newdims..., j); l = l + (l != -1), kw...)
end
print(buf, indent(l - 1), "]")
end

(isnull(::Any)::Bool) = false
(isnull(v::Missing)::Bool) = true
(isnull(v::Nothing)::Bool) = true
Expand Down
16 changes: 16 additions & 0 deletions test/Ser/SerJson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@
text::String
object::SerJsonObject
array::Vector{Int64}
matrix::Matrix{Float64}
end

obj = SerJsonFoo1(
34,
"sertupe",
SerJsonObject(Set(["a", "b"]), :a => 2, SerJsonFoo1, OneMoreObject(true, nothing)),
[1, 2, 3],
ones(2, 3)
)

@test Serde.to_pretty_json(obj) == """{
Expand All @@ -206,6 +208,20 @@
1,
2,
3
],
"matrix":[
[
1.0,
1.0
],
[
1.0,
1.0
],
[
1.0,
1.0
]
]
}"""
end
Expand Down

2 comments on commit b678f31

@gryumov
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

Release notes:

  • Add json_value for AbstractArray

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/113517

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.4.0 -m "<description of version>" b678f313f6cf5a9a18f75ef7cc83419e7c7b0eb3
git push origin v3.4.0

Please sign in to comment.