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

Allow JSON::Builder#field to accept non-scalar values. #4706

merged 2 commits into from Aug 18, 2017


Copy link

@straight-shoota straight-shoota commented Jul 12, 2017

This simplifies writing complex values in json fields:
Instead of json.field "field" { value.to_json(json) } you can simply
write json.field "field", value.

This simplifies writing complex objects as field values:
Instead of `json.field "field" { value.to_json(json) }` you can simply
write ``json.field "field", value`.
assert_built(%<{"hash":{"hash":"value"},"object":{"int":12}}>) do
object do
field "hash", {"hash" => "value"}
field "object",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation fail

Copy link

RX14 commented Jul 17, 2017

I've wanted this change in 2 projects now, would be great to get another opinion on this so it could be merged. Here's a snippet from a real project:

json.field "avatar" do
  json.object do
    json.field "id", @avatar_id
    json.field "url", "#{config.api_base_url}/v0/avatars/#{@avatar_id || "default"}"

this would be written more neatly as

json.field "avatar", {
  id: @avatar_id,
  url: "#{config.api_base_url}/v0/avatars/#{@avatar_id || "default"}"

@RX14 RX14 merged commit c932ced into crystal-lang:master Aug 18, 2017
@RX14 RX14 added this to the Next milestone Aug 18, 2017
@straight-shoota straight-shoota deleted the jm-json-field branch August 18, 2017 10:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

Successfully merging this pull request may close these issues.

None yet

4 participants