Fastest JSON parser to julia

Update: My implementation isn’t correct as it assumed ordered fields.

The implementation of the json benchmark looks not very favorable
Compare to the native facility of using StructTypes:

using JSON3
using StructTypes

struct Coordinate
    x::Float64
    y::Float64
    z::Float64
end

struct Coordinates
    arr::Vector{Coordinate}
end

StructTypes.StructType(::Type{Coordinate}) = StructTypes.Struct()
StructTypes.StructType(::Type{Coordinates}) = StructTypes.Struct()


function calc(text)
    jobj = JSON3.read(text)
    coordinates = jobj["coordinates"]
    len = length(coordinates)
    x = y = z = 0

    for coord in coordinates
        x += coord["x"]
        y += coord["y"]
        z += coord["z"]
    end

    Coordinate(x / len, y / len, z / len)
end

function calc_struct(text)
    coordinates = JSON3.read(text,Coordinates)
    
    len = length(coordinates.arr)
    x = y = z = 0

    for coord in coordinates.arr
        x += coord.x
        y += coord.y
        z += coord.z
    end

    Coordinate(x / len, y / len, z / len)
end

leads to:

julia> @btime calc(text)
  3.314 μs (20 allocations: 1.14 KiB)
Coordinate(2.0, 0.5, 0.25)

julia> @btime calc_struct(text)
  297.748 ns (11 allocations: 496 bytes)
Coordinate(2.0, 0.5, 0.25)
2 Likes