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