Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement printfile (complementary to parsefile) #326

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/JSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using .Parser: parse, parsefile
using .Writer: show_json, json, lower, print, StructuralContext, show_element,
show_string, show_key, show_pair, show_null, begin_array,
end_array, begin_object, end_object, indent, delimit, separate,
JSONText
JSONText, printfile
using .Serializations: Serialization, CommonSerialization,
StandardSerialization

Expand Down
9 changes: 9 additions & 0 deletions src/Writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,15 @@ print(io::IO, obj) = show_json(io, StandardSerialization(), obj)
print(a, indent) = print(stdout, a, indent)
print(a) = print(stdout, a)

"""
printfile(filename::AbstractString, printargs...)

A convenience wrapper that opens the `filename` for writing and prints the JSON
to it. All arguments other than `filename` are forwarded to `print`.
"""
printfile(filename::AbstractString, printargs...) =
open(io -> print(io, printargs...), filename, "w")

"""
json(a)
json(a, indent::Int)
Expand Down
7 changes: 7 additions & 0 deletions test/serializer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ let filename = tempname()
rm(filename)
end

# test convenience printing to file
let filename = tempname()
JSON.printfile(filename, Any[1, 2, 3.0])
@test read(filename, String) == "[1,2,3.0]"
rm(filename)
end

# issue #184: serializing a 0-dimensional array
@test sprint(JSON.show_json, JSON.StandardSerialization(), view([184], 1)) == "184"

Expand Down