From fc91c0b4898a94d5c132d459904bacf04da1623e Mon Sep 17 00:00:00 2001 From: joaquimg Date: Thu, 16 Jan 2025 13:32:35 -0800 Subject: [PATCH 1/8] [FileFormats.MOF] Use JSON3 to write files --- Project.toml | 1 + src/FileFormats/MOF/write.jl | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 6d627abf2a..7e9b0332a3 100644 --- a/Project.toml +++ b/Project.toml @@ -9,6 +9,7 @@ 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" diff --git a/src/FileFormats/MOF/write.jl b/src/FileFormats/MOF/write.jl index f9477e5293..76ebee0a68 100644 --- a/src/FileFormats/MOF/write.jl +++ b/src/FileFormats/MOF/write.jl @@ -3,6 +3,7 @@ # # Use of this source code is governed by an MIT-style license that can be found # in the LICENSE.md file or at https://opensource.org/licenses/MIT. +using JSON3 """ Base.write(io::IO, model::FileFormats.MOF.Model) @@ -29,8 +30,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)) + Base.write(io, JSON3.write(object)) return end From b801db6fcbc4511b5dbb8bb011899495a643417e Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 17 Jan 2025 13:06:11 +1300 Subject: [PATCH 2/8] Update --- Project.toml | 1 + src/FileFormats/MOF/MOF.jl | 2 +- src/FileFormats/MOF/read.jl | 2 +- src/FileFormats/MOF/write.jl | 3 +-- test/FileFormats/MOF/MOF.jl | 11 +++++++---- 5 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index 7e9b0332a3..7ced4329fb 100644 --- a/Project.toml +++ b/Project.toml @@ -28,6 +28,7 @@ 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/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 76ebee0a68..c229751b5c 100644 --- a/src/FileFormats/MOF/write.jl +++ b/src/FileFormats/MOF/write.jl @@ -3,7 +3,6 @@ # # Use of this source code is governed by an MIT-style license that can be found # in the LICENSE.md file or at https://opensource.org/licenses/MIT. -using JSON3 """ Base.write(io::IO, model::FileFormats.MOF.Model) @@ -30,7 +29,7 @@ function Base.write(io::IO, model::Model) objective = objective, constraints = constraints, ) - Base.write(io, JSON3.write(object)) + JSON3.write(io, object) return end diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index 662064cd6b..c17a8e0a09 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,11 @@ 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( + open(MOI.FileFormats.MOF.SCHEMA_PATH, "r") do io + return JSON3.read(io, Dict{String,Any}) + end, +) function runtests() for name in names(@__MODULE__, all = true) @@ -39,7 +42,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( From daaad3cb62527378d414b2264267db32d6a4261e Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 17 Jan 2025 13:09:00 +1300 Subject: [PATCH 3/8] Update --- Project.toml | 2 -- test/FileFormats/MOF/MOF.jl | 4 +--- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Project.toml b/Project.toml index 7ced4329fb..147db537d3 100644 --- a/Project.toml +++ b/Project.toml @@ -8,7 +8,6 @@ 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" @@ -27,7 +26,6 @@ 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" diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index c17a8e0a09..32704af166 100644 --- a/test/FileFormats/MOF/MOF.jl +++ b/test/FileFormats/MOF/MOF.jl @@ -17,9 +17,7 @@ const MOF = MOI.FileFormats.MOF const TEST_MOF_FILE = "test.mof.json" const SCHEMA = JSONSchema.Schema( - open(MOI.FileFormats.MOF.SCHEMA_PATH, "r") do io - return JSON3.read(io, Dict{String,Any}) - end, + JSON3.read(read(MOI.FileFormats.MOF.SCHEMA_PATH, String), Dict{String,Any}), ) function runtests() From 56230f42af8db4805a4fbd975f752c1c2ec0062b Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 17 Jan 2025 13:09:51 +1300 Subject: [PATCH 4/8] Update --- docs/src/submodules/FileFormats/overview.md | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) 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 From c706098d51a92ef11b201af49879018646679596 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 17 Jan 2025 13:11:46 +1300 Subject: [PATCH 5/8] Update --- test/FileFormats/MOF/MOF.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index 32704af166..01fb8c83ad 100644 --- a/test/FileFormats/MOF/MOF.jl +++ b/test/FileFormats/MOF/MOF.jl @@ -113,8 +113,9 @@ 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(, '\r' => "", ' ' => "", '\n' => "") + @test read(TEST_MOF_FILE, String) == target _validate(TEST_MOF_FILE) return end From 629d7f56994ae61dcf1a3078a90cebb5dd9b0e8f Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Fri, 17 Jan 2025 13:16:27 +1300 Subject: [PATCH 6/8] Update test/FileFormats/MOF/MOF.jl --- test/FileFormats/MOF/MOF.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index 01fb8c83ad..056464c232 100644 --- a/test/FileFormats/MOF/MOF.jl +++ b/test/FileFormats/MOF/MOF.jl @@ -114,7 +114,7 @@ function test_HS071() MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) MOI.write_to_file(model, TEST_MOF_FILE) target = read(joinpath(@__DIR__, "nlp.mof.json"), String) - target = replace(, '\r' => "", ' ' => "", '\n' => "") + target = replace(target, '\r' => "", ' ' => "", '\n' => "") @test read(TEST_MOF_FILE, String) == target _validate(TEST_MOF_FILE) return From cb32e4da92ea441806975e50a085676b77d389d7 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 17 Jan 2025 13:55:37 +1300 Subject: [PATCH 7/8] Update --- test/FileFormats/MOF/MOF.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index 056464c232..8aa81f5067 100644 --- a/test/FileFormats/MOF/MOF.jl +++ b/test/FileFormats/MOF/MOF.jl @@ -114,7 +114,7 @@ function test_HS071() MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE) MOI.write_to_file(model, TEST_MOF_FILE) target = read(joinpath(@__DIR__, "nlp.mof.json"), String) - target = replace(target, '\r' => "", ' ' => "", '\n' => "") + target = replace(target, r"\s" => "") @test read(TEST_MOF_FILE, String) == target _validate(TEST_MOF_FILE) return From 4f1dfa442a3c212adc4dfd589de5978b756660a2 Mon Sep 17 00:00:00 2001 From: odow Date: Fri, 17 Jan 2025 14:34:40 +1300 Subject: [PATCH 8/8] Update --- test/FileFormats/MOF/MOF.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/test/FileFormats/MOF/MOF.jl b/test/FileFormats/MOF/MOF.jl index 8aa81f5067..2263f479e9 100644 --- a/test/FileFormats/MOF/MOF.jl +++ b/test/FileFormats/MOF/MOF.jl @@ -115,6 +115,7 @@ function test_HS071() MOI.write_to_file(model, TEST_MOF_FILE) 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