Skip to content

Commit

Permalink
Renamed Json to JSON, Xml to XML and Yaml to YAML to foll…
Browse files Browse the repository at this point in the history
…ow a convention. Fixes #279
  • Loading branch information
Ary Borenszweig committed Dec 2, 2014
1 parent bd709f5 commit fd4ea77
Show file tree
Hide file tree
Showing 30 changed files with 198 additions and 199 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Next

* **(breaking change)** Renamed `Json` to `JSON`, `Xml` to `XML` and `Yaml` to `YAML` to follow [a convention](https://github.com/manastech/crystal/issues/279).
* **(breaking change)** `require "foo"` always looks up in `CRYSTAL_PATH`. `require "./foo"` looks up relative to the requiring file.
* Added `alias_method` macro (thanks @Exilor and @jtomschroeder).
* Added some `Complex` number methods and many math methods, refactors and specs (thanks @scidom).
Expand Down
2 changes: 1 addition & 1 deletion libs/oauth/access_token.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OAuth::AccessToken
new token.not_nil!, secret.not_nil!, extra
end

def self.new(pull : Json::PullParser)
def self.new(pull : JSON::PullParser)
token = nil
secret = nil
extra = {} of String => String
Expand Down
2 changes: 1 addition & 1 deletion libs/oauth2/access_token/access_token.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
abstract class OAuth2::AccessToken
def self.new(pull : Json::PullParser)
def self.new(pull : JSON::PullParser)
token_type = nil
access_token = nil
expires_in = nil
Expand Down
2 changes: 1 addition & 1 deletion libs/oauth2/error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OAuth2::Error < Exception
end
end

def self.new(pull : Json::PullParser)
def self.new(pull : JSON::PullParser)
error = nil
error_description = nil

Expand Down
6 changes: 3 additions & 3 deletions samples/pretty_json.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Json pretty printer
# JSON pretty printer
# ~~~~~~~~~~~~~~~~~~~
#
# Reads Json from STDIN and outputs it formatted and colored to STDOUT.
# Reads JSON from STDIN and outputs it formatted and colored to STDOUT.
#
# Usage: echo '[1, {"two": "three"}, false]' | pretty_json

Expand All @@ -10,7 +10,7 @@ require "colorize"

class PrettyPrinter
def initialize(@input, @output)
@pull = Json::PullParser.new @input
@pull = JSON::PullParser.new @input
@indent = 0
end

Expand Down
18 changes: 9 additions & 9 deletions spec/std/json/lexer_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ require "json"

def it_lexes_json(string, expected_type, file = __FILE__, line = __LINE__)
it "lexes #{string} from string", file, line do
lexer = Json::Lexer.new string
lexer = JSON::Lexer.new string
token = lexer.next_token
token.type.should eq(expected_type)
end

it "lexes #{string} from IO", file, line do
lexer = Json::Lexer.new StringIO.new(string)
lexer = JSON::Lexer.new StringIO.new(string)
token = lexer.next_token
token.type.should eq(expected_type)
end
end

def it_lexes_json_string(string, string_value, file = __FILE__, line = __LINE__)
it "lexes #{string} from String", file, line do
lexer = Json::Lexer.new string
lexer = JSON::Lexer.new string
token = lexer.next_token
token.type.should eq(:STRING)
token.string_value.should eq(string_value)
end

it "lexes #{string} from IO", file, line do
lexer = Json::Lexer.new StringIO.new(string)
lexer = JSON::Lexer.new StringIO.new(string)
token = lexer.next_token
token.type.should eq(:STRING)
token.string_value.should eq(string_value)
Expand All @@ -33,14 +33,14 @@ end

def it_lexes_json_int(string, int_value, file = __FILE__, line = __LINE__)
it "lexes #{string} from String", file, line do
lexer = Json::Lexer.new string
lexer = JSON::Lexer.new string
token = lexer.next_token
token.type.should eq(:INT)
token.int_value.should eq(int_value)
end

it "lexes #{string} from IO", file, line do
lexer = Json::Lexer.new StringIO.new(string)
lexer = JSON::Lexer.new StringIO.new(string)
token = lexer.next_token
token.type.should eq(:INT)
token.int_value.should eq(int_value)
Expand All @@ -49,21 +49,21 @@ end

def it_lexes_json_float(string, float_value, file = __FILE__, line = __LINE__)
it "lexes #{string} from String", file, line do
lexer = Json::Lexer.new string
lexer = JSON::Lexer.new string
token = lexer.next_token
token.type.should eq(:FLOAT)
token.float_value.should eq(float_value)
end

it "lexes #{string} from IO", file, line do
lexer = Json::Lexer.new StringIO.new(string)
lexer = JSON::Lexer.new StringIO.new(string)
token = lexer.next_token
token.type.should eq(:FLOAT)
token.float_value.should eq(float_value)
end
end

describe "Json::Lexer" do
describe "JSON::Lexer" do
it_lexes_json "", :EOF
it_lexes_json "{", :"{"
it_lexes_json "}", :"}"
Expand Down
56 changes: 28 additions & 28 deletions spec/std/json/mapping_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "spec"
require "json"

class JsonPerson
class JSONPerson
json_mapping({
name: {type: String},
age: {type: Int32, nilable: true},
Expand All @@ -13,33 +13,33 @@ class JsonPerson
end
end

class StrictJsonPerson
class StrictJSONPerson
json_mapping({
name: {type: String},
age: {type: Int32, nilable: true},
}, true)
end

class JsonPersonEmittingNull
class JSONPersonEmittingNull
json_mapping({
name: {type: String},
age: {type: Int32, nilable: true, emit_null: true},
})
end

class JsonWithBool
class JSONWithBool
json_mapping({
value: {type: Bool},
})
end

class JsonWithTime
class JSONWithTime
json_mapping({
value: {type: Time, converter: TimeFormat.new("%F %T")},
})
end

class JsonWithNilableTime
class JSONWithNilableTime
json_mapping({
value: {type: Time, converter: TimeFormat.new("%F")},
})
Expand All @@ -48,7 +48,7 @@ class JsonWithNilableTime
end
end

class JsonWithNilableTimeEmittingNull
class JSONWithNilableTimeEmittingNull
json_mapping({
value: {type: Time, converter: TimeFormat.new("%F"), emit_null: true},
})
Expand All @@ -57,92 +57,92 @@ class JsonWithNilableTimeEmittingNull
end
end

class JsonWithSimpleMapping
class JSONWithSimpleMapping
json_mapping({name: String, age: Int32})
end

describe "Json mapping" do
describe "JSON mapping" do
it "parses person" do
person = JsonPerson.from_json(%({"name": "John", "age": 30}))
person.should be_a(JsonPerson)
person = JSONPerson.from_json(%({"name": "John", "age": 30}))
person.should be_a(JSONPerson)
person.name.should eq("John")
person.age.should eq(30)
end

it "parses person without age" do
person = JsonPerson.from_json(%({"name": "John"}))
person.should be_a(JsonPerson)
person = JSONPerson.from_json(%({"name": "John"}))
person.should be_a(JSONPerson)
person.name.should eq("John")
person.name.length.should eq(4) # This verifies that name is not nilable
person.age.should be_nil
end

it "parses array of people" do
people = Array(JsonPerson).from_json(%([{"name": "John"}, {"name": "Doe"}]))
people = Array(JSONPerson).from_json(%([{"name": "John"}, {"name": "Doe"}]))
people.length.should eq(2)
end

it "does to_json" do
person = JsonPerson.from_json(%({"name": "John", "age": 30}))
person2 = JsonPerson.from_json(person.to_json)
person = JSONPerson.from_json(%({"name": "John", "age": 30}))
person2 = JSONPerson.from_json(person.to_json)
person2.should eq(person)
end

it "parses person with unknown attributes" do
person = JsonPerson.from_json(%({"name": "John", "age": 30, "foo": "bar"}))
person.should be_a(JsonPerson)
person = JSONPerson.from_json(%({"name": "John", "age": 30, "foo": "bar"}))
person.should be_a(JSONPerson)
person.name.should eq("John")
person.age.should eq(30)
end

it "parses strict person with unknown attributes" do
expect_raises Exception, "unknown json attribute: foo" do
StrictJsonPerson.from_json(%({"name": "John", "age": 30, "foo": "bar"}))
StrictJSONPerson.from_json(%({"name": "John", "age": 30, "foo": "bar"}))
end
end

it "doesn't emit null by default when doing to_json" do
person = JsonPerson.from_json(%({"name": "John"}))
person = JSONPerson.from_json(%({"name": "John"}))
(person.to_json =~ /age/).should be_falsey
end

it "emits null on request when doing to_json" do
person = JsonPersonEmittingNull.from_json(%({"name": "John"}))
person = JSONPersonEmittingNull.from_json(%({"name": "John"}))
(person.to_json =~ /age/).should be_truthy
end

it "doesn't raises on false value when not-nil" do
json = JsonWithBool.from_json(%({"value": false}))
json = JSONWithBool.from_json(%({"value": false}))
json.value.should be_false
end

it "parses json with TimeFormat converter" do
json = JsonWithTime.from_json(%({"value": "2014-10-31 23:37:16"}))
json = JSONWithTime.from_json(%({"value": "2014-10-31 23:37:16"}))
json.value.should be_a(Time)
json.value.to_s.should eq("2014-10-31 23:37:16")
json.to_json.should eq(%({"value":"2014-10-31 23:37:16"}))
end

it "allows setting a nilable property to nil" do
person = JsonPerson.new("John")
person = JSONPerson.new("John")
person.age = 1
person.age = nil
end

it "parses simple mapping" do
person = JsonWithSimpleMapping.from_json(%({"name": "John", "age": 30}))
person.should be_a(JsonWithSimpleMapping)
person = JSONWithSimpleMapping.from_json(%({"name": "John", "age": 30}))
person.should be_a(JSONWithSimpleMapping)
person.name.should eq("John")
person.age.should eq(30)
end

it "outputs with converter when nilable" do
json = JsonWithNilableTime.new
json = JSONWithNilableTime.new
json.to_json.should eq("{}")
end

it "outputs with converter when nilable when emit_null is true" do
json = JsonWithNilableTimeEmittingNull.new
json = JSONWithNilableTimeEmittingNull.new
json.to_json.should eq(%({"value":null}))
end
end
10 changes: 5 additions & 5 deletions spec/std/json/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ require "json"

def it_parses_json(string, expected_value, file = __FILE__, line = __LINE__)
it "parses #{string}", file, line do
Json.parse(string).should eq(expected_value)
JSON.parse(string).should eq(expected_value)
end
end

def it_raises_on_parse_json(string, file = __FILE__, line = __LINE__)
it "raises on parse #{string}", file, line do
expect_raises Json::ParseException do
Json.parse(string)
expect_raises JSON::ParseException do
JSON.parse(string)
end
end
end

describe "Json::Parser" do
describe "JSON::Parser" do
it_parses_json "1", 1
it_parses_json "2.5", 2.5
it_parses_json %("hello"), "hello"
Expand All @@ -34,7 +34,7 @@ describe "Json::Parser" do
it_parses_json "[0]", [0]
it_parses_json " [ 0 ] ", [0]

it_parses_json "{}", {} of String => Json::Type
it_parses_json "{}", {} of String => JSON::Type
it_parses_json %({"foo": 1}), {"foo" => 1}
it_parses_json %({"foo": 1, "bar": 1.5}), {"foo" => 1, "bar" => 1.5}
it_parses_json %({"fo\\no": 1}), {"fo\no" => 1}
Expand Down
Loading

0 comments on commit fd4ea77

Please sign in to comment.