Skip to content

Commit 70b3c83

Browse files
authored
[FileFormats.MOF] Use JSON3 to write files (#2613)
1 parent 7a081f4 commit 70b3c83

File tree

6 files changed

+15
-28
lines changed

6 files changed

+15
-28
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd"
88
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
99
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1010
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
11-
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
11+
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"
1212
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1313
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
1414
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
@@ -26,7 +26,7 @@ CodecBzip2 = "~0.6, 0.7, 0.8"
2626
CodecZlib = "~0.6, 0.7"
2727
DataStructures = "0.18"
2828
ForwardDiff = "0.5, 0.6, 0.7, 0.8, 0.9, 0.10"
29-
JSON = "~0.21"
29+
JSON3 = "1"
3030
JSONSchema = "1"
3131
LinearAlgebra = "<0.0.1, 1.6"
3232
MutableArithmetics = "1"

docs/src/submodules/FileFormats/overview.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,7 @@ MathOptInterface.Utilities.IndexMap with 1 entry:
114114
julia> MOI.write_to_file(dest, "file.mof.json")
115115
116116
julia> print(read("file.mof.json", String))
117-
{
118-
"name": "MathOptFormat Model",
119-
"version": {
120-
"major": 1,
121-
"minor": 7
122-
},
123-
"variables": [
124-
{
125-
"name": "x1"
126-
}
127-
],
128-
"objective": {
129-
"sense": "feasibility"
130-
},
131-
"constraints": []
132-
}
117+
{"name":"MathOptFormat Model","version":{"major":1,"minor":7},"variables":[{"name":"x1"}],"objective":{"sense":"feasibility"},"constraints":[]}
133118
```
134119

135120
## Read from file

src/FileFormats/MOF/MOF.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
module MOF
88

99
import ..FileFormats
10-
import JSON
10+
import JSON3
1111
import MathOptInterface as MOI
1212

1313
"""

src/FileFormats/MOF/read.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function Base.read!(io::IO, model::Model)
1313
if !MOI.is_empty(model)
1414
error("Cannot read model from file as destination model is not empty.")
1515
end
16-
object = JSON.parse(io; dicttype = Dict{String,Any})
16+
object = JSON3.read(io, Dict{String,Any})
1717
file_version = _parse_mof_version(object["version"]::Dict{String,Any})
1818
if !(file_version in _SUPPORTED_VERSIONS)
1919
version = _SUPPORTED_VERSIONS[1]

src/FileFormats/MOF/write.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ function Base.write(io::IO, model::Model)
2929
objective = objective,
3030
constraints = constraints,
3131
)
32-
indent = options.print_compact ? nothing : 2
33-
Base.write(io, JSON.json(object, indent))
32+
JSON3.write(io, object)
3433
return
3534
end
3635

test/FileFormats/MOF/MOF.jl

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ module TestMOF
88

99
using Test
1010

11-
import JSON
11+
import JSON3
1212
import JSONSchema
1313
import MathOptInterface as MOI
1414

1515
const MOF = MOI.FileFormats.MOF
1616

1717
const TEST_MOF_FILE = "test.mof.json"
1818

19-
const SCHEMA =
20-
JSONSchema.Schema(JSON.parsefile(MOI.FileFormats.MOF.SCHEMA_PATH))
19+
const SCHEMA = JSONSchema.Schema(
20+
JSON3.read(read(MOI.FileFormats.MOF.SCHEMA_PATH, String), Dict{String,Any}),
21+
)
2122

2223
function runtests()
2324
for name in names(@__MODULE__, all = true)
@@ -39,7 +40,7 @@ function _validate(filename::String)
3940
"r",
4041
MOI.FileFormats.AutomaticCompression(),
4142
) do io
42-
object = JSON.parse(io)
43+
object = JSON3.read(io, Dict{String,Any})
4344
ret = JSONSchema.validate(SCHEMA, object)
4445
if ret !== nothing
4546
error(
@@ -112,8 +113,10 @@ function test_HS071()
112113
MOI.set(model, MOI.NLPBlock(), HS071(x))
113114
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
114115
MOI.write_to_file(model, TEST_MOF_FILE)
115-
@test replace(read(TEST_MOF_FILE, String), '\r' => "") ==
116-
replace(read(joinpath(@__DIR__, "nlp.mof.json"), String), '\r' => "")
116+
target = read(joinpath(@__DIR__, "nlp.mof.json"), String)
117+
target = replace(target, r"\s" => "")
118+
target = replace(target, "MathOptFormatModel" => "MathOptFormat Model")
119+
@test read(TEST_MOF_FILE, String) == target
117120
_validate(TEST_MOF_FILE)
118121
return
119122
end

0 commit comments

Comments
 (0)