I'm trying to write a JSON3StructTypes interface to the JSON created by Pandoc.
For a Markdown file like this:
# Pandoc.jl
[![Build Status](https://travis-ci.com/kdheepak/Pandoc.jl.svg?branch=master)](https://travis-ci.com/kdheepak/Pandoc.jl)
[![Documentation](https://img.shields.io/badge/docs-ready-blue.svg)](https://kdheepak.github.io/Pandoc.jl/stable)
[Pandoc.jl](https://github.com/kdheepak/Pandoc.jl) is a package to make it easier to write filters for [Pandoc](https://github.com/jgm/pandoc) in Julia.
## Install
To install Pandoc.jl, open the Julia package manager prompt and type:
```julia
(v1.8) pkg> add Pandoc
Quick Start
julia> using Pandoc, FilePaths, Test
julia> doc = Pandoc.Document(p"./test/data/writer.markdown");
julia> @test doc.pandoc_api_version == v"1.23"
julia> @test length(doc.blocks) == 239
In Julia, I'm representing this Space type like so:
"""
Inter-word space
"""
struct Space <: Inline end
and the Link type like so:
"""
Link target (URL, title).
"""
Base.@kwdef struct Target
url::Text = ""
title::Text = ""
end
Base.@kwdef struct Link <: Inline
attr::Attr = Attr()
content::Vector{Inline} = []
target::Target = Target()
end
The example in the document suggests that I have to do something like this:
struct Person
id::Int
name::String
end
StructTypes.StructType(::Type{Person}) = StructTypes.Struct()
struct PersonWrapper
person::Person
end
StructTypes.StructType(::Type{PersonWrapper}) = StructTypes.CustomStruct()
StructTypes.lower(x::PersonWrapper) = x.person
StructTypes.lowertype(::Type{PersonWrapper}) = Person
StructTypes.construct(::Type{PersonWrapper}, x::Person) = PersonWrapper(x)
To have something like JSON3.read(pandoc_json, Document), it appears that I need to write Wrapper types for every one of the Julia types I have mentioned above (35 structs), which seems like a lot of code to write and maintain just for a wrapper type.
Is there an alternative to using wrapper types? If not, should there be a macro of some kind to support this functionality for the package.
For comparison, in Rust you can use serde(tag = "t", content = "c") as a procedural macro that automatically solve this kind of problem for you.
Thanks for writing and maintaining this package!
I'm trying to write a
JSON3
StructTypes
interface to theJSON
created by Pandoc.For a Markdown file like this:
Quick Start
There's 15
Block
types:And 20
Inline
types:In the JSON, every type is represented by a tag under the
t
key, and the content under ac
key.For example, this represents the inline
Space
.Or this represents a
Link
:In Julia, I'm representing this
Space
type like so:and the
Link
type like so:The example in the document suggests that I have to do something like this:
To have something like
JSON3.read(pandoc_json, Document)
, it appears that I need to writeWrapper
types for every one of the Julia types I have mentioned above (35 structs), which seems like a lot of code to write and maintain just for a wrapper type.Is there an alternative to using wrapper types? If not, should there be a macro of some kind to support this functionality for the package.
For comparison, in Rust you can use
serde(tag = "t", content = "c")
as a procedural macro that automatically solve this kind of problem for you.Also, (maybe somewhat unrelated), in Julia I'm using Enums. Is that possible to serialize and deserialize from json too?