Skip to content

Commit

Permalink
Add JSON::Builder#next_is_object_key?
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 24, 2019
1 parent 62bf7fb commit 1c26bce
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions spec/std/json/builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,18 @@ describe JSON::Builder do
builder.start_object
end
end

it "#next_is_object_key?" do
io = IO::Memory.new
builder = JSON::Builder.new(io)
builder.next_is_object_key?.should be_false
builder.start_document
builder.next_is_object_key?.should be_false
builder.start_object
builder.next_is_object_key?.should be_true
builder.scalar("foo")
builder.next_is_object_key?.should be_false
builder.scalar("bar")
builder.next_is_object_key?.should be_true
end
end
7 changes: 7 additions & 0 deletions src/json/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,13 @@ class JSON::Builder
end
end

# Returns `true` if the next thing that must pushed into this
# builder is an object key (so a string) or the end of an object.
def next_is_object_key? : Bool
state = @state.last
state.is_a?(ObjectState) && state.name
end

private def scalar(string = false)
start_scalar(string)
yield.tap { end_scalar(string) }
Expand Down

0 comments on commit 1c26bce

Please sign in to comment.