Skip to content

Commit

Permalink
[FileFormats.MOF] Use JSON3 to write files (#2613)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquimg authored Jan 17, 2025
1 parent 7a081f4 commit 70b3c83
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
17 changes: 1 addition & 16 deletions docs/src/submodules/FileFormats/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/FileFormats/MOF/MOF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module MOF

import ..FileFormats
import JSON
import JSON3
import MathOptInterface as MOI

"""
Expand Down
2 changes: 1 addition & 1 deletion src/FileFormats/MOF/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 1 addition & 2 deletions src/FileFormats/MOF/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 9 additions & 6 deletions test/FileFormats/MOF/MOF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ module TestMOF

using Test

import JSON
import JSON3
import JSONSchema
import MathOptInterface as MOI

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)
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 70b3c83

Please sign in to comment.