Skip to content

Commit f656813

Browse files
committed
remove custom inf_mappings and add kw inf_mapping to docstring
1 parent 1ce1b28 commit f656813

File tree

2 files changed

+10
-38
lines changed

2 files changed

+10
-38
lines changed

src/write.jl

+6-31
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ Write JSON.
2525
## Keyword Args
2626
2727
* `allow_inf`: Allow writing of `Inf` and `NaN` values (not part of the JSON standard). [default `false`]
28+
* `inf_mapping`: A function to map `Inf`, `-Inf` and `NaN` values to a custom representation. [default `nothing`]
29+
30+
e.g. a quoted version of the default mapping
31+
32+
`quoted_inf_mapping(x) = x == Inf ? "\\"Infinity\\"" : x == -Inf ? "\\"-Infinity\\"" : "\\"NaN\\""`
33+
2834
* `dateformat`: A [`DateFormat`](https://docs.julialang.org/en/v1/stdlib/Dates/#Dates.DateFormat) describing how to format `Date`s in the object. [default `Dates.default_format(T)`]
2935
"""
3036
function write(io::IO, obj::T; kw...) where {T}
@@ -279,37 +285,6 @@ function write(::NumberType, buf, pos, len, x::AbstractFloat; allow_inf::Bool=fa
279285
return buf, pos, len
280286
end
281287

282-
"""
283-
underscore_inf_mapping(x)
284-
285-
This function provides alternative string mappings for `Inf` and `NaN` values.
286-
### Example
287-
288-
```
289-
inf_mapping = JSON3.underscore_inf_mapping
290-
JSON3.write(NaN; inf_mapping) # "\\"__nan__\\""
291-
```
292-
293-
Alternative mappings can easily be defined by the user, e.g. a quoted version of the default mapping:
294-
295-
```
296-
inf_mapping(x) = x == Inf ? "\\"my_infinity\\"" : x == -Inf ? "\\"-my_infinity\\"" : "\\"my_nan\\""
297-
JSON3.write(NaN; inf_mapping) # "\\"my_nan\\""
298-
```
299-
300-
See also [`quoted_inf_mapping`](@ref).
301-
"""
302-
underscore_inf_mapping(x) = x == Inf ? "\"__inf__\"" : x == -Inf ? "\"__neginf__\"" : "\"__nan__\""
303-
304-
"""
305-
quoted_inf_mapping(x)
306-
307-
Provides a quoted version of the default mappings for `Inf` and `NaN` values.
308-
309-
See also [`underscore_inf_mapping`](@ref).
310-
"""
311-
quoted_inf_mapping(x) = x == Inf ? "\"Infinity\"" : x == -Inf ? "\"-Infinity\"" : "\"NaN\""
312-
313288
@inline function write(::NumberType, buf, pos, len, x::T; inf_mapping::Union{Function, Nothing} = nothing, allow_inf::Bool = inf_mapping !== nothing, kw...) where {T <: Base.IEEEFloat}
314289
if isfinite(x) || allow_inf && inf_mapping === nothing && isnan(x)
315290
@check Ryu.neededdigits(T)

test/json.jl

+4-7
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ end
4747
@test JSON3.read("Infinity"; allow_inf=true) === Inf
4848
@test JSON3.read("-Infinity"; allow_inf=true) === -Inf
4949

50-
@test JSON3.write(NaN, inf_mapping = JSON3.underscore_inf_mapping) == "\"__nan__\""
51-
@test JSON3.write(Inf, inf_mapping = JSON3.underscore_inf_mapping) == "\"__inf__\""
52-
@test JSON3.write(-Inf, inf_mapping = JSON3.underscore_inf_mapping) == "\"__neginf__\""
53-
54-
@test JSON3.write(NaN, inf_mapping = JSON3.quoted_inf_mapping) == "\"NaN\""
55-
@test JSON3.write(Inf, inf_mapping = JSON3.quoted_inf_mapping) == "\"Infinity\""
56-
@test JSON3.write(-Inf, inf_mapping = JSON3.quoted_inf_mapping) == "\"-Infinity\""
50+
quoted_inf_mapping(x) = x == Inf ? "\"Infinity\"" : x == -Inf ? "\"-Infinity\"" : "\"NaN\""
51+
@test JSON3.write(NaN, inf_mapping = quoted_inf_mapping) == "\"NaN\""
52+
@test JSON3.write(Inf, inf_mapping = quoted_inf_mapping) == "\"Infinity\""
53+
@test JSON3.write(-Inf, inf_mapping = quoted_inf_mapping) == "\"-Infinity\""
5754
end
5855

5956
@testset "Char" begin

0 commit comments

Comments
 (0)