Skip to content

Pretty generation produces incorrect indentation with 2.11.x #790

@Bo98

Description

@Bo98

Using:

ruby 3.4.3 (2025-04-14 revision d0b7e5b6a0) +PRISM [arm64-darwin23]

Test script:

require "json"
require "uri"

test = {
  a: {
    b: URI("https://example.com"),
  },
  c: "d",
}
puts JSON.pretty_generate(test)

Output with 2.10.2:

$ cat Gemfile
source "https://rubygems.org"

gem "json", "=2.10.2"
$ bundle exec ruby test.rb
{
  "a": {
    "b": "https://example.com"
  },
  "c": "d"
}

Output with 2.11.2:

$ cat Gemfile             
source "https://rubygems.org"

gem "json", "=2.11.2"
$ bundle exec ruby test.rb
{
  "a": {
    "b": "https://example.com"
  },
    "c": "d"
}

Above example was trying to give a real-world use case (URIs). A simpler example for an rspec would be something like:

require "json"

class A
  def to_s = "value"
end
   
test = {
  a: {  
    b: A.new,
  },
  c: "d",
}
puts JSON.pretty_generate(test)
$ bundle exec ruby test.rb
{
  "a": {
    "b": "value"
  },
    "c": "d"
}

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