Skip to content

Commit fdd36d4

Browse files
authored
Merge pull request #848 from JuliaRobotics/22Q1/enh/topjsononly
top level JSON2.write only, distr are JSON
2 parents 979b427 + 79a5493 commit fdd36d4

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

src/Neo4jDFG/services/Neo4jDFG.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ function getFactor(dfg::Neo4jDFG, label::Union{Symbol, String})
271271
props = result.results[1]["data"][1]["row"][1]
272272

273273
# NOTE: Until we address #590, base64 decode the data to ensure robustness, #833
274-
props["data"] = String(base64decode(props["data"]))
274+
# TODO: consolidate with top level only JSON, #848
275+
packedData = String(base64decode(props["data"]))
276+
props["data"] = JSON2.read(packedData)
275277

276278
return rebuildFactorMetadata!(dfg, unpackFactor(dfg, props))
277279
end

src/entities/DFGFactor.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ mutable struct GenericFunctionNodeData{T<:Union{<:AbstractPackedFactor, <:Abstra
4646
nullhypo::Float64
4747
solveInProgress::Int
4848
inflation::Float64
49-
# inner constructor needed for dual use
50-
# GenericFunctionNodeData{T}(x1::Bool,x2::Bool,x3,x4::T,args...) where T = new{T}(x1,x2,x3,x4,args...)
51-
# TODO deprecate all these inner constructors at end of DFG v0.9.x (was added for GFND.nullhypo::Float64 breaking change)
52-
# GenericFunctionNodeData{T}(el,po,ed,fn,mu::Vector{<:Real},ce::Vector{Int},so::Int) where T =
53-
# new{T}(el,po,ed,fn,mu,ce,0.0,so)
54-
# GenericFunctionNodeData{T}(el,po,ed,fn,mu::Vector{<:Real},ce::Vector{Int},nu::Real,so::Int,infl::Real) where T = new{T}(el,po,ed,fn,mu,ce,nu,so,infl)
5549
end
5650

5751
## Constructors

src/services/Serialization.jl

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ function _packSolverData(
299299
packtype = convertPackedType(fnctype)
300300
try
301301
packed = convert( PackedFunctionNodeData{packtype}, getSolverData(f) )
302-
packedJson = JSON2.write(packed)
302+
packedJson = packed # JSON2.write(packed) # NOTE SINGLE TOP LEVEL JSON.write ONLY
303303
if base64Encode
304-
# 833
304+
# 833, 848, Neo4jDFG still using base64(JSON2.write(solverdata))...
305+
packedJson = JSON2.write(packed)
305306
packedJson = base64encode(packedJson)
306307
end
307308
return packedJson
@@ -321,13 +322,13 @@ function packFactor(dfg::AbstractDFG, f::DFGFactor)
321322
props["label"] = string(f.label)
322323
props["timestamp"] = Dates.format(f.timestamp, "yyyy-mm-ddTHH:MM:SS.ssszzz")
323324
props["nstime"] = string(f.nstime.value)
324-
props["tags"] = JSON2.write(f.tags)
325+
props["tags"] = String.(f.tags) # JSON2.write(f.tags)
325326
# Pack the node data
326327
fnctype = getSolverData(f).fnc.usrfnc!
327328
props["data"] = _packSolverData(f, fnctype)
328329
# Include the type
329330
props["fnctype"] = String(_getname(fnctype))
330-
props["_variableOrderSymbols"] = JSON2.write(f._variableOrderSymbols)
331+
props["_variableOrderSymbols"] = String.(f._variableOrderSymbols) # JSON2.write(f._variableOrderSymbols)
331332
props["solvable"] = getSolvable(f)
332333
props["_version"] = _getDFGVersion()
333334
return props
@@ -347,6 +348,26 @@ function decodePackedType(dfg::AbstractDFG, varOrder::AbstractVector{Symbol}, ::
347348
return factordata
348349
end
349350

351+
function Base.convert(::Type{PF}, nt::NamedTuple) where {PF <: AbstractPackedFactor}
352+
# Here we define a convention, must provide PackedType(;kw...) constructor, easiest is just use Base.@kwdef
353+
PF(;nt...)
354+
end
355+
356+
function Base.convert(::Type{GenericFunctionNodeData{P}}, nt::NamedTuple) where P
357+
GenericFunctionNodeData{P}(
358+
nt.eliminated,
359+
nt.potentialused,
360+
nt.edgeIDs,
361+
convert(P,nt.fnc),
362+
nt.multihypo,
363+
nt.certainhypo,
364+
nt.nullhypo,
365+
nt.solveInProgress,
366+
nt.inflation,
367+
)
368+
end
369+
370+
350371
# Returns `::DFGFactor`
351372
function unpackFactor(dfg::G, packedProps::Dict{String, Any}) where G <: AbstractDFG
352373
# Version checking.
@@ -367,6 +388,14 @@ function unpackFactor(dfg::G, packedProps::Dict{String, Any}) where G <: Abstrac
367388
tags = Vector{Symbol}()
368389
end
369390
end
391+
392+
# Get the stored variable order
393+
_variableOrderSymbols = if packedProps["_variableOrderSymbols"] isa String
394+
JSON2.read(packedProps["_variableOrderSymbols"], Vector{Symbol})
395+
else
396+
Symbol.(packedProps["_variableOrderSymbols"])
397+
end
398+
370399
data = packedProps["data"]
371400
datatype = packedProps["fnctype"]
372401
@debug "DECODING factor type = '$(datatype)' for factor '$label'"
@@ -376,15 +405,10 @@ function unpackFactor(dfg::G, packedProps::Dict{String, Any}) where G <: Abstrac
376405
packed = nothing
377406
fullFactorData = nothing
378407

379-
# Get the stored variable order
380-
_variableOrderSymbols = if packedProps["_variableOrderSymbols"] isa String
381-
JSON2.read(packedProps["_variableOrderSymbols"], Vector{Symbol})
382-
else
383-
Symbol.(packedProps["_variableOrderSymbols"])
384-
end
385-
408+
# @show packtype
409+
# @show data
386410
try
387-
packed = JSON2.read(data, GenericFunctionNodeData{packtype})
411+
packed = convert(GenericFunctionNodeData{packtype}, data) # JSON2.read(data, GenericFunctionNodeData{packtype})
388412
decodeType = getFactorOperationalMemoryType(dfg)
389413
fullFactorData = decodePackedType(dfg, _variableOrderSymbols, decodeType, packed)
390414
catch ex

test/testBlocks.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ struct TestAbstractPrior <: AbstractPrior end
3434
struct TestAbstractRelativeFactor <: AbstractRelativeRoots end
3535
struct TestAbstractRelativeFactorMinimize <: AbstractRelativeMinimize end
3636

37-
struct PackedTestFunctorInferenceType1 <: AbstractPackedFactor
38-
s::String
37+
Base.@kwdef struct PackedTestFunctorInferenceType1 <: AbstractPackedFactor
38+
s::String = ""
3939
end
40-
PackedTestFunctorInferenceType1() = PackedTestFunctorInferenceType1("")
40+
# PackedTestFunctorInferenceType1() = PackedTestFunctorInferenceType1("")
4141

4242
function Base.convert(::Type{PackedTestFunctorInferenceType1}, d::TestFunctorInferenceType1)
4343
# @info "convert(::Type{PackedTestFunctorInferenceType1}, d::TestFunctorInferenceType1)"
@@ -55,10 +55,10 @@ function Base.convert(::Type{TestFunctorInferenceType1}, d::PackedTestFunctorInf
5555
end
5656

5757

58-
struct PackedTestAbstractPrior <: AbstractPackedFactor
59-
s::String
58+
Base.@kwdef struct PackedTestAbstractPrior <: AbstractPackedFactor
59+
s::String = ""
6060
end
61-
PackedTestAbstractPrior() = PackedTestAbstractPrior("")
61+
# PackedTestAbstractPrior() = PackedTestAbstractPrior("")
6262

6363
function Base.convert(::Type{PackedTestAbstractPrior}, d::TestAbstractPrior)
6464
# @info "convert(::Type{PackedTestAbstractPrior}, d::TestAbstractPrior)"

0 commit comments

Comments
 (0)