diff --git a/Project.toml b/Project.toml index 6d627abf2a..147db537d3 100644 --- a/Project.toml +++ b/Project.toml @@ -8,7 +8,7 @@ CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd" CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193" DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" -JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" +JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0" NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" @@ -26,7 +26,7 @@ CodecBzip2 = "~0.6, 0.7, 0.8" CodecZlib = "~0.6, 0.7" DataStructures = "0.18" ForwardDiff = "0.5, 0.6, 0.7, 0.8, 0.9, 0.10" -JSON = "~0.21" +JSON3 = "1" JSONSchema = "1" LinearAlgebra = "<0.0.1, 1.6" MutableArithmetics = "1" diff --git a/docs/src/submodules/FileFormats/overview.md b/docs/src/submodules/FileFormats/overview.md index 3a77f8ee5a..1d75c96c95 100644 --- a/docs/src/submodules/FileFormats/overview.md +++ b/docs/src/submodules/FileFormats/overview.md @@ -114,22 +114,7 @@ MathOptInterface.Utilities.IndexMap with 1 entry: julia> MOI.write_to_file(dest, "file.mof.json") julia> print(read("file.mof.json", String)) -{ - "name": "MathOptFormat Model", - "version": { - "major": 1, - "minor": 7 - }, - "variables": [ - { - "name": "x1" - } - ], - "objective": { - "sense": "feasibility" - }, - "constraints": [] -} +{"name":"MathOptFormat Model","version":{"major":1,"minor":7},"variables":[{"name":"x1"}],"objective":{"sense":"feasibility"},"constraints":[]} ``` ## Read from file diff --git a/src/FileFormats/MOF/MOF.jl b/src/FileFormats/MOF/MOF.jl index f6de179f28..b4dc9772c7 100644 --- a/src/FileFormats/MOF/MOF.jl +++ b/src/FileFormats/MOF/MOF.jl @@ -7,7 +7,7 @@ module MOF import ..FileFormats -import JSON +import JSON3 import MathOptInterface as MOI """ diff --git a/src/FileFormats/MOF/read.jl b/src/FileFormats/MOF/read.jl index 3ae49c735f..ab4efa1ded 100644 --- a/src/FileFormats/MOF/read.jl +++ b/src/FileFormats/MOF/read.jl @@ -13,7 +13,7 @@ function Base.read!(io::IO, model::Model) if !MOI.is_empty(model) error("Cannot read model from file as destination model is not empty.") end - object = JSON.parse(io; dicttype = Dict{String,Any}) + object = JSON3.read(io, Dict{String,Any}) file_version = _parse_mof_version(object["version"]::Dict{String,Any}) if !(file_version in _SUPPORTED_VERSIONS) version = _SUPPORTED_VERSIONS[1] diff --git a/src/FileFormats/MOF/write.jl b/src/FileFormats/MOF/write.jl index f9477e5293..c229751b5c 100644 --- a/src/FileFormats/MOF/write.jl +++ b/src/FileFormats/MOF/write.jl @@ -29,8 +29,7 @@ function Base.write(io::IO, model::Model) objective = objective, constraints = constraints, ) - indent = options.print_compact ? nothing : 2 - Base.write(io, JSON.json(object, indent)) + JSON3.write(io, object) return end diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index 662064cd6b..2263f479e9 100644 --- a/test/FileFormats/MOF/MOF.jl +++ b/test/FileFormats/MOF/MOF.jl @@ -8,7 +8,7 @@ module TestMOF using Test -import JSON +import JSON3 import JSONSchema import MathOptInterface as MOI @@ -16,8 +16,9 @@ const MOF = MOI.FileFormats.MOF const TEST_MOF_FILE = "test.mof.json" -const SCHEMA = - JSONSchema.Schema(JSON.parsefile(MOI.FileFormats.MOF.SCHEMA_PATH)) +const SCHEMA = JSONSchema.Schema( + JSON3.read(read(MOI.FileFormats.MOF.SCHEMA_PATH, String), Dict{String,Any}), +) function runtests() for name in names(@__MODULE__, all = true) @@ -39,7 +40,7 @@ function _validate(filename::String) "r", MOI.FileFormats.AutomaticCompression(), ) do io - object = JSON.parse(io) + object = JSON3.read(io, Dict{String,Any}) ret = JSONSchema.validate(SCHEMA, object) if ret !== nothing error( @@ -112,8 +113,10 @@ function test_HS071() MOI.set(model, MOI.NLPBlock(), HS071(x)) MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) MOI.write_to_file(model, TEST_MOF_FILE) - @test replace(read(TEST_MOF_FILE, String), '\r' => "") == - replace(read(joinpath(@__DIR__, "nlp.mof.json"), String), '\r' => "") + target = read(joinpath(@__DIR__, "nlp.mof.json"), String) + target = replace(target, r"\s" => "") + target = replace(target, "MathOptFormatModel" => "MathOptFormat Model") + @test read(TEST_MOF_FILE, String) == target _validate(TEST_MOF_FILE) return end