Skip to content

json.pretty_generate behaviour changed in 2.8.0 #714

Closed
@YOU54F

Description

@YOU54F

Issue: Empty hashes no longer print over two lines in json 2.8.0 when using JSON.pretty_generate

Expected: Empty hashes would print over two lines in json 2.8.0
Actual: Empty hashes would print on a single line in json 2.8.0

Observations: Only occurs in Ruby 3.0.x +, works correctly in 2.7

Reproducer

Change the gem version, and run script

# !/usr/bin/env ruby
if __FILE__ == $0

  require 'bundler/inline'

  gemfile(true) do
    source 'https://rubygems.org'

    gem 'json', '2.8.0'
    # gem 'json', '2.7.6'
  end

  expected_data_1 = {
    "thing": {
      "alligator": {
        "name": "Mary"
      }
    }
  }
  data_1 = { thing: { alligator: { name: 'Mary' } } }
  prettified_data_1 = JSON.pretty_generate(data_1)
  puts prettified_data_1


  data_2 = { thing: {} }
  prettified_data_2 = JSON.pretty_generate(data_2)
  puts prettified_data_2
end

Output

With Ruby 3.x

2.7.6

{
  "thing": {
    "alligator": {
      "name": "Mary"
    }
  }
}
{
  "thing": {
  }
}

2.8.0

{
  "thing": {
    "alligator": {
      "name": "Mary"
    }
  }
}
{
  "thing": {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions