Skip to content

Instantly share code, notes, and snippets.

@antimon2
Last active September 7, 2021 00:00
Show Gist options
  • Save antimon2/5115fe7c3d3fe4f2a9461826e78fcc59 to your computer and use it in GitHub Desktop.
Save antimon2/5115fe7c3d3fe4f2a9461826e78fcc59 to your computer and use it in GitHub Desktop.
JSON3.CustomDeserializationSample.jl.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.469Z",
"end_time": "2021-09-07T08:57:12.309000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "versioninfo()",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Julia Version 1.6.2\nCommit 1b93d53fc4 (2021-07-14 15:36 UTC)\nPlatform Info:\n OS: Linux (x86_64-pc-linux-gnu)\n CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\n WORD_SIZE: 64\n LIBM: libopenlibm\n LLVM: libLLVM-11.0.1 (ORCJIT, skylake)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.471Z",
"end_time": "2021-09-07T08:57:12.382000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "# ]activate",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true,
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.473Z",
"end_time": "2021-09-07T08:57:12.394000+09:00"
}
},
"cell_type": "code",
"source": "# ]add JSON3\nusing JSON3",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.474Z",
"end_time": "2021-09-07T08:57:12.482000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "using Dates",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.476Z",
"end_time": "2021-09-07T08:57:12.984000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "dtm1 = \"\\\"2021-09-07T08:15:56.789\\\"\"",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "\"\\\"2021-09-07T08:15:56.789\\\"\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.477Z",
"end_time": "2021-09-07T08:57:13.386000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "JSON3.read(dtm1, DateTime)",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "2021-09-07T08:15:56.789"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.478Z",
"end_time": "2021-09-07T08:57:13.933000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "JSON3.read(dtm1, DateTime; dateformat=dateformat\"yyyy-mm-ddTHH:MM:SS.sss\")",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "2021-09-07T08:15:56.789"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.479Z",
"end_time": "2021-09-07T08:57:13.934000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "src1 = \"\"\"{\n \"date\": \"2021-09-07T08:15:56.789\"\n}\"\"\"",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "\"{\\n \\\"date\\\": \\\"2021-09-07T08:15:56.789\\\"\\n}\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.480Z",
"end_time": "2021-09-07T08:57:17.430000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "json1_1 = JSON3.read(src1)",
"execution_count": 9,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 9,
"data": {
"text/plain": "JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 1 entry:\n :date => \"2021-09-07T08:15:56.789\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.481Z",
"end_time": "2021-09-07T08:57:17.431000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "struct Src1\n date::DateTime\nend",
"execution_count": 10,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.482Z",
"end_time": "2021-09-07T08:57:18.327000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "json1_2 = JSON3.read(src1, Src1)",
"execution_count": 11,
"outputs": [
{
"output_type": "error",
"ename": "LoadError",
"evalue": "ArgumentError: Src1 doesn't have a defined `StructTypes.StructType`",
"traceback": [
"ArgumentError: Src1 doesn't have a defined `StructTypes.StructType`",
"",
"Stacktrace:",
" [1] read(::StructTypes.NoStructType, buf::Base.CodeUnits{UInt8, String}, pos::Int64, len::Int64, b::UInt8, ::Type{Src1}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})",
" @ JSON3 ~/.julia/packages/JSON3/VnoI3/src/structs.jl:49",
" [2] read(::StructTypes.NoStructType, buf::Base.CodeUnits{UInt8, String}, pos::Int64, len::Int64, b::UInt8, ::Type{Src1})",
" @ JSON3 ~/.julia/packages/JSON3/VnoI3/src/structs.jl:49",
" [3] read(str::String, ::Type{Src1}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})",
" @ JSON3 ~/.julia/packages/JSON3/VnoI3/src/structs.jl:34",
" [4] read(str::String, ::Type{Src1})",
" @ JSON3 ~/.julia/packages/JSON3/VnoI3/src/structs.jl:33",
" [5] top-level scope",
" @ In[11]:1",
" [6] eval",
" @ ./boot.jl:360 [inlined]",
" [7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
" @ Base ./loading.jl:1116"
]
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.483Z",
"end_time": "2021-09-07T08:57:18.327000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "# ]add StructTypes\nusing StructTypes",
"execution_count": 12,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.484Z",
"end_time": "2021-09-07T08:57:18.328000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "StructTypes.StructType(::Type{Src1}) = StructTypes.Struct()",
"execution_count": 13,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.485Z",
"end_time": "2021-09-07T08:57:20.575000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "json1_2 = JSON3.read(src1, Src1)",
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 14,
"data": {
"text/plain": "Src1(DateTime(\"2021-09-07T08:15:56.789\"))"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.487Z",
"end_time": "2021-09-07T08:57:20.576000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "src2 = \"\"\"{\n \"dates\": [\n \"2021-09-07T08:15:43.210\",\n \"2112-09-03T12:34:56.789\"\n ]\n}\"\"\"",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "\"{\\n \\\"dates\\\": [\\n \\\"2021-09-07T08:15:43.210\\\",\\n \\\"2112-09-03T12:34:56.789\\\"\\n ]\\n}\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.488Z",
"end_time": "2021-09-07T08:57:20.711000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "json2_1 = JSON3.read(src2)",
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 1 entry:\n :dates => [\"2021-09-07T08:15:43.210\", \"2112-09-03T12:34:56.789\"]"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.490Z",
"end_time": "2021-09-07T08:57:20.712000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "struct Src2\n dates::Vector{DateTime}\nend",
"execution_count": 17,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.491Z",
"end_time": "2021-09-07T08:57:20.713000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "StructTypes.StructType(::Type{Src2}) = StructTypes.Struct()",
"execution_count": 18,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.493Z",
"end_time": "2021-09-07T08:57:21.714000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "json2_2 = JSON3.read(src2, Src2)",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "Src2([DateTime(\"2021-09-07T08:15:43.210\"), DateTime(\"2112-09-03T12:34:56.789\")])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.494Z",
"end_time": "2021-09-07T08:57:21.715000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "abstract type Kinds123 end",
"execution_count": 20,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.495Z",
"end_time": "2021-09-07T08:57:22.130000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "Kinds123(x::Integer) = Kinds123(Val(x%Int), x)\nKinds123(x) = Kinds123(Val(Symbol(x)), x)",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "Kinds123"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.496Z",
"end_time": "2021-09-07T08:57:22.136000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "struct Kind1 <: Kinds123 end\nKinds123(::Union{Val{1}, Val{Symbol(\"1\")}}, @nospecialize(x)) = Kind1()",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": "Kinds123"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.497Z",
"end_time": "2021-09-07T08:57:22.138000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "struct Kind2 <: Kinds123 end\nKinds123(::Union{Val{2}, Val{Symbol(\"2\")}}, @nospecialize(x)) = Kind2()",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 23,
"data": {
"text/plain": "Kinds123"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.498Z",
"end_time": "2021-09-07T08:57:22.139000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "struct Kind3 <: Kinds123 end\nKinds123(::Union{Val{3}, Val{Symbol(\"3\")}}, @nospecialize(x)) = Kind3()",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 24,
"data": {
"text/plain": "Kinds123"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.499Z",
"end_time": "2021-09-07T08:57:22.142000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "struct KindOther{T} <: Kinds123\n value::T\nend\nKinds123(::Val, x::T) where {T} = KindOther{T}(x)",
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 25,
"data": {
"text/plain": "Kinds123"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.500Z",
"end_time": "2021-09-07T08:57:23.416000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "[Kinds123(v) for v in (1, \"1\", 2, '2', 3, 0x03, 4, \"other\")]",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "8-element Vector{Kinds123}:\n Kind1()\n Kind1()\n Kind2()\n Kind2()\n Kind3()\n Kind3()\n KindOther{Int64}(4)\n KindOther{String}(\"other\")"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.501Z",
"end_time": "2021-09-07T08:57:23.417000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "StructTypes.StructType(::Type{Kinds123}) = StructTypes.Struct()\n# StructTypes.StructType(::Type{Kinds123}) = StructTypes.CustomStruct()",
"execution_count": 27,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.502Z",
"end_time": "2021-09-07T08:57:24.097000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "JSON3.read(\"1\", Kinds123)",
"execution_count": 28,
"outputs": [
{
"output_type": "error",
"ename": "LoadError",
"evalue": "ArgumentError: invalid JSON at byte position 1 while parsing type Kinds123: ExpectedOpeningObjectChar\n1\n",
"traceback": [
"ArgumentError: invalid JSON at byte position 1 while parsing type Kinds123: ExpectedOpeningObjectChar\n1\n",
"",
"Stacktrace:",
" [1] invalid(error::JSON3.Error, buf::Base.CodeUnits{UInt8, String}, pos::Int64, T::Type)",
" @ JSON3 ~/.julia/packages/JSON3/VnoI3/src/JSON3.jl:29",
" [2] #read#43",
" @ ~/.julia/packages/JSON3/VnoI3/src/structs.jl:586 [inlined]",
" [3] read",
" @ ~/.julia/packages/JSON3/VnoI3/src/structs.jl:505 [inlined]",
" [4] read(str::String, ::Type{Kinds123}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})",
" @ JSON3 ~/.julia/packages/JSON3/VnoI3/src/structs.jl:34",
" [5] read(str::String, ::Type{Kinds123})",
" @ JSON3 ~/.julia/packages/JSON3/VnoI3/src/structs.jl:33",
" [6] top-level scope",
" @ In[28]:1",
" [7] eval",
" @ ./boot.jl:360 [inlined]",
" [8] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
" @ Base ./loading.jl:1116"
]
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.503Z",
"end_time": "2021-09-07T08:57:24.098000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "# StructTypes.StructType(::Type{Kinds123}) = StructTypes.Struct()\nStructTypes.StructType(::Type{Kinds123}) = StructTypes.CustomStruct()",
"execution_count": 29,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.504Z",
"end_time": "2021-09-07T08:57:24.098000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "# StructTypes.construct(::Type{<:Kinds123}, x) = Kinds123(x)",
"execution_count": 30,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.505Z",
"end_time": "2021-09-07T08:57:24.709000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "JSON3.read(\"1\", Kinds123)",
"execution_count": 31,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 31,
"data": {
"text/plain": "Kind1()"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.506Z",
"end_time": "2021-09-07T08:57:25.417000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "JSON3.read(\"\"\"[1, \"1\", 2, \"2\", 3, \"3\", 4, \"other\"]\"\"\", Vector{Kinds123})",
"execution_count": 32,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 32,
"data": {
"text/plain": "8-element Vector{Kinds123}:\n Kind1()\n Kind1()\n Kind2()\n Kind2()\n Kind3()\n Kind3()\n KindOther{Int64}(4)\n KindOther{String}(\"other\")"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.510Z",
"end_time": "2021-09-07T08:57:25.418000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "src3 = \"\"\"{\n \"kinds\": [\n 1,\n \"1\",\n 2,\n \"2\",\n 3,\n \"3\",\n 4,\n \"other\"\n ]\n}\"\"\"",
"execution_count": 33,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 33,
"data": {
"text/plain": "\"{\\n \\\"kinds\\\": [\\n 1,\\n \\\"1\\\",\\n 2,\\n \\\"2\\\",\\n 3,\\n \\\"3\\\",\\n 4,\\n \\\"other\\\"\\n ]\\n}\""
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.511Z",
"end_time": "2021-09-07T08:57:25.628000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "json3_1 = JSON3.read(src3)",
"execution_count": 34,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 34,
"data": {
"text/plain": "JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}} with 1 entry:\n :kinds => Any[1, \"1\", 2, \"2\", 3, \"3\", 4, \"other\"]"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.512Z",
"end_time": "2021-09-07T08:57:25.630000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "struct Src3_2\n kinds::Vector{Kinds123}\nend",
"execution_count": 35,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.514Z",
"end_time": "2021-09-07T08:57:25.631000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "StructTypes.StructType(::Type{Src3_2}) = StructTypes.Struct()",
"execution_count": 36,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2021-09-06T23:57:12.515Z",
"end_time": "2021-09-07T08:57:27.325000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "json3_1 = JSON3.read(src3, Src3_2)",
"execution_count": 37,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 37,
"data": {
"text/plain": "Src3_2(Kinds123[Kind1(), Kind1(), Kind2(), Kind2(), Kind3(), Kind3(), KindOther{Int64}(4), KindOther{String}(\"other\")])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.6",
"display_name": "Julia 1.6.2",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.6.2"
},
"gist": {
"id": "",
"data": {
"description": "JSON3.CustomDeserializationSample.jl.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
[compat]
julia = "1.6"
[deps]
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
StructTypes = "856f2bd8-1eba-4b0a-8007-ebc267875bd4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment