File: serializer.jl

package info (click to toggle)
julia 1.5.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 91,132 kB
  • sloc: lisp: 278,486; ansic: 60,186; cpp: 29,801; sh: 2,403; makefile: 1,998; pascal: 1,313; objc: 647; javascript: 516; asm: 226; python: 161; xml: 34
file content (95 lines) | stat: -rw-r--r-- 2,218 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
module TestSerializer

using JSON
using Test

# to define a new serialization behaviour, import these first
import JSON.Serializations: CommonSerialization, StandardSerialization
import JSON: StructuralContext

# those names are long so we can define some type aliases
const CS = CommonSerialization
const SC = StructuralContext

# for test harness purposes
function sprint_kwarg(f, args...; kwargs...)
    b = IOBuffer()
    f(b, args...; kwargs...)
    String(take!(b))
end

# issue #168: Print NaN and Inf as Julia would
struct NaNSerialization <: CS end
JSON.show_json(io::SC, ::NaNSerialization, f::AbstractFloat) = Base.print(io, f)

@test sprint(JSON.show_json, NaNSerialization(), [NaN, Inf, -Inf, 0.0]) ==
    "[NaN,Inf,-Inf,0.0]"

@test sprint_kwarg(
    JSON.show_json,
    NaNSerialization(),
    [NaN, Inf, -Inf, 0.0];
    indent=4
) == """
[
    NaN,
    Inf,
    -Inf,
    0.0
]
"""

# issue #170: Print JavaScript functions directly
struct JSSerialization <: CS end
struct JSFunction
    data::String
end

function JSON.show_json(io::SC, ::JSSerialization, f::JSFunction)
    first = true
    for line in split(f.data, '\n')
        if !first
            JSON.indent(io)
        end
        first = false
        Base.print(io, line)
    end
end

@test sprint_kwarg(JSON.show_json, JSSerialization(), Any[
    1,
    2,
    JSFunction("function test() {\n  return 1;\n}")
]; indent=2) == """
[
  1,
  2,
  function test() {
    return 1;
  }
]
"""

# test serializing a type without any fields
struct SingletonType end
@test_throws ErrorException json(SingletonType())

# test printing to stdout
let filename = tempname()
    open(filename, "w") do f
        redirect_stdout(f) do
            JSON.print(Any[1, 2, 3.0])
        end
    end
    @test read(filename, String) == "[1,2,3.0]"
    rm(filename)
end

# issue #184: serializing a 0-dimensional array
@test sprint(JSON.show_json, JSON.StandardSerialization(), view([184], 1)) == "184"

# test serializing with a JSONText object
@test json([JSONText("{\"bar\":  [3,4,5]}"),314159]) == "[{\"bar\":  [3,4,5]},314159]"
@test json([JSONText("{\"bar\":  [3,4,5]}"),314159], 1) == "[\n {\n  \"bar\": [\n   3,\n   4,\n   5\n  ]\n },\n 314159\n]\n"

end