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

Add default "null" to JSON::Builder#field #10563

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 55 additions & 13 deletions spec/std/json/builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,65 @@ describe JSON::Builder do
end
end

it "writes field with scalar in object" do
assert_built(%<{"int":42,"float":0.815,"null":null,"bool":true,"string":"string"}>) do
object do
field "int", 42
field "float", 0.815
field "null", nil
field "bool", true
field "string", "string"
describe "#field" do
it "writes field with scalar in object" do
assert_built(%<{"int":42,"float":0.815,"null":null,"bool":true,"string":"string"}>) do
object do
field "int", 42
field "float", 0.815
field "null", nil
field "bool", true
field "string", "string"
end
end
end

it "writes field with arbitrary value in object" do
assert_built(%<{"hash":{"hash":"value"},"object":{"int":12}}>) do
object do
field "hash", {"hash" => "value"}
field "object", TestObject.new
end
end
end
end

it "writes field with arbitrary value in object" do
assert_built(%<{"hash":{"hash":"value"},"object":{"int":12}}>) do
object do
field "hash", {"hash" => "value"}
field "object", TestObject.new
describe "#field(name, &)" do
it "writes field with scalar in object" do
assert_built(%<{"int":42,"float":0.815,"null":null,"bool":true,"string":"string"}>) do
object do
field "int" { scalar 42 }
field "float" { scalar 0.815 }
field "null" { scalar nil }
field "bool" { scalar true }
field "string" { scalar "string" }
end
end
end

it "writes field with arbitrary value in object" do
assert_built(%<{"hash":{"hash":"value"},"object":{"int":12}}>) do |builder|
object do
field "hash" { {"hash" => "value"}.to_json(builder) }
field "object" { TestObject.new.to_json(builder) }
end
end
end

it "defaults to null" do
assert_built(%<{"foo":null}>) do
object do
field "foo" { }
end
end
end

it "detects invalid state" do
builder = JSON::Builder.new(IO::Memory.new)
builder.start_document
builder.start_object
expect_raises(JSON::Error, "Invalid builder state, not inside an object") do
builder.field "foo" { builder.start_array }
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions src/json/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,21 @@ class JSON::Builder
# Writes an object's field and then invokes the block.
# This is equivalent of invoking `string(value)` and then
# invoking the block.
#
# If the block does not write a value, the default is `null`.
def field(name)
string(name)

yield

case state = @state.last
when ObjectState
unless state.name
null
end
else
raise JSON::Error.new("Invalid builder state, not inside an object.")
end
end

# Flushes the underlying `IO`.
Expand Down