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
10 changes: 10 additions & 0 deletions spec/std/json/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ describe "JSON serialization" do
Time.from_json(%("20161116T095548-03:00")).to_utc.should eq(Time.utc(2016, 11, 16, 12, 55, 48))
end

it "deserializes Time::Location" do
Time::Location.from_json(%("UTC")).should eq(Time::Location.load("UTC"))
end

describe "parse exceptions" do
it "has correct location when raises in NamedTuple#from_json" do
ex = expect_raises(JSON::ParseException) do
Expand Down Expand Up @@ -816,6 +820,12 @@ describe "JSON serialization" do
end
end

describe "Time::Location" do
it "#to_json" do
Time::Location.load("UTC").to_json.should eq(%("UTC"))
end
end

it "provide symmetric encoding and decoding for Union types" do
a = 1.as(Float64 | Int32)
b = (Float64 | Int32).from_json(a.to_json)
Expand Down
9 changes: 9 additions & 0 deletions spec/std/yaml/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@ describe "YAML serialization" do
expect_raises(YAML::ParseException) { Time.from_yaml(%(!!timestamp "2001-12-14\\v21:59:43.10 -5")) }
end

it "deserializes Time::Location" do
Time::Location.from_yaml("UTC").should eq(Time::Location.load("UTC"))
end

it "deserializes bytes" do
Bytes.from_yaml("!!binary aGVsbG8=").should eq("hello".to_slice)
end
Expand Down Expand Up @@ -665,6 +669,11 @@ describe "YAML serialization" do
assert_yaml_document_end(time.to_yaml, "--- 2010-11-12 01:02:03.456000000\n")
end

it "does for time location" do
location = Time::Location.load("UTC")
assert_yaml_document_end(location.to_yaml, "--- UTC\n")
end

it "does for bytes" do
yaml = "hello".to_slice.to_yaml

Expand Down
4 changes: 4 additions & 0 deletions src/json/from_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ def Time.new(pull : JSON::PullParser)
Time::Format::ISO_8601_DATE_TIME.parse(pull.read_string)
end

def Time::Location.new(pull : JSON::PullParser)
load(pull.read_string)
end

struct Time::Format
def from_json(pull : JSON::PullParser) : Time
string = pull.read_string
Expand Down
6 changes: 6 additions & 0 deletions src/json/to_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ struct NamedTuple
end
end

class Time::Location
def to_json(json : JSON::Builder) : Nil
json.string(to_s)
end
end

struct Time::Format
def to_json(value : Time, json : JSON::Builder) : Nil
json.string do |io|
Expand Down
8 changes: 8 additions & 0 deletions src/yaml/from_yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ def Time.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
parse_scalar(ctx, node, Time)
end

def Time::Location.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.kind}"
end

load(node.value)
end

struct Time::Format
def from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Time
unless node.is_a?(YAML::Nodes::Scalar)
Expand Down
6 changes: 6 additions & 0 deletions src/yaml/to_yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ struct Time
end
end

class Time::Location
def to_yaml(yaml : YAML::Nodes::Builder) : Nil
yaml.scalar to_s
end
end

struct Time::Format
def to_yaml(value : Time, yaml : YAML::Nodes::Builder) : Nil
yaml.scalar format(value)
Expand Down