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

Choose JSON Renderer due to bug in JSON3/StructTypes #636

Open
lazzarello opened this issue Mar 22, 2023 · 5 comments
Open

Choose JSON Renderer due to bug in JSON3/StructTypes #636

lazzarello opened this issue Mar 22, 2023 · 5 comments

Comments

@lazzarello
Copy link

lazzarello commented Mar 22, 2023

I'm working on an API that needs to serialize a matrix. The JSON.jl package outputs the following

julia> JSON.json(zeros(Float64, 6, 6))
"[[0.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0],[0.0,0.0,0.0,0.0,0.0,0.0]]"

Which are the results I expect. The JSON3.jl package flattens the array without any information about the original dimensions.

julia> JSON3.write(zeros(Float64, 6, 6))
"[0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0]"

Because of #304 the Genie JSON renderer uses JSON3, so I have to make my own renderer. The upstream projects have open issues[1][2] to address this unexpected type behavior but it doesn't look like anyone is working them. It would be nice to perhaps add a keyword argument to the Genie renderer to choose which backing package to use.

[1] quinnj/JSON3.jl#196
[2] JuliaData/StructTypes.jl#14

@hhaensel
Copy link
Member

Are you talking about Genie or Stipple?

For Stipple, I just committed a change that supports parsing list of lists into a matrix.
For testing use

] add Stipple#master

@essenciary We could opt to render Matrices as list of lists.
Then everything would work out fine.

With the above commit, you can always opt in to rendering matrices as list of lists by

Stipple.render(val::Matrix, fieldname::Union{Nothing, Symbol}) = collect(eachrow(val))

and communication in Stipple apps should be ok.

@lazzarello
Copy link
Author

I don't know what Stipple is yet. I'm talking about Genie.

@hhaensel
Copy link
Member

hhaensel commented May 7, 2023

Stipple.jl is part of the GenieFramework.
It's a page renderer for Vue.js webpages with bidirectional communication and has it's own render function.
The approach I took could be usable for your Genie page, though.
Do you have may matrices to be converted?

@lazzarello
Copy link
Author

I'll pick this up in a couple days, thanks for the advice I'll look at Stipple.

@essenciary
Copy link
Member

As the JSON3 issue is still open, looks like a good time to add a choice of JSON serializers (between JSON and JSON3) with JSON3 as the default, in Genie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants