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

Serialize Enum to underscored String by default #10431

Merged
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
11 changes: 11 additions & 0 deletions spec/std/json/pull_parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,15 @@ describe JSON::PullParser do
pull.read?(Float32).should be_nil
end
end

it "#raise" do
pull = JSON::PullParser.new("[1, 2, 3]")
expect_raises(JSON::ParseException, "foo bar at line 1, column 2") do
pull.raise "foo bar"
end
pull.read_begin_array
expect_raises(JSON::ParseException, "foo bar at line 1, column 3") do
pull.raise "foo bar"
end
end
end
199 changes: 181 additions & 18 deletions spec/std/json/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ enum JSONSpecEnum
Zero
One
Two
OneHundred
end

@[Flags]
enum JSONSpecFlagEnum
One
Two
OneHundred
end

describe "JSON serialization" do
Expand Down Expand Up @@ -212,30 +214,125 @@ describe "JSON serialization" do
expect_raises(JSON::ParseException) { BigDecimal.from_json("{}") }
end

it "does for Enum with number" do
JSONSpecEnum.from_json("1").should eq(JSONSpecEnum::One)
describe "Enum" do
it "normal enum" do
JSONSpecEnum.from_json(%("one")).should eq(JSONSpecEnum::One)
JSONSpecEnum.from_json(%("One")).should eq(JSONSpecEnum::One)
JSONSpecEnum.from_json(%("two")).should eq(JSONSpecEnum::Two)
JSONSpecEnum.from_json(%("ONE_HUNDRED")).should eq(JSONSpecEnum::OneHundred)
expect_raises(JSON::ParseException, %(Unknown enum JSONSpecEnum value: "ONE-HUNDRED")) do
JSONSpecEnum.from_json(%("ONE-HUNDRED"))
end
expect_raises(JSON::ParseException, %(Unknown enum JSONSpecEnum value: " one ")) do
JSONSpecEnum.from_json(%(" one "))
end

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

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

it "does for Enum with string" do
JSONSpecEnum.from_json(%("One")).should eq(JSONSpecEnum::One)
it "flag enum" do
JSONSpecFlagEnum.from_json(%(["one"])).should eq(JSONSpecFlagEnum::One)
JSONSpecFlagEnum.from_json(%(["One"])).should eq(JSONSpecFlagEnum::One)
JSONSpecFlagEnum.from_json(%(["one", "one"])).should eq(JSONSpecFlagEnum::One)
JSONSpecFlagEnum.from_json(%(["one", "two"])).should eq(JSONSpecFlagEnum::One | JSONSpecFlagEnum::Two)
JSONSpecFlagEnum.from_json(%(["one", "two", "one_hundred"])).should eq(JSONSpecFlagEnum::All)
JSONSpecFlagEnum.from_json(%([])).should eq(JSONSpecFlagEnum::None)

expect_raises(JSON::ParseException, "Expected String but was BeginArray") do
JSONSpecFlagEnum.from_json(%(["one", ["two"]]))
end

expect_raises(ArgumentError, "Unknown enum JSONSpecEnum value: Three") do
JSONSpecEnum.from_json(%("Three"))
expect_raises(JSON::ParseException, %(Unknown enum JSONSpecFlagEnum value: "three")) do
JSONSpecFlagEnum.from_json(%(["one", "three"]))
end
expect_raises(JSON::ParseException, %(Expected String but was Int)) do
JSONSpecFlagEnum.from_json(%([1, 2]))
end
expect_raises(JSON::ParseException, %(Expected String but was Int)) do
JSONSpecFlagEnum.from_json(%(["one", 2]))
end
expect_raises(JSON::ParseException, "Expected BeginArray but was BeginObject") do
JSONSpecFlagEnum.from_json(%({}))
end
expect_raises(JSON::ParseException, "Expected BeginArray but was String") do
JSONSpecFlagEnum.from_json(%("one"))
end
end
end

it "does for flag Enum with number" do
JSONSpecFlagEnum.from_json("0").should eq(JSONSpecFlagEnum::None)
JSONSpecFlagEnum.from_json("1").should eq(JSONSpecFlagEnum::One)
JSONSpecFlagEnum.from_json("2").should eq(JSONSpecFlagEnum::Two)
JSONSpecFlagEnum.from_json("3").should eq(JSONSpecFlagEnum::All)
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(Exception, "Unknown enum JSONSpecFlagEnum value: 4") do
JSONSpecFlagEnum.from_json("4")
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

Expand Down Expand Up @@ -442,8 +539,74 @@ describe "JSON serialization" do
{x: 1, y: "hello"}.to_json.should eq(%({"x":1,"y":"hello"}))
end

it "does for Enum" do
JSONSpecEnum::One.to_json.should eq("1")
describe "Enum" do
it "normal enum" do
JSONSpecEnum::One.to_json.should eq %("one")
JSONSpecEnum.from_json(JSONSpecEnum::One.to_json).should eq(JSONSpecEnum::One)

JSONSpecEnum::OneHundred.to_json.should eq %("one_hundred")
JSONSpecEnum.from_json(JSONSpecEnum::OneHundred.to_json).should eq(JSONSpecEnum::OneHundred)

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

it "flag enum" do
JSONSpecFlagEnum::One.to_json.should eq %(["one"])
JSONSpecFlagEnum.from_json(JSONSpecFlagEnum::One.to_json).should eq(JSONSpecFlagEnum::One)

JSONSpecFlagEnum::OneHundred.to_json.should eq %(["one_hundred"])
JSONSpecFlagEnum.from_json(JSONSpecFlagEnum::OneHundred.to_json).should eq(JSONSpecFlagEnum::OneHundred)

combined = JSONSpecFlagEnum::OneHundred | JSONSpecFlagEnum::One
combined.to_json.should eq %(["one","one_hundred"])
JSONSpecFlagEnum.from_json(combined.to_json).should eq(combined)

JSONSpecFlagEnum::None.to_json.should eq %([])
JSONSpecFlagEnum.from_json(JSONSpecFlagEnum::None.to_json).should eq(JSONSpecFlagEnum::None)

JSONSpecFlagEnum::All.to_json.should eq %(["one","two","one_hundred"])
JSONSpecFlagEnum.from_json(JSONSpecFlagEnum::All.to_json).should eq(JSONSpecFlagEnum::All)

JSONSpecFlagEnum.new(42).to_json.should eq %(["two"])
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

pending_win32 "does for BigInt" do
Expand Down
Loading