Skip to content

Commit

Permalink
Adjust Structs usage as its API evolves a bit and gets cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Mar 16, 2024
1 parent a866287 commit 04b4fda
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/JSONBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Values = Union{LazyValue, BinaryValue}

# allow LazyValue/BinaryValue to participate in
# selection syntax by overloading applyeach
@inline function Structs.applyeach(f, x::Values)
@inline function Structs.applyeach(::Structs.StructStyle, f, x::Values)
if gettype(x) == JSONTypes.OBJECT
return applyobject(f, x)
elseif gettype(x) == JSONTypes.ARRAY
Expand Down
32 changes: 2 additions & 30 deletions src/json.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,6 @@ struct WriteClosure{JS, arraylike, T} # T is the type of the parent object/array
objids::Base.IdSet{Any} # to track circular references
end

# Structs.applyeach calls f(::String, val), but we want to call
# Structs.lower(T, ::Symbol, val), so translate here
@generated function fieldsym(::Type{T}, key) where {T}
ex = quote
# @show T, key, val
end
if key == String
for i = 1:fieldcount(T)
fnm = fieldname(T, i)
nm = String(fnm)
push!(ex.args, :(key == $nm && return $(Meta.quot(fnm))))
end
end
push!(ex.args, :(return key))
return ex
end

@inline function indent(buf, pos, ind, depth)
if ind > 0
n = ind * depth + 1
Expand All @@ -172,14 +155,6 @@ end
pos = indent(buf, pos, ind, f.depth)
# if not an array, we need to write the key + ':'
if !arraylike
if key isa Symbol
tags = Structs.fieldtags(f.style, T, key)
if tags !== nothing && haskey(tags, :name)
key = tags.name
end
else
tags = nothing
end
pos = _string(buf, pos, key)
@checkn 1
buf[pos] = UInt8(':')
Expand All @@ -189,17 +164,14 @@ end
buf[pos] = UInt8(' ')
pos += 1
end
lowered = Structs.lower(f.style, val, tags)
else
lowered = Structs.lower(f.style, val)
end
# check if the lowered value is in our objectid set
if lowered in f.objids
if val in f.objids
# if so, it's a circular reference! so we just write `null`
pos = _null(buf, pos)
else
# note that jsonlines is hard-coded as false here because you can't recursively print jsonlines
pos = json!(buf, pos, lowered, f.style, f.allownan, false, f.objids, ind, f.depth)
pos = json!(buf, pos, val, f.style, f.allownan, false, f.objids, ind, f.depth)
end
@checkn 1
@inbounds buf[pos] = f.jsonlines ? UInt8('\n') : UInt8(',')
Expand Down

0 comments on commit 04b4fda

Please sign in to comment.