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 96 97 98 99 100 101 102 103 104 105 106 107 108
|
# JSON.jl
### Parsing and printing JSON in pure Julia.
[](https://travis-ci.org/JuliaIO/JSON.jl)
[](https://ci.appveyor.com/project/staticfloat/json-jl)
[](http://codecov.io/github/JuliaIO/JSON.jl?branch=master)
[](http://pkg.julialang.org/?pkg=JSON&ver=0.3)
[](http://pkg.julialang.org/?pkg=JSON&ver=0.4)
[](http://pkg.julialang.org/?pkg=JSON&ver=0.5)
[](http://pkg.julialang.org/?pkg=JSON&ver=0.6)
**Installation**: `julia> Pkg.add("JSON")`
## Basic Usage
```julia
import JSON
# JSON.parse - string or stream to Julia data structures
s = "{\"a_number\" : 5.0, \"an_array\" : [\"string\", 9]}"
j = JSON.parse(s)
# Dict{AbstractString,Any} with 2 entries:
# "an_array" => {"string",9}
# "a_number" => 5.0
# JSON.json - Julia data structures to a string
JSON.json([2,3])
# "[2,3]"
JSON.json(j)
# "{\"an_array\":[\"string\",9],\"a_number\":5.0}"
```
## Documentation
```julia
JSON.print(io::IO, s::AbstractString)
JSON.print(io::IO, s::Union{Integer, AbstractFloat})
JSON.print(io::IO, n::Nothing)
JSON.print(io::IO, b::Bool)
JSON.print(io::IO, a::AbstractDict)
JSON.print(io::IO, v::AbstractVector)
JSON.print{T, N}(io::IO, v::Array{T, N})
```
Writes a compact (no extra whitespace or indentation) JSON representation
to the supplied IO.
```julia
JSON.print(a::AbstractDict, indent)
JSON.print(io::IO, a::AbstractDict, indent)
```
Writes a JSON representation with newlines, and indentation if specified. Non-zero `indent` will be applied recursively to nested elements.
```julia
json(a::Any)
```
Returns a compact JSON representation as an `AbstractString`.
```julia
JSON.parse(s::AbstractString; dicttype=Dict, inttype=Int64)
JSON.parse(io::IO; dicttype=Dict, inttype=Int64)
JSON.parsefile(filename::AbstractString; dicttype=Dict, inttype=Int64, use_mmap=true)
```
Parses a JSON `AbstractString` or IO stream into a nested `Array` or `Dict`.
The `dicttype` indicates the dictionary type (`<: Associative`), or a function that
returns an instance of a dictionary type,
that JSON objects are parsed to. It defaults to `Dict` (the built-in Julia
dictionary), but a different type can be passed for additional functionality.
For example, if you `import DataStructures`
(assuming the [DataStructures
package](https://github.com/JuliaLang/DataStructures.jl) is
installed)
- you can pass `dicttype=DataStructures.OrderedDict` to maintain the insertion order
of the items in the object;
- or you can pass `()->DefaultDict{String,Any}(Missing)` to having any non-found keys
return `missing` when you index the result.
The `inttype` argument controls how integers are parsed. If a number in a JSON
file is recognized to be an integer, it is parsed as one; otherwise it is parsed
as a `Float64`. The `inttype` defaults to `Int64`, but, for example, if you know
that your integer numbers are all small and want to save space, you can pass
`inttype=Int32`. Alternatively, if your JSON input has integers which are too large
for Int64, you can pass `inttype=Int128` or `inttype=BigInt`. `inttype` can be any
subtype of `Real`.
```julia
JSONText(s::AbstractString)
```
A wrapper around a Julia string representing JSON-formatted text,
which is inserted *as-is* in the JSON output of `JSON.print` and `JSON.json`.
```julia
JSON.lower(p::Point2D) = [p.x, p.y]
```
Define a custom serialization rule for a particular data type. Must return a
value that can be directly serialized; see help for more details.
|