< Summary

Information
Class: src/jsonpointer.jl
Assembly: Default
File(s): src/jsonpointer.jl
Tag: 61_25545638018
Line coverage
96%
Covered lines: 57
Uncovered lines: 2
Coverable lines: 59
Total lines: 100
Line coverage: 96.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/jsonpointer.jl

#LineLine coverage
 1
 2"""
 3    parse_column_header(token_string)::Pointer
 4
 5Parse an Excel column header into a `JSONPointer.Pointer`.
 6
 7A leading `/` is added if missing, so both `"a/b"` and `"/a/b"` are accepted.
 8A trailing `{jsontype}` suffix declares an array element type and produces a
 9`Pointer{Array{T, 1}}`, where `T` is resolved by `jsontype_to_juliatype`.
 10Without the suffix, a plain (JSON)`Pointer` is returned.
 11
 12# Examples
 13```julia
 14parse_column_header("a/b")           # Pointer
 15parse_column_header("/a/b")          # Pointer
 16parse_column_header("a/b{integer}")  # Pointer{Array{Int, 1}}
 17```
 18"""
 27519function parse_column_header(token_string::AbstractString)::Pointer
 27520    if !startswith(token_string, JSONPointer.TOKEN_PREFIX)
 2021        token_string = "/" * token_string
 22    end
 27523    if endswith(token_string, "}")
 3224        x = split(token_string, "{")
 3225        p = Pointer(x[1])
 3226        T = jsontype_to_juliatype(x[2][1:end-1])
 27
 3128        return Pointer{Array{T, 1}}(p.tokens)
 29    else
 24330        return Pointer(token_string)
 31    end
 32end
 033parse_column_header(token) = parse_column_header(string(token))
 34
 4235function jsontype_to_juliatype(t)
 4236    if t == "string"
 837        return String
 3438    elseif t == "number"
 1039        return Float64
 40    # JSON does not have distinct types for integers and floating-point values
 41    # but Excel does, and distinguishing integer is useful for many things.
 2442    elseif t == "integer"
 1243        return Int
 1244    elseif t == "object"
 245        return OrderedDict{String,Any}
 1046    elseif t == "array"
 247        return Vector{Any}
 848    elseif t == "boolean"
 249        return Bool
 650    elseif t == "null"
 251        return Missing
 52    else
 453        error(
 54            "You specified a type that JSON doesn't recognize! Instead of " *
 55            "`::$t`, you must use one of `::string`, `::number`, " *
 56            "`::object`, `::array`, `::boolean`, or `::null`."
 57        )
 58    end
 59end
 60
 3261function pointer_to_colname(p::Pointer{T})::String where T
 3262    col = "/" * join(p.tokens, "/")
 3263    t = juliatype_to_jsontype(T)
 3264    if t == "array"
 465        col *= "::$t"
 466        t2 = juliatype_to_jsontype(eltype(T))
 467        if !isempty(t2)
 368            col *= "{$t2}"
 69        end
 2870    elseif !isempty(t)
 071        col *= "::$t"
 72    end
 3273    return col
 74end
 75
 4776function juliatype_to_jsontype(T)
 4777    if T <: OrderedDict
 178        t = "object"
 4679    elseif T <: Array
 680        t = "array"
 4081    elseif T == String
 282        t = "string"
 3883    elseif T == Float64
 284        t = "number"
 3685    elseif T == Int
 286        t = "integer"
 3487    elseif T == Bool
 188        t = "boolean"
 3389    elseif T == Missing
 190        t = "null"
 3291    elseif T == Nothing
 192        t = "null"
 3193    elseif T == Any
 3094        t = ""
 95    else
 196        @warn("cannot find jsontype from $T, returning empty string")
 197        t = ""
 98    end
 4799    return t
 100end

Methods/Properties