| | 1 | | const TOKEN_PREFIX = '/' |
| | 2 | |
|
| | 3 | | macro j_str(token) |
| 94 | 4 | | Pointer(token) |
| | 5 | | end |
| | 6 | |
|
| | 7 | | """ |
| | 8 | | _unescape_jpath(raw::String) |
| | 9 | |
|
| | 10 | | Transform escaped characters in JPaths back to their original value. |
| | 11 | | https://tools.ietf.org/html/rfc6901 |
| | 12 | | """ |
| | 13 | | function _unescape_jpath(raw::AbstractString) |
| 13 | 14 | | m = match(r"%([0-9A-F]{2})", raw) |
| 13 | 15 | | if m !== nothing |
| 6 | 16 | | for c in m.captures |
| 12 | 17 | | raw = replace(raw, "%$(c)" => Char(parse(UInt8, "0x$(c)"))) |
| | 18 | | end |
| | 19 | | end |
| 13 | 20 | | return raw |
| | 21 | | end |
| | 22 | |
|
| | 23 | | function _last_element_to_type!(jk) |
| 97 | 24 | | if !occursin("::", jk[end]) |
| 86 | 25 | | return Any |
| | 26 | | end |
| 11 | 27 | | x = split(jk[end], "::") |
| 11 | 28 | | jk[end] = String(x[1]) |
| 11 | 29 | | if x[2] == "string" |
| 1 | 30 | | return String |
| 10 | 31 | | elseif x[2] == "number" |
| 1 | 32 | | return Union{Int, Float64} |
| 9 | 33 | | elseif x[2] == "object" |
| 2 | 34 | | return OrderedCollections.OrderedDict{String, Any} |
| 7 | 35 | | elseif x[2] == "array" |
| 2 | 36 | | return Vector{Any} |
| 5 | 37 | | elseif x[2] == "boolean" |
| 2 | 38 | | return Bool |
| 3 | 39 | | elseif x[2] == "null" |
| 1 | 40 | | return Missing |
| | 41 | | else |
| 2 | 42 | | error( |
| | 43 | | "You specified a type that JSON doesn't recognize! Instead of " * |
| | 44 | | "`::$(x[2])`, you must use one of `::string`, `::number`, " * |
| | 45 | | "`::object`, `::array`, `::boolean`, or `::null`." |
| | 46 | | ) |
| | 47 | | end |
| | 48 | | end |
| | 49 | |
|
| | 50 | | """ |
| | 51 | | Pointer(token::AbstractString; shift_index::Bool = false) |
| | 52 | |
|
| | 53 | | A JSON Pointer is a Unicode string containing a sequence of zero or more |
| | 54 | | reference tokens, each prefixed by a '/' (%x2F) character. |
| | 55 | |
|
| | 56 | | Follows IETF JavaScript Object Notation (JSON) Pointer https://tools.ietf.org/html/rfc6901. |
| | 57 | |
|
| | 58 | | ## Arguments |
| | 59 | |
|
| | 60 | | - `shift_index`: shift given index by 1 for compatibility with original JSONPointer. |
| | 61 | |
|
| | 62 | | ## Non-standard extensions |
| | 63 | |
|
| | 64 | | - Index numbers starts from `1` instead of `0` |
| | 65 | |
|
| | 66 | | - User can declare type with '::T' notation at the end. For example |
| | 67 | | `/foo::string`. The type `T` must be one of the six types supported by JSON: |
| | 68 | | * `::string` |
| | 69 | | * `::number` |
| | 70 | | * `::object` |
| | 71 | | * `::array` |
| | 72 | | * `::boolean` |
| | 73 | | * `::null` |
| | 74 | |
|
| | 75 | | ## Examples |
| | 76 | |
|
| | 77 | | Pointer("/a") |
| | 78 | | Pointer("/a/3") |
| | 79 | | Pointer("/a/b/c::number") |
| | 80 | | Pointer("/a/0/c::object"; shift_index = true) |
| | 81 | | """ |
| | 82 | | struct Pointer{T} |
| 105 | 83 | | tokens::Vector{Union{String, Int}} |
| | 84 | | end |
| | 85 | |
|
| | 86 | | function Pointer(token_string::AbstractString; shift_index::Bool = false) |
| 310 | 87 | | if startswith(token_string, "#") |
| 25 | 88 | | token_string = _unescape_jpath(token_string[2:end]) |
| | 89 | | end |
| 104 | 90 | | if isempty(token_string) |
| 3 | 91 | | return Pointer{Nothing}([""]) |
| | 92 | | end |
| 202 | 93 | | if !startswith(token_string, TOKEN_PREFIX) |
| 1 | 94 | | throw(ArgumentError("JSONPointer must starts with '$TOKEN_PREFIX' prefix")) |
| | 95 | | end |
| 197 | 96 | | tokens = convert( |
| | 97 | | Vector{Union{String, Int}}, |
| | 98 | | String.(split(token_string, TOKEN_PREFIX; keepempty = false)), |
| | 99 | | ) |
| 100 | 100 | | if length(tokens) == 0 |
| 3 | 101 | | return Pointer{Any}([""]) |
| | 102 | | end |
| 97 | 103 | | T = _last_element_to_type!(tokens) |
| 190 | 104 | | for (i, token) in enumerate(tokens) |
| 217 | 105 | | if occursin(r"^\d+$", token) # index of a array |
| 51 | 106 | | tokens[i] = parse(Int, token) |
| 51 | 107 | | if shift_index |
| 3 | 108 | | tokens[i] += 1 |
| | 109 | | end |
| 51 | 110 | | if iszero(tokens[i]) |
| 2 | 111 | | throw(ArgumentError("Julia uses 1-based indexing, use '1' instead of '0'")) |
| | 112 | | end |
| 166 | 113 | | elseif occursin(r"^\\\d+$", token) # literal string for a number |
| 3 | 114 | | tokens[i] = String(chop(token; head = 1, tail = 0)) |
| 163 | 115 | | elseif occursin("~", token) |
| 343 | 116 | | tokens[i] = replace(replace(token, "~0" => "~"), "~1" => "/") |
| | 117 | | end |
| | 118 | | end |
| 93 | 119 | | return Pointer{T}(tokens) |
| | 120 | | end |
| | 121 | |
|
| 66 | 122 | | Base.length(x::Pointer) = length(x.tokens) |
| | 123 | |
|
| 16 | 124 | | Base.eltype(::Pointer{T}) where {T} = T |
| | 125 | |
|
| 0 | 126 | | function Base.show(io::IO, x::Pointer{T}) where {T} |
| 0 | 127 | | print(io, "JSONPointer{", T, "}(\"/", join(x.tokens, "/"), "\")") |
| | 128 | | end |
| | 129 | |
|
| 0 | 130 | | function Base.show(io::IO, ::Pointer{Nothing}) |
| 0 | 131 | | print(io, "JSONPointer{Nothing}(\"\")") |
| | 132 | | end |
| | 133 | |
|
| | 134 | | # This code block needs some explaining. |
| | 135 | | # |
| | 136 | | # Ideally, one would define methods like Base.haskey(::AbstractDict, ::Pointer). |
| | 137 | | # However, this causes an ambiguity with Base.haskey(::Dict, key), which has a |
| | 138 | | # more concrete first argument and a less concrete second argument. We could |
| | 139 | | # just define both methods to avoid the ambiguity with Dict, but this would |
| | 140 | | # probably break any package which defines an <:AbstractDict and fails to type |
| | 141 | | # the second argument to haskey, getindex, etc! |
| | 142 | | # |
| | 143 | | # To avoid the ambiguity issue, we have to manually encode each AbstractDict |
| | 144 | | # subtype that we support :( |
| | 145 | | for T in (Dict, OrderedCollections.OrderedDict) |
| | 146 | | @eval begin |
| | 147 | | # This method is used when creating new dictionaries from JSON pointers. |
| | 148 | | function $T{K, V}(kv::Pair{<:Pointer, V}...) where {V, K<:Pointer} |
| 1 | 149 | | return $T{String, Any}() |
| | 150 | | end |
| | 151 | |
|
| 8 | 152 | | _new_container(::$T) = $T{String, Any}() |
| | 153 | |
|
| 20 | 154 | | Base.haskey(dict::$T, p::Pointer) = _haskey(dict, p) |
| 51 | 155 | | Base.getindex(dict::$T, p::Pointer) = _getindex(dict, p) |
| 21 | 156 | | Base.setindex!(dict::$T, v, p::Pointer) = _setindex!(dict, v, p) |
| 3 | 157 | | Base.get(dict::$T, p::Pointer, default) = _get(dict, p, default) |
| | 158 | | end |
| | 159 | | end |
| | 160 | |
|
| 7 | 161 | | Base.getindex(A::AbstractArray, p::Pointer) = _getindex(A, p) |
| 2 | 162 | | Base.haskey(A::AbstractArray, p::Pointer) = _haskey(A, p) |
| | 163 | |
|
| | 164 | | function Base.unique(arr::AbstractArray{<:Pointer, N}) where {N} |
| 1 | 165 | | out = deepcopy(arr) |
| 1 | 166 | | if isempty(arr) |
| 0 | 167 | | return out |
| | 168 | | end |
| 2 | 169 | | pointers = getfield.(arr, :tokens) |
| 1 | 170 | | if allunique(pointers) |
| 0 | 171 | | return out |
| | 172 | | end |
| 1 | 173 | | delete_target = Int[] |
| 1 | 174 | | for p in pointers |
| 3 | 175 | | indicies = findall(el -> el == p, pointers) |
| 3 | 176 | | if length(indicies) > 1 |
| 3 | 177 | | append!(delete_target, indicies[1:end-1]) |
| | 178 | | end |
| | 179 | | end |
| 1 | 180 | | deleteat!(out, unique(delete_target)) |
| 1 | 181 | | return out |
| | 182 | | end |
| | 183 | |
|
| 1 | 184 | | Base.:(==)(a::Pointer{U}, b::Pointer{U}) where {U} = a.tokens == b.tokens |
| | 185 | |
|
| | 186 | | # ============================================================================== |
| | 187 | |
|
| 41 | 188 | | _checked_get(collection::AbstractArray, token::Int) = collection[token] |
| | 189 | |
|
| 156 | 190 | | _checked_get(collection::AbstractDict, token::String) = collection[token] |
| | 191 | |
|
| | 192 | | function _checked_get(collection, token) |
| 1 | 193 | | error( |
| | 194 | | "JSON pointer does not match the data-structure. I tried (and " * |
| | 195 | | "failed) to index $(collection) with the key: $(token)" |
| | 196 | | ) |
| | 197 | | end |
| | 198 | |
|
| | 199 | | # ============================================================================== |
| | 200 | |
|
| 1 | 201 | | _haskey(::Any, ::Pointer{Nothing}) = true |
| | 202 | |
|
| | 203 | | function _haskey(collection, p::Pointer) |
| 24 | 204 | | for token in p.tokens |
| 132 | 205 | | if !_haskey(collection, token) |
| 6 | 206 | | return false |
| | 207 | | end |
| 79 | 208 | | collection = _checked_get(collection, token) |
| | 209 | | end |
| 18 | 210 | | return true |
| | 211 | | end |
| | 212 | |
|
| 57 | 213 | | _haskey(collection::AbstractDict, token::String) = haskey(collection, token) |
| | 214 | |
|
| | 215 | | function _haskey(collection::AbstractArray, token::Int) |
| 10 | 216 | | return 1 <= token <= length(collection) |
| | 217 | | end |
| | 218 | |
|
| 0 | 219 | | _haskey(::Any, ::Any) = false |
| | 220 | |
|
| | 221 | | # ============================================================================== |
| | 222 | |
|
| 2 | 223 | | _getindex(collection, ::Pointer{Nothing}) = collection |
| | 224 | |
|
| | 225 | | function _getindex(collection, p::Pointer) |
| 57 | 226 | | return _getindex(collection, p.tokens) |
| | 227 | | end |
| | 228 | |
|
| | 229 | | function _getindex(collection, tokens::Vector{Union{String, Int}}) |
| 57 | 230 | | for token in tokens |
| 189 | 231 | | collection = _checked_get(collection, token) |
| | 232 | | end |
| 52 | 233 | | return collection |
| | 234 | | end |
| | 235 | |
|
| | 236 | | # ============================================================================== |
| | 237 | |
|
| | 238 | | function _get(collection, p::Pointer, default) |
| 3 | 239 | | if _haskey(collection, p) |
| 1 | 240 | | return _getindex(collection, p) |
| | 241 | | end |
| 2 | 242 | | return default |
| | 243 | | end |
| | 244 | |
|
| | 245 | | # ============================================================================== |
| | 246 | |
|
| 5 | 247 | | _null_value(p::Pointer) = _null_value(eltype(p)) |
| 1 | 248 | | _null_value(::Type{String}) = "" |
| 1 | 249 | | _null_value(::Type{<:Real}) = 0 |
| 1 | 250 | | _null_value(::Type{<:AbstractDict}) = OrderedCollections.OrderedDict{String, Any}() |
| 1 | 251 | | _null_value(::Type{<:AbstractVector{T}}) where {T} = T[] |
| 1 | 252 | | _null_value(::Type{Bool}) = false |
| 0 | 253 | | _null_value(::Type{Nothing}) = nothing |
| 0 | 254 | | _null_value(::Type{Missing}) = missing |
| | 255 | |
|
| 24 | 256 | | _null_value(::Type{Any}) = missing |
| | 257 | |
|
| 17 | 258 | | _convert_v(v::U, ::Pointer{U}) where {U} = v |
| | 259 | | function _convert_v(v::V, p::Pointer{U}) where {U, V} |
| 8 | 260 | | v = ismissing(v) ? _null_value(p) : v |
| 8 | 261 | | try |
| 8 | 262 | | return convert(eltype(p), v) |
| | 263 | | catch |
| 2 | 264 | | error( |
| | 265 | | "$(v)::$(typeof(v)) is not valid type for $(p). Remove type " * |
| | 266 | | "assertion in the JSON pointer if you don't a need static type." |
| | 267 | | ) |
| | 268 | | end |
| | 269 | | end |
| | 270 | |
|
| | 271 | | function _add_element_if_needed(prev::AbstractVector{T}, k::Int) where {T} |
| 22 | 272 | | x = k - length(prev) |
| 22 | 273 | | if x > 0 |
| 22 | 274 | | append!(prev, [_null_value(T) for _ = 1:x]) |
| | 275 | | end |
| 22 | 276 | | return |
| | 277 | | end |
| | 278 | |
|
| | 279 | | function _add_element_if_needed( |
| | 280 | | prev::AbstractDict{K, V}, k::String |
| | 281 | | ) where {K, V} |
| 44 | 282 | | if !haskey(prev, k) |
| 25 | 283 | | prev[k] = _null_value(V) |
| | 284 | | end |
| | 285 | | end |
| | 286 | |
|
| | 287 | | function _add_element_if_needed(collection, token) |
| 3 | 288 | | error( |
| | 289 | | "JSON pointer does not match the data-structure. I tried (and " * |
| | 290 | | "failed) to set $(collection) at the index: $(token)" |
| | 291 | | ) |
| | 292 | | end |
| | 293 | |
|
| 9 | 294 | | _new_data(::Any, n::Int) = Vector{Any}(missing, n) |
| 4 | 295 | | _new_data(::AbstractVector, ::String) = OrderedCollections.OrderedDict{String, Any}() |
| 8 | 296 | | _new_data(x::AbstractDict, ::String) = _new_container(x) |
| | 297 | |
|
| | 298 | | function _setindex!(collection::AbstractDict, v, p::Pointer) |
| 28 | 299 | | prev = collection |
| 56 | 300 | | for (i, token) in enumerate(p.tokens) |
| 71 | 301 | | _add_element_if_needed(prev, token) |
| 66 | 302 | | if i != length(p) |
| 61 | 303 | | if ismissing(prev[token]) |
| 21 | 304 | | prev[token] = _new_data(prev, p.tokens[i + 1]) |
| | 305 | | end |
| 82 | 306 | | prev = prev[token] |
| | 307 | | end |
| | 308 | | end |
| 25 | 309 | | prev[p.tokens[end]] = _convert_v(v, p) |
| 23 | 310 | | return v |
| | 311 | | end |
| | 312 | |
|