Skip to content

Commit

Permalink
Add an option to print the JSON with no whitespace (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Apr 16, 2019
1 parent f7a6027 commit 9ae7035
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/MOF/MOF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const Model = MOIU.UniversalFallback{InnerModel{Float64}}
struct ModelOptions <: MOI.AbstractModelAttribute end

struct Options
print_compact::Bool
validate::Bool
warn::Bool
end
Expand All @@ -57,13 +58,18 @@ Create an empty instance of MathOptFormat.Model.
Keyword arguments are:
- `validate::Bool=true`: validate each file prior to reading against the MOF schema
- `print_compact::Bool=false`: print the JSON file in a compact format without
spaces or newlines.
- `validate::Bool=true`: validate each file prior to reading against the MOF
schema
- `warn::Bool=false`: print a warning when variables or constraints are renamed
"""
function Model(; validate::Bool = true, warn::Bool = false)
function Model(;
print_compact::Bool = false, validate::Bool = true, warn::Bool = false)
model = MOIU.UniversalFallback(InnerModel{Float64}())
MOI.set(model, ModelOptions(), Options(validate, warn))
MOI.set(model, ModelOptions(), Options(print_compact, validate, warn))
return model
end

Expand Down
3 changes: 2 additions & 1 deletion src/MOF/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ function MOI.write_to_file(model::Model, io::IO)
write_nlpblock(object, model, name_map)
write_objectives(object, model, name_map)
write_constraints(object, model, name_map)
Base.write(io, JSON.json(object, 2))
indent = options.print_compact ? nothing : 2
Base.write(io, JSON.json(object, indent))
return
end

Expand Down

0 comments on commit 9ae7035

Please sign in to comment.