diff --git a/spec/std/json/any_spec.cr b/spec/std/json/any_spec.cr index 6601a53378bd..95f7425f2ed4 100644 --- a/spec/std/json/any_spec.cr +++ b/spec/std/json/any_spec.cr @@ -3,6 +3,19 @@ require "json" require "yaml" describe JSON::Any do + it ".new" do + JSON::Any.new(nil).raw.should be_nil + JSON::Any.new(true).raw.should eq true + JSON::Any.new(1_i64).raw.should eq 1_i64 + JSON::Any.new(1).raw.should eq 1 + JSON::Any.new(1_u8).raw.should eq 1 + JSON::Any.new(0.0).raw.should eq 0.0 + JSON::Any.new(0.0_f32).raw.should eq 0.0 + JSON::Any.new("foo").raw.should eq "foo" + JSON::Any.new([] of JSON::Any).raw.should eq [] of JSON::Any + JSON::Any.new({} of String => JSON::Any).raw.should eq({} of String => JSON::Any) + end + describe "casts" do it "gets nil" do JSON.parse("null").as_nil.should be_nil diff --git a/spec/std/yaml/any_spec.cr b/spec/std/yaml/any_spec.cr index ffab8f14babc..1eedb5e4366b 100644 --- a/spec/std/yaml/any_spec.cr +++ b/spec/std/yaml/any_spec.cr @@ -89,6 +89,22 @@ private def it_fetches_from_hash?(key, *equivalent_keys) end describe YAML::Any do + it ".new" do + YAML::Any.new(nil).raw.should be_nil + YAML::Any.new(true).raw.should eq true + YAML::Any.new(1_i64).raw.should eq 1_i64 + YAML::Any.new(1).raw.should eq 1 + YAML::Any.new(1_u8).raw.should eq 1 + YAML::Any.new(0.0).raw.should eq 0.0 + YAML::Any.new(0.0_f32).raw.should eq 0.0 + YAML::Any.new("foo").raw.should eq "foo" + YAML::Any.new(Time.utc(2023, 7, 2)).raw.should eq Time.utc(2023, 7, 2) + YAML::Any.new(Bytes[1, 2, 3]).raw.should eq Bytes[1, 2, 3] + YAML::Any.new([] of YAML::Any).raw.should eq [] of YAML::Any + YAML::Any.new({} of YAML::Any => YAML::Any).raw.should eq({} of YAML::Any => YAML::Any) + YAML::Any.new(Set(YAML::Any).new).raw.should eq Set(YAML::Any).new + end + describe "casts" do it "gets nil" do YAML.parse("").as_nil.should be_nil diff --git a/src/json/any.cr b/src/json/any.cr index d64b7859bc53..057fd54e737d 100644 --- a/src/json/any.cr +++ b/src/json/any.cr @@ -57,6 +57,18 @@ struct JSON::Any def initialize(@raw : Type) end + # :ditto: + def self.new(raw : Int) + # FIXME: Workaround for https://github.com/crystal-lang/crystal/issues/11645 + new(raw.to_i64) + end + + # :ditto: + def self.new(raw : Float) + # FIXME: Workaround for https://github.com/crystal-lang/crystal/issues/11645 + new(raw.to_f64) + end + # Assumes the underlying value is an `Array` or `Hash` and returns its size. # Raises if the underlying value is not an `Array` or `Hash`. def size : Int diff --git a/src/yaml/any.cr b/src/yaml/any.cr index 1518623efd5f..745662282dd7 100644 --- a/src/yaml/any.cr +++ b/src/yaml/any.cr @@ -66,6 +66,18 @@ struct YAML::Any def initialize(@raw : Type) end + # :ditto: + def self.new(raw : Int) + # FIXME: Workaround for https://github.com/crystal-lang/crystal/issues/11645 + new(raw.to_i64) + end + + # :ditto: + def self.new(raw : Float) + # FIXME: Workaround for https://github.com/crystal-lang/crystal/issues/11645 + new(raw.to_f64) + end + # Assumes the underlying value is an `Array` or `Hash` and returns its size. # # Raises if the underlying value is not an `Array` or `Hash`.