Skip to content
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
162 changes: 162 additions & 0 deletions spec/std/json/serializable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ require "big/json"
require "uuid"
require "uuid/json"

enum JSONSerializableEnum
Zero
One
Two
OneHundred
end

@[Flags]
enum JSONSerializableFlagEnum
One
Two
OneHundred
end

class JSONAttrValue(T)
include JSON::Serializable

Expand Down Expand Up @@ -515,6 +529,44 @@ module JsonDiscriminatorBug
end
end

record JSONAttrWithEnumValue, value : JSONSerializableEnum do
include JSON::Serializable

@[JSON::Field(converter: Enum::ValueConverter(JSONSerializableEnum))]
@value : JSONSerializableEnum
end

record JSONAttrWithFlagEnumValue, value : JSONSerializableFlagEnum do
include JSON::Serializable

@[JSON::Field(converter: Enum::ValueConverter(JSONSerializableFlagEnum))]
@value : JSONSerializableFlagEnum
end

abstract class SerializableFoo
include JSON::Serializable

module Converter
def self.from_json(pull : JSON::PullParser) : SerializableFoo
SerializableFoo.find.from_json("{}")
end
end

def self.find : SerializableFoo.class
SerializableBar.as(SerializableFoo.class)
end
end

class SerializableBar < SerializableFoo
@[JSON::Field(converter: SerializableFoo::Converter)]
getter foo : SerializableFoo = SerializableBaz.new
end

class SerializableBaz < SerializableFoo
def initialize
end
end

class JSONInitializeOpts
include JSON::Serializable

Expand Down Expand Up @@ -1182,6 +1234,112 @@ describe "JSON mapping" do
end
end

describe "Enum::ValueConverter.from_json" do
it "normal enum" do
JSONAttrWithEnumValue.from_json(%({"value": 0})).value.should eq(JSONSerializableEnum::Zero)
JSONAttrWithEnumValue.from_json(%({"value": 1})).value.should eq(JSONSerializableEnum::One)
JSONAttrWithEnumValue.from_json(%({"value": 2})).value.should eq(JSONSerializableEnum::Two)
JSONAttrWithEnumValue.from_json(%({"value": 3})).value.should eq(JSONSerializableEnum::OneHundred)

expect_raises(JSON::ParseException, %(Expected Int but was String)) do
JSONAttrWithEnumValue.from_json(%({"value": "3"}))
end
expect_raises(JSON::ParseException, %(Unknown enum JSONSerializableEnum value: 4)) do
JSONAttrWithEnumValue.from_json(%({"value": 4}))
end
expect_raises(JSON::ParseException, %(Unknown enum JSONSerializableEnum value: -1)) do
JSONAttrWithEnumValue.from_json(%({"value": -1}))
end
expect_raises(JSON::ParseException, %(Expected Int but was String)) do
JSONAttrWithEnumValue.from_json(%({"value": ""}))
end

expect_raises(JSON::ParseException, "Expected Int but was String") do
JSONAttrWithEnumValue.from_json(%({"value": "one"}))
end

expect_raises(JSON::ParseException, "Expected Int but was BeginObject") do
JSONAttrWithEnumValue.from_json(%({"value": {}}))
end
expect_raises(JSON::ParseException, "Expected Int but was BeginArray") do
JSONAttrWithEnumValue.from_json(%({"value": []}))
end
end

it "flag enum" do
JSONAttrWithFlagEnumValue.from_json(%({"value": 0})).value.should eq(JSONSerializableFlagEnum::None)
JSONAttrWithFlagEnumValue.from_json(%({"value": 1})).value.should eq(JSONSerializableFlagEnum::One)
JSONAttrWithFlagEnumValue.from_json(%({"value": 2})).value.should eq(JSONSerializableFlagEnum::Two)
JSONAttrWithFlagEnumValue.from_json(%({"value": 4})).value.should eq(JSONSerializableFlagEnum::OneHundred)
JSONAttrWithFlagEnumValue.from_json(%({"value": 5})).value.should eq(JSONSerializableFlagEnum::OneHundred | JSONSerializableFlagEnum::One)
JSONAttrWithFlagEnumValue.from_json(%({"value": 7})).value.should eq(JSONSerializableFlagEnum::All)

expect_raises(JSON::ParseException, %(Unknown enum JSONSerializableFlagEnum value: 8)) do
JSONAttrWithFlagEnumValue.from_json(%({"value": 8}))
end
expect_raises(JSON::ParseException, %(Unknown enum JSONSerializableFlagEnum value: -1)) do
JSONAttrWithFlagEnumValue.from_json(%({"value": -1}))
end
expect_raises(JSON::ParseException, %(Expected Int but was String)) do
JSONAttrWithFlagEnumValue.from_json(%({"value": ""}))
end
expect_raises(JSON::ParseException, "Expected Int but was String") do
JSONAttrWithFlagEnumValue.from_json(%({"value": "one"}))
end
expect_raises(JSON::ParseException, "Expected Int but was BeginObject") do
JSONAttrWithFlagEnumValue.from_json(%({"value": {}}))
end
expect_raises(JSON::ParseException, "Expected Int but was BeginArray") do
JSONAttrWithFlagEnumValue.from_json(%({"value": []}))
end
end
end

describe "Enum::ValueConverter.to_json" do
it "normal enum" do
klass = JSONAttrWithEnumValue

klass.new(JSONSerializableEnum::One).to_json.should eq %({"value":1})
klass.from_json(klass.new(JSONSerializableEnum::One).to_json).value
.should eq(JSONSerializableEnum::One)

klass.new(JSONSerializableEnum::OneHundred).to_json.should eq %({"value":3})
klass.from_json(klass.new(JSONSerializableEnum::OneHundred).to_json).value
.should eq(JSONSerializableEnum::OneHundred)

# undefined members can't be parsed back because the standard converter only accepts
# named members
klass.new(JSONSerializableEnum.new(42)).to_json.should eq %({"value":42})
end

it "flag enum" do
klass = JSONAttrWithFlagEnumValue

klass.new(JSONSerializableFlagEnum::One).to_json.should eq %({"value":1})
klass.from_json(klass.new(JSONSerializableFlagEnum::One).to_json).value
.should eq(JSONSerializableFlagEnum::One)

klass.new(JSONSerializableFlagEnum::OneHundred).to_json.should eq %({"value":4})
klass.from_json(klass.new(JSONSerializableFlagEnum::OneHundred).to_json).value
.should eq(JSONSerializableFlagEnum::OneHundred)

combined = JSONSerializableFlagEnum::OneHundred | JSONSerializableFlagEnum::One

klass.new(combined).to_json.should eq %({"value":5})
klass.from_json(klass.new(combined).to_json).value.should eq(combined)

klass.new(JSONSerializableFlagEnum::None).to_json.should eq %({"value":0})
klass.from_json(klass.new(JSONSerializableFlagEnum::None).to_json).value
.should eq(JSONSerializableFlagEnum::None)

klass.new(JSONSerializableFlagEnum::All).to_json.should eq %({"value":7})
klass.from_json(klass.new(JSONSerializableFlagEnum::All).to_json).value
.should eq(JSONSerializableFlagEnum::All)

klass.new(JSONSerializableFlagEnum.new(42)).to_json.should eq %({"value":42})
end
end

describe "namespaced classes" do
it "lets default values use the object's own namespace" do
request = JSONNamespace::FooRequest.from_json(%({"foo":{}}))
Expand All @@ -1201,4 +1359,8 @@ describe "JSON mapping" do
it "supports generic type variables in converters" do
JSONAttrWithGenericConverter(Time::EpochConverter).from_json(%({"value":1459859781})).value.should eq(Time.unix(1459859781))
end

it "fixes #16141" do
SerializableFoo.find.from_json("{}").should be_a(SerializableBar)
end
end
97 changes: 0 additions & 97 deletions spec/std/json/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -389,67 +389,6 @@ describe "JSON serialization" do
end
end

describe "Enum::ValueConverter.from_json" do
it "normal enum" do
Enum::ValueConverter(JSONSpecEnum).from_json("0").should eq(JSONSpecEnum::Zero)
Enum::ValueConverter(JSONSpecEnum).from_json("1").should eq(JSONSpecEnum::One)
Enum::ValueConverter(JSONSpecEnum).from_json("2").should eq(JSONSpecEnum::Two)
Enum::ValueConverter(JSONSpecEnum).from_json("3").should eq(JSONSpecEnum::OneHundred)

expect_raises(JSON::ParseException, %(Expected Int but was String)) do
Enum::ValueConverter(JSONSpecEnum).from_json(%("3"))
end
expect_raises(JSON::ParseException, %(Unknown enum JSONSpecEnum value: 4)) do
Enum::ValueConverter(JSONSpecEnum).from_json("4")
end
expect_raises(JSON::ParseException, %(Unknown enum JSONSpecEnum value: -1)) do
Enum::ValueConverter(JSONSpecEnum).from_json("-1")
end
expect_raises(JSON::ParseException, %(Expected Int but was String)) do
Enum::ValueConverter(JSONSpecEnum).from_json(%(""))
end

expect_raises(JSON::ParseException, "Expected Int but was String") do
Enum::ValueConverter(JSONSpecEnum).from_json(%("one"))
end

expect_raises(JSON::ParseException, "Expected Int but was BeginObject") do
Enum::ValueConverter(JSONSpecEnum).from_json(%({}))
end
expect_raises(JSON::ParseException, "Expected Int but was BeginArray") do
Enum::ValueConverter(JSONSpecEnum).from_json(%([]))
end
end

it "flag enum" do
Enum::ValueConverter(JSONSpecFlagEnum).from_json("0").should eq(JSONSpecFlagEnum::None)
Enum::ValueConverter(JSONSpecFlagEnum).from_json("1").should eq(JSONSpecFlagEnum::One)
Enum::ValueConverter(JSONSpecFlagEnum).from_json("2").should eq(JSONSpecFlagEnum::Two)
Enum::ValueConverter(JSONSpecFlagEnum).from_json("4").should eq(JSONSpecFlagEnum::OneHundred)
Enum::ValueConverter(JSONSpecFlagEnum).from_json("5").should eq(JSONSpecFlagEnum::OneHundred | JSONSpecFlagEnum::One)
Enum::ValueConverter(JSONSpecFlagEnum).from_json("7").should eq(JSONSpecFlagEnum::All)

expect_raises(JSON::ParseException, %(Unknown enum JSONSpecFlagEnum value: 8)) do
Enum::ValueConverter(JSONSpecFlagEnum).from_json("8")
end
expect_raises(JSON::ParseException, %(Unknown enum JSONSpecFlagEnum value: -1)) do
Enum::ValueConverter(JSONSpecFlagEnum).from_json("-1")
end
expect_raises(JSON::ParseException, %(Expected Int but was String)) do
Enum::ValueConverter(JSONSpecFlagEnum).from_json(%(""))
end
expect_raises(JSON::ParseException, "Expected Int but was String") do
Enum::ValueConverter(JSONSpecFlagEnum).from_json(%("one"))
end
expect_raises(JSON::ParseException, "Expected Int but was BeginObject") do
Enum::ValueConverter(JSONSpecFlagEnum).from_json(%({}))
end
expect_raises(JSON::ParseException, "Expected Int but was BeginArray") do
Enum::ValueConverter(JSONSpecFlagEnum).from_json(%([]))
end
end
end

it "deserializes with root" do
Int32.from_json(%({"foo": 1}), root: "foo").should eq(1)
Array(Int32).from_json(%({"foo": [1, 2]}), root: "foo").should eq([1, 2])
Expand Down Expand Up @@ -703,42 +642,6 @@ describe "JSON serialization" do
end
end

describe "Enum::ValueConverter" do
it "normal enum" do
converter = Enum::ValueConverter(JSONSpecEnum)
converter.to_json(JSONSpecEnum::One).should eq %(1)
converter.from_json(converter.to_json(JSONSpecEnum::One)).should eq(JSONSpecEnum::One)

converter.to_json(JSONSpecEnum::OneHundred).should eq %(3)
converter.from_json(converter.to_json(JSONSpecEnum::OneHundred)).should eq(JSONSpecEnum::OneHundred)

# undefined members can't be parsed back because the standard converter only accepts named
# members
converter.to_json(JSONSpecEnum.new(42)).should eq %(42)
end

it "flag enum" do
converter = Enum::ValueConverter(JSONSpecFlagEnum)
converter.to_json(JSONSpecFlagEnum::One).should eq %(1)
converter.from_json(converter.to_json(JSONSpecFlagEnum::One)).should eq(JSONSpecFlagEnum::One)

converter.to_json(JSONSpecFlagEnum::OneHundred).should eq %(4)
converter.from_json(converter.to_json(JSONSpecFlagEnum::OneHundred)).should eq(JSONSpecFlagEnum::OneHundred)

combined = JSONSpecFlagEnum::OneHundred | JSONSpecFlagEnum::One
converter.to_json(combined).should eq %(5)
converter.from_json(converter.to_json(combined)).should eq(combined)

converter.to_json(JSONSpecFlagEnum::None).should eq %(0)
converter.from_json(converter.to_json(JSONSpecFlagEnum::None)).should eq(JSONSpecFlagEnum::None)

converter.to_json(JSONSpecFlagEnum::All).should eq %(7)
converter.from_json(converter.to_json(JSONSpecFlagEnum::All)).should eq(JSONSpecFlagEnum::All)

converter.to_json(JSONSpecFlagEnum.new(42)).should eq %(42)
end
end

it "does for BigInt" do
big = BigInt.new("123456789123456789123456789123456789123456789")
big.to_json.should eq("123456789123456789123456789123456789123456789")
Expand Down
4 changes: 2 additions & 2 deletions src/json/from_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Int32.from_json("1") # => 1
# Array(Int32).from_json("[1, 2, 3]") # => [1, 2, 3]
# ```
def Object.from_json(string_or_io : String | IO) : Object
def Object.from_json(string_or_io : String | IO)
parser = JSON::PullParser.new(string_or_io)
new parser
end
Expand All @@ -21,7 +21,7 @@ end
# ```
# Int32.from_json(%({"main": 1}), root: "main") # => 1
# ```
def Object.from_json(string_or_io : String | IO, root : String) : Object
def Object.from_json(string_or_io : String | IO, root : String)
parser = JSON::PullParser.new(string_or_io)
parser.on_key!(root) do
new parser
Expand Down