Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/mcmcgrath13/jsontypeprovider.jl
- Owner: mcmcgrath13
- License: mit
- Created: 2021-02-19T02:07:41.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-03-06T05:02:47.000Z (over 3 years ago)
- Last Synced: 2024-10-15T07:23:44.810Z (about 1 month ago)
- Language: Julia
- Size: 110 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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 JSONTypeProviderjson = 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}
endstruct Menu
header::String
items::Array{Union{Nothing, Item}, 1}
endstruct MyStruct
menu::Menu
end
```