Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change nonsense return types to Nil: JSON and YAML #10622

Merged
merged 4 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/big/json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require "big"

class JSON::Builder
# Writes a big decimal.
def number(number : BigDecimal)
def number(number : BigDecimal) : Nil
scalar do
@io << number
end
Expand All @@ -26,7 +26,7 @@ struct BigInt
to_s
end

def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.number(self)
end
end
Expand Down Expand Up @@ -56,7 +56,7 @@ struct BigFloat
to_s
end

def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.number(self)
end
end
Expand Down Expand Up @@ -86,7 +86,7 @@ struct BigDecimal
to_s
end

def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.number(self)
end
end
4 changes: 2 additions & 2 deletions src/compiler/crystal/tools/doc/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ class Crystal::Doc::Type
builder.field "full_name", full_name
builder.field "name", name
builder.field "abstract", abstract?
builder.field "superclass" { superclass.try(&.to_json_simple(builder)) || builder.scalar(nil) }
builder.field "superclass" { (s = superclass) ? s.to_json_simple(builder) : builder.null }
builder.field "ancestors" do
builder.array do
ancestors.each &.to_json_simple(builder)
Expand Down Expand Up @@ -821,7 +821,7 @@ class Crystal::Doc::Type
including_types.each &.to_json_simple(builder)
end
end
builder.field "namespace" { namespace.try(&.to_json_simple(builder)) || builder.scalar(nil) }
builder.field "namespace" { (n = namespace) ? n.to_json_simple(builder) : builder.null }
builder.field "doc", doc
builder.field "summary", formatted_summary
builder.field "class_methods", class_methods
Expand Down
26 changes: 13 additions & 13 deletions src/json/builder.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class JSON::Builder
end

# Starts a document.
def start_document
def start_document : Nil
case state = @state.last
when StartState
@state[-1] = DocumentStartState.new
Expand Down Expand Up @@ -62,28 +62,28 @@ class JSON::Builder
end

# Writes a `null` value.
def null
def null : Nil
scalar do
@io << "null"
end
end

# Writes a boolean value.
def bool(value : Bool)
def bool(value : Bool) : Nil
scalar do
@io << value
end
end

# Writes an integer.
def number(number : Int)
def number(number : Int) : Nil
scalar do
@io << number
end
end

# Writes a float.
def number(number : Float)
def number(number : Float) : Nil
scalar do
case number
when .nan?
Expand All @@ -100,7 +100,7 @@ class JSON::Builder
# by invoking `to_s` on it.
#
# This method can also be used to write the name of an object field.
def string(value)
def string(value) : Nil
string = value.to_s

scalar(string: true) do
Expand Down Expand Up @@ -157,22 +157,22 @@ class JSON::Builder
# the IO without processing. This is the only method that
# might lead to invalid JSON being generated, so you must
# be sure that *string* contains a valid JSON string.
def raw(string : String)
def raw(string : String) : Nil
scalar do
@io << string
end
end

# Writes the start of an array.
def start_array
def start_array : Nil
start_scalar
increase_indent
@state.push ArrayState.new(empty: true)
@io << '['
end

# Writes the end of an array.
def end_array
def end_array : Nil
case state = @state.last
when ArrayState
@state.pop
Expand All @@ -193,15 +193,15 @@ class JSON::Builder
end

# Writes the start of an object.
def start_object
def start_object : Nil
start_scalar
increase_indent
@state.push ObjectState.new(empty: true, name: true)
@io << '{'
end

# Writes the end of an object.
def end_object
def end_object : Nil
case state = @state.last
when ObjectState
unless state.name
Expand Down Expand Up @@ -235,12 +235,12 @@ class JSON::Builder
end

# :ditto:
def scalar(value : Int | Float)
def scalar(value : Int | Float) : Nil
number(value)
end

# :ditto:
def scalar(value : String)
def scalar(value : String) : Nil
string(value)
end

Expand Down
4 changes: 2 additions & 2 deletions src/json/pull_parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class JSON::PullParser
# Reads the new value and fill the a JSON builder with it.
#
# Use this method with a `JSON::Builder` to read a JSON while building another one.
def read_raw(json)
def read_raw(json) : Nil
case @kind
when .null?
read_next
Expand Down Expand Up @@ -558,7 +558,7 @@ class JSON::PullParser
#
# It skips the whole value, not only the next lexer's token.
# For example if the next value is an array, the whole array will be skipped.
def skip
def skip : Nil
@lexer.skip = true
skip_internal
@lexer.skip = false
Expand Down
34 changes: 17 additions & 17 deletions src/json/to_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Object
end

struct Nil
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.null
end

Expand All @@ -35,13 +35,13 @@ struct Nil
end

struct Bool
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.bool(self)
end
end

struct Int
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.number(self)
end

Expand All @@ -51,7 +51,7 @@ struct Int
end

struct Float
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.number(self)
end

Expand All @@ -61,7 +61,7 @@ struct Float
end

class String
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
oprypin marked this conversation as resolved.
Show resolved Hide resolved
json.string(self)
end

Expand All @@ -71,7 +71,7 @@ class String
end

struct Path
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
@name.to_json(json)
end

Expand All @@ -81,7 +81,7 @@ struct Path
end

struct Symbol
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.string(to_s)
end

Expand All @@ -91,23 +91,23 @@ struct Symbol
end

class Array
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.array do
each &.to_json(json)
end
end
end

class Deque
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.array do
each &.to_json(json)
end
end
end

struct Set
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.array do
each &.to_json(json)
end
Expand All @@ -120,7 +120,7 @@ class Hash
# Keys are serialized by invoking `to_json_object_key` on them.
# Values are serialized with the usual `to_json(json : JSON::Builder)`
# method.
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.object do
each do |key, value|
json.field key.to_json_object_key do
Expand All @@ -132,7 +132,7 @@ class Hash
end

struct Tuple
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.array do
{% for i in 0...T.size %}
self[{{i}}].to_json(json)
Expand All @@ -154,7 +154,7 @@ struct NamedTuple
end

struct Time::Format
def to_json(value : Time, json : JSON::Builder)
def to_json(value : Time, json : JSON::Builder) : Nil
format(value).to_json(json)
end
end
Expand Down Expand Up @@ -263,7 +263,7 @@ struct Time
# a time value.
#
# See `#from_json` for reference.
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.string(Time::Format::RFC_3339.format(self, fraction_digits: 0))
end
end
Expand Down Expand Up @@ -343,7 +343,7 @@ end
# person.to_json # => %({"birth_date":1459859781})
# ```
module Time::EpochConverter
def self.to_json(value : Time, json : JSON::Builder)
def self.to_json(value : Time, json : JSON::Builder) : Nil
json.number(value.to_unix)
end
end
Expand All @@ -367,7 +367,7 @@ end
# timestamp.to_json # => %({"value":1459860483856})
# ```
module Time::EpochMillisConverter
def self.to_json(value : Time, json : JSON::Builder)
def self.to_json(value : Time, json : JSON::Builder) : Nil
json.number(value.to_unix_ms)
end
end
Expand All @@ -394,7 +394,7 @@ end
# raw.to_json # => %({"value":123456789876543212345678987654321})
# ```
module String::RawConverter
def self.to_json(value : String, json : JSON::Builder)
def self.to_json(value : String, json : JSON::Builder) : Nil
json.raw(value)
end
end
2 changes: 1 addition & 1 deletion src/oauth2/access_token/bearer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OAuth2::AccessToken::Bearer < OAuth2::AccessToken
request.headers["Authorization"] = "Bearer #{access_token}"
end

def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.object do
json.field "token_type", "bearer"
json.field "access_token", access_token
Expand Down
2 changes: 1 addition & 1 deletion src/oauth2/access_token/mac.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class OAuth2::AccessToken::Mac < OAuth2::AccessToken
Base64.strict_encode OpenSSL::HMAC.digest(digest, mac_key, normalized_request_string)
end

def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.object do
json.field "token_type", "mac"
json.field "access_token", access_token
Expand Down
2 changes: 1 addition & 1 deletion src/uuid/json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct UUID
# uuid = UUID.new("87b3042b-9b9a-41b7-8b15-a93d3f17025e")
# uuid.to_json # => "\"87b3042b-9b9a-41b7-8b15-a93d3f17025e\""
# ```
def to_json(json : JSON::Builder)
def to_json(json : JSON::Builder) : Nil
json.string(to_s)
end

Expand Down
2 changes: 1 addition & 1 deletion src/yaml/any.cr
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ struct YAML::Any
raw.to_yaml(io)
end

def to_json(builder : JSON::Builder)
def to_json(builder : JSON::Builder) : Nil
if (raw = self.raw).is_a?(Slice)
raise "Can't serialize #{raw.class} to JSON"
else
Expand Down
Loading