Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mcmcgrath13/jsontypeprovider.jl

An F# inspired type provider for JSON3.jl
https://github.com/mcmcgrath13/jsontypeprovider.jl

Last synced: 11 days ago
JSON representation

An F# inspired type provider for JSON3.jl

Awesome Lists containing this project

README

        

# DEPRECATED - Incorporated into [JSON3](https://github.com/quinnj/JSON3.jl)

# JSONTypeProvider

[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://mcmcgrath13.github.io/JSONTypeProvider.jl/dev)
[![Build Status](https://github.com/mcmcgrath13/JSONTypeProvider.jl/workflows/CI/badge.svg)](https://github.com/mcmcgrath13/JSONTypeProvider.jl/actions)
[![Coverage](https://codecov.io/gh/mcmcgrath13/JSONTypeProvider.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/mcmcgrath13/JSONTypeProvider.jl)

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

```julia
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`:

```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:

```julia
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
```