# Why not defined Kernal#to\_json use JSON.build(&) directly?

**URL:** <https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898>\
**Category:** Help & Support\
**Created:** [August 2, 2023, 3:13am UTC](https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898 "2023-08-02T03:13:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [August 2, 2023, 3:13am UTC](https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898/1 "2023-08-02T03:13:13Z")

</div>

Check following source code.

> <https://github.com/crystal-lang/crystal/blob/master/src/json/to_json.cr#L2-L6>

It defined by use `#to_json(IO)`, then in that method, use JSON.build(io, &)

why not use JSON.build(&) directly like following?

```crystal
def to_json : String
  JSON.build do |b|
     to_json(b)
  end
end

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [August 2, 2023, 4:44am UTC](https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898/2 "2023-08-02T04:44:57Z")

</div>

> [@zw963](#):
>
> why not use JSON.build(&) directly like following?

Because `JSON.build` doesn’t return a string.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [August 2, 2023, 5:07am UTC](https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898/3 "2023-08-02T05:07:09Z")

</div>

Following is example come from API docs.

```auto
require "json"

string = JSON.build do |json|
  json.object do
    json.field "name", "foo"
    json.field "values" do
      json.array do
        json.number 1
        json.number 2
        json.number 3
      end
    end
  end
end

p typeof(string) # => String

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [August 2, 2023, 1:03pm UTC](https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898/4 "2023-08-02T13:03:31Z")

</div>

Ahh rip, was looking at the one that takes an `IO`. In the end I don’t think it matters much because this version still uses `String.build` under the hood so 🤷.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [August 2, 2023, 1:09pm UTC](https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898/5 "2023-08-02T13:09:30Z")

</div>

> In the end I don’t think it matters much because this version still uses `String.build` under the hood

Yes, i see, that my question, if don’t think it matters, why we create a JSON.build(&) method which only wrap String.build?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [August 2, 2023, 1:12pm UTC](https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898/6 "2023-08-02T13:12:29Z")

</div>

Probably just convenience, for when you just want to build a JSON string without forcing the user to end up using `String.build` themselves.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [August 2, 2023, 1:17pm UTC](https://forum.crystal-lang.org/t/why-not-defined-kernal-to-json-use-json-build-directly/5898/7 "2023-08-02T13:17:48Z")

</div>

Aha, perhaps this really not a big deal, when i do rename refactor, i find this issue, those duplicate code in Kernel#to\_json and JSON.build maybe give better performance.
