quinnj / JSON3.jl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Discussion: relax order requirement for StructTypes.Struct()

chris-b1 opened this issue · comments

This isn't a bug, as it is well documented, but I wanted to raise the possibility of changing this behavior. Possibly related to #47 Simple example

using JSON3
using StructTypes

struct Foo
    a::Int64
    b::Int64
end
StructTypes.StructType(::Type{Foo}) = StructTypes.Struct()

julia> JSON3.read("""{"b": 1, "a": 2}""", Foo).a
1 # oops!

The two issues I see are

  • (in my opinion) - breaks principle of least surprise. Using the "default" julia type (struct) with a JSON parser (unordered), implicitly requires ordered data
  • Forces uses of mutable structs to safely interact with JSON3

That all said I don't know how technically feasible this is - naively I would think because you know the field order of Foo, the parser could re-order data as it receives it to match, but assume this would at minimum cost some performance.

Strict order requirements for Struct have been lifted as of #113