Skip to content

mcmcgrath13/JSONTypeProvider.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DEPRECATED - Incorporated into JSON3

JSONTypeProvider

Dev Build Status Coverage

An F# inspired type provider to JSON. Given a JSON3 Object or Array, create a type from it, and write it to file.

import JSON3
import JSONTypeProvider

json = JSON3.read(read("test/menu.json", String)) # either a JSON3.Array or JSON3.Object

# build a type for the JSON
raw_json_type = JSONTypeProvider.build_type(json)
# result:
# NamedTuple{(:menu,),Tuple{NamedTuple{(:header, :items),Tuple{String,Array{Union{Nothing, NamedTuple{(:id, :label),Tuple{String,Union{Nothing, String}}}},1}}}}}

# turn the type into struct expressions, including replacing sub types with references to a struct
json_exprs = JSONTypeProvider.to_exprs(raw_json_type, :MyStruct)
# result:
# 3-element Array{Any,1}:
# :(struct Item
#      id::String
#      label::Union{Nothing, String}
#  end)
# :(struct Menu
#      header::String
#      items::Array{Union{Nothing, Item}, 1}
#  end)
# :(struct MyStruct
#      menu::Menu
#  end)

# write the types to a file, then can be edited/included as needed
JSONTypeProvider.write_exprs(json_exprs, "test.jl")

In the example above, the file test/menu.json:

{
  "menu": {
    "header": "SVG\\tViewer\\u03b1",
    "items": [
      {
        "id": "Open"
      },
      {
        "id": "OpenNew",
        "label": "Open New"
      },
      {
        "id": "ZoomIn",
        "label": "Zoom In"
      },
      null,
      {
        "id": "Help"
      },
      {
        "id": "About",
        "label": "About Adobe SVG Viewer..."
      }
    ]
  }
}

is parsed into the following types:

struct Item
    id::String
    label::Union{Nothing, String}
end

struct Menu
    header::String
    items::Array{Union{Nothing, Item}, 1}
end

struct MyStruct
    menu::Menu
end

About

An F# inspired type provider for JSON3.jl

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages