Skip to content

Inconsistent conversion of LazyJSON.Object to Union type #11

@mstetson

Description

@mstetson

I'm pretty new to Julia, so I'm not sure whether this is a bug in LazyJSON, a bug in Julia itself, or a bug in my understanding. I'm trying to save some values from a JSON document into a struct type that includes a Union{LazyJSON.Object, Nothing}. Depending on exactly how I call the constructor, Julia may try to call convert to convert the LazyJSON.Object to the union type. If that happens, the method that gets called is an @generated method for LazyJSON's struct conversion, which fails trying to call fieldnames() on the union type.

Here's a demonstration of the problem with LazyJSON 0.1.1 and Julia 1.1.0.

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.1.0 (2019-01-21)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> import LazyJSON

julia> struct T s::String; u::Union{LazyJSON.Object,Nothing} end

julia> T(LazyJSON.value("\"foo\""), nothing)
T("foo", nothing)

julia> T("foo", LazyJSON.value("{\"foo\":\"bar\"}"))
T("foo", LazyJSON.Object{Nothing,String}("foo"=>"bar"))

julia> T(LazyJSON.value("\"foo\""), LazyJSON.value("{\"foo\":\"bar\"}"))
ERROR: MethodError: no method matching fieldnames(::Type{Union{Nothing, Object}})
Closest candidates are:
  fieldnames(::Core.TypeofBottom) at reflection.jl:175
  fieldnames(::Type{#s21} where #s21<:Tuple) at reflection.jl:177
  fieldnames(::DataType) at reflection.jl:172
  ...
Stacktrace:
 [1] #s71#28(::Any, ::Any, ::Any, ::Any) at /Users/micah/.julia/packages/LazyJSON/EmafJ/src/AbstractDict.jl:82
 [2] (::Core.GeneratedFunctionStub)(::Any, ::Vararg{Any,N} where N) at ./boot.jl:522
 [3] T(::LazyJSON.String{String}, ::LazyJSON.Object{Nothing,String}) at ./REPL[4]:1
 [4] top-level scope at none:0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions