Created
January 14, 2020 09:09
-
-
Save timgentry/aad01c698c9e6162d708858b1430fa3c to your computer and use it in GitHub Desktop.
Use of JSON::State to output more human readable JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
hash = { 'a' => 1, 'b' => 2 } | |
state = JSON::State.new(indent: ' ', | |
space: ' ', | |
space_before: '', | |
object_nl: "\n", | |
array_nl: "\n") | |
hash.to_json # => "{\"a\":1,\"b\":2}" | |
hash.to_json(state) # => "{\n \"a\": 1,\n \"b\": 2\n}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment