Skip to content

Commit

Permalink
Merge pull request #1575 from ksss/json-kernel
Browse files Browse the repository at this point in the history
Add Kernel#`j`, `jj` and `JSON` with stdlib/json
  • Loading branch information
soutaro authored Oct 30, 2023
2 parents 069eb3e + c12053f commit b533b6b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
37 changes: 37 additions & 0 deletions stdlib/json/0/json.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,43 @@ JSON::VERSION_MAJOR: Integer

JSON::VERSION_MINOR: Integer

%a{annotate:rdoc:skip}
module Kernel
private

# <!--
# rdoc-file=ext/json/lib/json/common.rb
# - j(*objs)
# -->
# Outputs *objs* to STDOUT as JSON strings in the shortest form, that is in one
# line.
#
def j: (*_ToJson) -> nil

# <!--
# rdoc-file=ext/json/lib/json/common.rb
# - jj(*objs)
# -->
# Outputs *objs* to STDOUT as JSON strings in a pretty format, with indentation
# and over many lines.
#
def jj: (*_ToJson) -> nil

# <!--
# rdoc-file=ext/json/lib/json/common.rb
# - JSON(object, *args)
# -->
# If *object* is string-like, parse the string and return the parsed result as a
# Ruby data structure. Otherwise, generate a JSON text from the Ruby data
# structure object and return it.
#
# The *opts* argument is passed through to generate/parse respectively. See
# generate and parse for their documentation.
#
def JSON: (string source, ?json_options opts) -> untyped
| (_ToJson obj, ?json_options opts) -> String
end

%a{annotate:rdoc:skip}
class Object
# Converts this object to a string (calling #to_s), converts
Expand Down
36 changes: 36 additions & 0 deletions test/stdlib/json/JSONKernel_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative "../test_helper"
require "json"

class JSONKernelInstanceTest < Test::Unit::TestCase
include TypeAssertions

library "json"
testing "::Kernel"

def silent
orig_stdout = $stdout
$stdout = StringIO.new
yield
ensure
$stdout = orig_stdout
end

def test_j
silent do
assert_send_type("(Integer) -> nil", self, :j, 1)
assert_send_type("(Array[Integer]) -> nil", self, :j, [1, 2, 3])
end
end

def test_jj
silent do
assert_send_type("(Integer) -> nil", self, :jj, 1)
assert_send_type("(Array[Integer]) -> nil", self, :jj, [1, 2, 3])
end
end

def test_JSON
assert_send_type("(String) -> Hash[String, Integer]", self, :JSON, '{"a": 1}')
assert_send_type("(Array[Integer]) -> String", self, :JSON, [1, 2, 3])
end
end

0 comments on commit b533b6b

Please sign in to comment.