From fd4ea77d393a5c53c4afbbe0c85e7798d7aad6ae Mon Sep 17 00:00:00 2001 From: Ary Borenszweig <aborenszweig@manas.com.ar> Date: Tue, 2 Dec 2014 08:10:15 -0300 Subject: [PATCH] Renamed `Json` to `JSON`, `Xml` to `XML` and `Yaml` to `YAML` to follow a convention. Fixes #279 --- CHANGELOG.md | 1 + libs/oauth/access_token.cr | 2 +- libs/oauth2/access_token/access_token.cr | 2 +- libs/oauth2/error.cr | 2 +- samples/pretty_json.cr | 6 +- spec/std/json/lexer_spec.cr | 18 +++--- spec/std/json/mapping_spec.cr | 56 +++++++++--------- spec/std/json/parser_spec.cr | 10 ++-- spec/std/json/pull_parser_spec.cr | 54 +++++++++--------- spec/std/json/serialization_spec.cr | 2 +- spec/std/xml/document_spec.cr | 6 +- spec/std/yaml_spec.cr | 18 +++--- src/json/from_json.cr | 22 ++++---- src/json/json.cr | 2 +- src/json/lexer.cr | 2 +- src/json/lexer/io_based.cr | 2 +- src/json/lexer/string_based.cr | 2 +- src/json/mapping.cr | 2 +- src/json/parser.cr | 4 +- src/json/pull_parser.cr | 2 +- src/json/to_json.cr | 20 +++---- src/json/token.cr | 2 +- src/project/project.cr | 2 +- src/xml/dom.cr | 2 +- src/xml/libxml2.cr | 14 ++--- src/xml/reader.cr | 58 +++++++++---------- src/xml/type.cr | 4 +- src/yaml/lib_yaml.cr | 2 +- src/yaml/parser.cr | 72 ++++++++++++------------ src/yaml/yaml.cr | 6 +- 30 files changed, 198 insertions(+), 199 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1de9ef2d1f..524929ca73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/libs/oauth/access_token.cr b/libs/oauth/access_token.cr index 9b684217ab..258a0ee94f 100644 --- a/libs/oauth/access_token.cr +++ b/libs/oauth/access_token.cr @@ -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 diff --git a/libs/oauth2/access_token/access_token.cr b/libs/oauth2/access_token/access_token.cr index e14d07eebb..904a2ccc8b 100644 --- a/libs/oauth2/access_token/access_token.cr +++ b/libs/oauth2/access_token/access_token.cr @@ -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 diff --git a/libs/oauth2/error.cr b/libs/oauth2/error.cr index 7d34478364..f489832862 100644 --- a/libs/oauth2/error.cr +++ b/libs/oauth2/error.cr @@ -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 diff --git a/samples/pretty_json.cr b/samples/pretty_json.cr index 83e3bd595b..6cf88c09c0 100644 --- a/samples/pretty_json.cr +++ b/samples/pretty_json.cr @@ -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 @@ -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 diff --git a/spec/std/json/lexer_spec.cr b/spec/std/json/lexer_spec.cr index c707e8ad4c..b5b01440c1 100644 --- a/spec/std/json/lexer_spec.cr +++ b/spec/std/json/lexer_spec.cr @@ -3,13 +3,13 @@ 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 @@ -17,14 +17,14 @@ 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) @@ -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) @@ -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 "}", :"}" diff --git a/spec/std/json/mapping_spec.cr b/spec/std/json/mapping_spec.cr index 08ec1904d3..d641fd2306 100644 --- a/spec/std/json/mapping_spec.cr +++ b/spec/std/json/mapping_spec.cr @@ -1,7 +1,7 @@ require "spec" require "json" -class JsonPerson +class JSONPerson json_mapping({ name: {type: String}, age: {type: Int32, nilable: true}, @@ -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")}, }) @@ -48,7 +48,7 @@ class JsonWithNilableTime end end -class JsonWithNilableTimeEmittingNull +class JSONWithNilableTimeEmittingNull json_mapping({ value: {type: Time, converter: TimeFormat.new("%F"), emit_null: true}, }) @@ -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 diff --git a/spec/std/json/parser_spec.cr b/spec/std/json/parser_spec.cr index e589c5d3dc..fd1966225e 100644 --- a/spec/std/json/parser_spec.cr +++ b/spec/std/json/parser_spec.cr @@ -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" @@ -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} diff --git a/spec/std/json/pull_parser_spec.cr b/spec/std/json/pull_parser_spec.cr index b647a24dc4..d243897dd2 100644 --- a/spec/std/json/pull_parser_spec.cr +++ b/spec/std/json/pull_parser_spec.cr @@ -1,7 +1,7 @@ require "spec" require "json" -class Json::PullParser +class JSON::PullParser def assert(event_kind : Symbol) kind.should eq(event_kind) read_next @@ -86,7 +86,7 @@ class Json::PullParser end def assert_error - expect_raises Json::ParseException do + expect_raises JSON::ParseException do read_next end end @@ -94,16 +94,16 @@ end def assert_pull_parse(string) it "parses #{string}" do - parser = Json::PullParser.new string - parser.assert Json.parse(string) + parser = JSON::PullParser.new string + parser.assert JSON.parse(string) parser.kind.should eq(:EOF) end end def assert_pull_parse_error(string) it "errors on #{string}" do - expect_raises Json::ParseException do - parser = Json::PullParser.new string + expect_raises JSON::ParseException do + parser = JSON::PullParser.new string while parser.kind != :EOF parser.read_next end @@ -111,7 +111,7 @@ def assert_pull_parse_error(string) end end -describe "Json::PullParser" do +describe "JSON::PullParser" do assert_pull_parse "null" assert_pull_parse "false" assert_pull_parse "true" @@ -166,7 +166,7 @@ describe "Json::PullParser" do {"object", %({"foo": [1, 2], "bar": {"baz": [3]}})}, ].each do |tuple| it "skips #{tuple[0]}" do - pull = Json::PullParser.new("[1, #{tuple[1]}, 2]") + pull = JSON::PullParser.new("[1, #{tuple[1]}, 2]") pull.read_array do pull.read_int.should eq(1) pull.skip @@ -177,38 +177,38 @@ describe "Json::PullParser" do end it "reads bool or null" do - Json::PullParser.new("null").read_bool_or_null.should be_nil - Json::PullParser.new("false").read_bool_or_null.should be_false + JSON::PullParser.new("null").read_bool_or_null.should be_nil + JSON::PullParser.new("false").read_bool_or_null.should be_false end it "reads int or null" do - Json::PullParser.new("null").read_int_or_null.should be_nil - Json::PullParser.new("1").read_int_or_null.should eq(1) + JSON::PullParser.new("null").read_int_or_null.should be_nil + JSON::PullParser.new("1").read_int_or_null.should eq(1) end it "reads float or null" do - Json::PullParser.new("null").read_float_or_null.should be_nil - Json::PullParser.new("1.5").read_float_or_null.should eq(1.5) + JSON::PullParser.new("null").read_float_or_null.should be_nil + JSON::PullParser.new("1.5").read_float_or_null.should eq(1.5) end it "reads string or null" do - Json::PullParser.new("null").read_string_or_null.should be_nil - Json::PullParser.new(%("hello")).read_string_or_null.should eq("hello") + JSON::PullParser.new("null").read_string_or_null.should be_nil + JSON::PullParser.new(%("hello")).read_string_or_null.should eq("hello") end it "reads array or null" do - Json::PullParser.new("null").read_array_or_null { fail "expected block not to be called" } + JSON::PullParser.new("null").read_array_or_null { fail "expected block not to be called" } - pull = Json::PullParser.new(%([1])) + pull = JSON::PullParser.new(%([1])) pull.read_array_or_null do pull.read_int.should eq(1) end end it "reads object or null" do - Json::PullParser.new("null").read_object_or_null { fail "expected block not to be called" } + JSON::PullParser.new("null").read_object_or_null { fail "expected block not to be called" } - pull = Json::PullParser.new(%({"foo": 1})) + pull = JSON::PullParser.new(%({"foo": 1})) pull.read_object_or_null do |key| key.should eq("foo") pull.read_int.should eq(1) @@ -217,7 +217,7 @@ describe "Json::PullParser" do describe "on key" do it "finds key" do - pull = Json::PullParser.new(%({"foo": 1, "bar": 2})) + pull = JSON::PullParser.new(%({"foo": 1, "bar": 2})) bar = nil pull.on_key("bar") do @@ -228,7 +228,7 @@ describe "Json::PullParser" do end it "finds key" do - pull = Json::PullParser.new(%({"foo": 1, "bar": 2})) + pull = JSON::PullParser.new(%({"foo": 1, "bar": 2})) bar = nil pull.on_key("bar") do @@ -239,7 +239,7 @@ describe "Json::PullParser" do end it "doesn't find key" do - pull = Json::PullParser.new(%({"foo": 1, "baz": 2})) + pull = JSON::PullParser.new(%({"foo": 1, "baz": 2})) bar = nil pull.on_key("bar") do @@ -250,7 +250,7 @@ describe "Json::PullParser" do end it "finds key with bang" do - pull = Json::PullParser.new(%({"foo": 1, "bar": 2})) + pull = JSON::PullParser.new(%({"foo": 1, "bar": 2})) bar = nil pull.on_key!("bar") do @@ -261,7 +261,7 @@ describe "Json::PullParser" do end it "doesn't find key with bang" do - pull = Json::PullParser.new(%({"foo": 1, "baz": 2})) + pull = JSON::PullParser.new(%({"foo": 1, "baz": 2})) expect_raises Exception, "json key not found: bar" do pull.on_key!("bar") do @@ -270,7 +270,7 @@ describe "Json::PullParser" do end it "reads float when it is an int" do - pull = Json::PullParser.new(%(1)) + pull = JSON::PullParser.new(%(1)) f = pull.read_float f.should be_a(Float64) f.should eq(1.0) @@ -278,7 +278,7 @@ describe "Json::PullParser" do ["1", "[1]", %({"x": [1]})].each do |value| it "yields all keys when skipping #{value}" do - pull = Json::PullParser.new(%({"foo": #{value}, "bar": 2})) + pull = JSON::PullParser.new(%({"foo": #{value}, "bar": 2})) pull.read_object do |key| key.should_not eq("") pull.skip diff --git a/spec/std/json/serialization_spec.cr b/spec/std/json/serialization_spec.cr index 99617c884f..cc5b909fd9 100644 --- a/spec/std/json/serialization_spec.cr +++ b/spec/std/json/serialization_spec.cr @@ -1,7 +1,7 @@ require "spec" require "json" -describe "Json serialization" do +describe "JSON serialization" do describe "from_json" do it "does Array(Nil)#from_json" do Array(Nil).from_json("[null, null]").should eq([nil, nil]) diff --git a/spec/std/xml/document_spec.cr b/spec/std/xml/document_spec.cr index 2a2d1aa9e5..b0215ad766 100644 --- a/spec/std/xml/document_spec.cr +++ b/spec/std/xml/document_spec.cr @@ -1,9 +1,9 @@ require "spec" require "xml" -describe Xml::Document do +describe XML::Document do it "parses" do - doc = Xml.parse(%(\ + doc = XML.parse(%(\ <?xml version='1.0' encoding='UTF-8'?> <people> <person id="1"> @@ -11,7 +11,7 @@ describe Xml::Document do </person> </people> )) - doc.should be_a(Xml::Document) + doc.should be_a(XML::Document) doc.child_nodes.length.should eq(1) people = doc.child_nodes.first diff --git a/spec/std/yaml_spec.cr b/spec/std/yaml_spec.cr index 0c40011e5d..c62360643c 100755 --- a/spec/std/yaml_spec.cr +++ b/spec/std/yaml_spec.cr @@ -1,22 +1,22 @@ require "spec" require "yaml" -describe "Yaml" do +describe "YAML" do describe "parser" do - assert { Yaml.load("foo").should eq("foo") } - assert { Yaml.load("- foo\n- bar").should eq(["foo", "bar"]) } - assert { Yaml.load_all("---\nfoo\n---\nbar\n").should eq(["foo", "bar"]) } - assert { Yaml.load("foo: bar").should eq({"foo" => "bar"}) } - assert { Yaml.load("--- []\n").should eq([] of Yaml::Type) } - assert { Yaml.load("---\n...").should eq("") } + assert { YAML.load("foo").should eq("foo") } + assert { YAML.load("- foo\n- bar").should eq(["foo", "bar"]) } + assert { YAML.load_all("---\nfoo\n---\nbar\n").should eq(["foo", "bar"]) } + assert { YAML.load("foo: bar").should eq({"foo" => "bar"}) } + assert { YAML.load("--- []\n").should eq([] of YAML::Type) } + assert { YAML.load("---\n...").should eq("") } it "parses recursive sequence" do - doc = Yaml.load("--- &foo\n- *foo\n") as Array + doc = YAML.load("--- &foo\n- *foo\n") as Array doc[0].object_id.should eq(doc.object_id) end it "parses alias to scalar" do - doc = Yaml.load("---\n- &x foo\n- *x\n") as Array + doc = YAML.load("---\n- &x foo\n- *x\n") as Array doc.should eq(["foo", "foo"]) doc[0].object_id.should eq(doc[1].object_id) end diff --git a/src/json/from_json.cr b/src/json/from_json.cr index eea72a47db..1b7b6eb456 100644 --- a/src/json/from_json.cr +++ b/src/json/from_json.cr @@ -1,25 +1,25 @@ def Object.from_json(string_or_io) - parser = Json::PullParser.new(string_or_io) + parser = JSON::PullParser.new(string_or_io) new parser end -def Nil.new(pull : Json::PullParser) +def Nil.new(pull : JSON::PullParser) pull.read_null end -def Bool.new(pull : Json::PullParser) +def Bool.new(pull : JSON::PullParser) pull.read_bool end -def Int32.new(pull : Json::PullParser) +def Int32.new(pull : JSON::PullParser) pull.read_int.to_i end -def Int64.new(pull : Json::PullParser) +def Int64.new(pull : JSON::PullParser) pull.read_int.to_i64 end -def Float32.new(pull : Json::PullParser) +def Float32.new(pull : JSON::PullParser) case pull.kind when :int value = pull.int_value.to_f32 @@ -30,7 +30,7 @@ def Float32.new(pull : Json::PullParser) end end -def Float64.new(pull : Json::PullParser) +def Float64.new(pull : JSON::PullParser) case pull.kind when :int value = pull.int_value.to_f @@ -41,11 +41,11 @@ def Float64.new(pull : Json::PullParser) end end -def String.new(pull : Json::PullParser) +def String.new(pull : JSON::PullParser) pull.read_string end -def Array.new(pull : Json::PullParser) +def Array.new(pull : JSON::PullParser) ary = new pull.read_array do ary << T.new(pull) @@ -53,7 +53,7 @@ def Array.new(pull : Json::PullParser) ary end -def Hash.new(pull : Json::PullParser) +def Hash.new(pull : JSON::PullParser) hash = new pull.read_object do |key| if pull.kind == :null @@ -66,7 +66,7 @@ def Hash.new(pull : Json::PullParser) end struct TimeFormat - def from_json(pull : Json::PullParser) + def from_json(pull : JSON::PullParser) string = pull.read_string parse(string) end diff --git a/src/json/json.cr b/src/json/json.cr index 32807c0331..ec4d6a70e0 100644 --- a/src/json/json.cr +++ b/src/json/json.cr @@ -1,4 +1,4 @@ -module Json +module JSON class ParseException < Exception getter line_number getter column_number diff --git a/src/json/lexer.cr b/src/json/lexer.cr index c468aab485..659712b509 100644 --- a/src/json/lexer.cr +++ b/src/json/lexer.cr @@ -1,6 +1,6 @@ require "string_pool" -abstract class Json::Lexer +abstract class JSON::Lexer getter token property skip diff --git a/src/json/lexer/io_based.cr b/src/json/lexer/io_based.cr index 320150dff8..f66ff283f3 100644 --- a/src/json/lexer/io_based.cr +++ b/src/json/lexer/io_based.cr @@ -1,4 +1,4 @@ -class Json::Lexer::IOBased < Json::Lexer +class JSON::Lexer::IOBased < JSON::Lexer def initialize(io) super() @io = io diff --git a/src/json/lexer/string_based.cr b/src/json/lexer/string_based.cr index 82fabfb267..e3fd08fde4 100644 --- a/src/json/lexer/string_based.cr +++ b/src/json/lexer/string_based.cr @@ -1,4 +1,4 @@ -class Json::Lexer::StringBased < Json::Lexer +class JSON::Lexer::StringBased < JSON::Lexer def initialize(string) super() @reader = CharReader.new(string) diff --git a/src/json/mapping.cr b/src/json/mapping.cr index 4906d59560..7804801453 100644 --- a/src/json/mapping.cr +++ b/src/json/mapping.cr @@ -8,7 +8,7 @@ class Object property {{key.id}} :: {{value[:type]}} {{ (value[:nilable] ? "?" : "").id }} {% end %} - def initialize(_pull : Json::PullParser) + def initialize(_pull : JSON::PullParser) {% for key, value in properties %} _{{key.id}} = nil {% end %} diff --git a/src/json/parser.cr b/src/json/parser.cr index d4dd4c3f98..aea5211354 100644 --- a/src/json/parser.cr +++ b/src/json/parser.cr @@ -1,8 +1,8 @@ require "./lexer" -class Json::Parser +class JSON::Parser def initialize(string_or_io) - @lexer = Json::Lexer.new(string_or_io) + @lexer = JSON::Lexer.new(string_or_io) next_token end diff --git a/src/json/pull_parser.cr b/src/json/pull_parser.cr index 8e1a652fda..ec11c8018d 100644 --- a/src/json/pull_parser.cr +++ b/src/json/pull_parser.cr @@ -1,4 +1,4 @@ -class Json::PullParser +class JSON::PullParser getter kind getter bool_value getter int_value diff --git a/src/json/to_json.cr b/src/json/to_json.cr index b9fda085d4..2db176c2e7 100644 --- a/src/json/to_json.cr +++ b/src/json/to_json.cr @@ -12,11 +12,11 @@ class Object end def to_pretty_json(io : IO) - to_json Json::PrettyWriter.new(io) + to_json JSON::PrettyWriter.new(io) end end -struct Json::ObjectBuilder(T) +struct JSON::ObjectBuilder(T) def initialize(@io : T, @indent = 0) @count = 0 end @@ -40,7 +40,7 @@ struct Json::ObjectBuilder(T) end end -struct Json::ArrayBuilder(T) +struct JSON::ArrayBuilder(T) def initialize(@io : T, @indent = 0) @count = 0 end @@ -56,25 +56,25 @@ struct Json::ArrayBuilder(T) end end -module Json::Builder +module JSON::Builder def json_object self << "{" - yield Json::ObjectBuilder.new(self) + yield JSON::ObjectBuilder.new(self) self << "}" end def json_array self << "[" - yield Json::ArrayBuilder.new(self) + yield JSON::ArrayBuilder.new(self) self << "]" end end module IO - include Json::Builder + include JSON::Builder end -class Json::PrettyWriter +class JSON::PrettyWriter include IO def initialize(@io) @@ -87,7 +87,7 @@ class Json::PrettyWriter def json_object self << "{\n" @indent += 1 - yield Json::ObjectBuilder.new(self, @indent) + yield JSON::ObjectBuilder.new(self, @indent) @indent -= 1 self << '\n' @indent.times { @io << " " } @@ -97,7 +97,7 @@ class Json::PrettyWriter def json_array self << "[\n" @indent += 1 - yield Json::ArrayBuilder.new(self, @indent) + yield JSON::ArrayBuilder.new(self, @indent) @indent -= 1 self << '\n' @indent.times { @io << " " } diff --git a/src/json/token.cr b/src/json/token.cr index 17926ad9f3..c3036eadea 100644 --- a/src/json/token.cr +++ b/src/json/token.cr @@ -1,4 +1,4 @@ -class Json::Token +class JSON::Token property :type property :string_value property :int_value diff --git a/src/project/project.cr b/src/project/project.cr index c89a8fab1d..508375df29 100644 --- a/src/project/project.cr +++ b/src/project/project.cr @@ -16,7 +16,7 @@ class Project # Load lockfile if File.file?(".deps.lock") - lock = Json.parse(File.read(".deps.lock")) as Hash + lock = JSON.parse(File.read(".deps.lock")) as Hash @dependencies.each do |dep| if locked_version = lock[dep.name]? dep.locked_version = locked_version as String diff --git a/src/xml/dom.cr b/src/xml/dom.cr index d141478c16..10cae16d8e 100644 --- a/src/xml/dom.cr +++ b/src/xml/dom.cr @@ -1,6 +1,6 @@ require "./reader" -module Xml +module XML def self.parse(string_or_io) Document.parse(string_or_io) end diff --git a/src/xml/libxml2.cr b/src/xml/libxml2.cr index 172f694115..f4c4b3b765 100644 --- a/src/xml/libxml2.cr +++ b/src/xml/libxml2.cr @@ -3,7 +3,7 @@ require "./type" @[Link("xml2")] lib LibXML type InputBuffer = Void* - type XmlTextReader = Void* + type XMLTextReader = Void* XML_READER_TYPE_NONE = 0 XML_READER_TYPE_ELEMENT = 1 @@ -26,11 +26,11 @@ lib LibXML fun xmlParserInputBufferCreateStatic(mem : UInt8*, size : Int32, encoding : Int32) : InputBuffer fun xmlParserInputBufferCreateIO(ioread : (Void*, UInt8*, Int32) -> Int32, ioclose : Void* -> Int32, ioctx : Void*, enc : Int32) : InputBuffer - fun xmlNewTextReader(input : InputBuffer, uri : UInt8*) : XmlTextReader + fun xmlNewTextReader(input : InputBuffer, uri : UInt8*) : XMLTextReader - fun xmlTextReaderRead(reader : XmlTextReader) : Int32 - fun xmlTextReaderNodeType(reader : XmlTextReader) : Xml::Type - fun xmlTextReaderConstName(reader : XmlTextReader) : UInt8* - fun xmlTextReaderIsEmptyElement(reader : XmlTextReader) : Int32 - fun xmlTextReaderConstValue(reader : XmlTextReader) : UInt8* + fun xmlTextReaderRead(reader : XMLTextReader) : Int32 + fun xmlTextReaderNodeType(reader : XMLTextReader) : XML::Type + fun xmlTextReaderConstName(reader : XMLTextReader) : UInt8* + fun xmlTextReaderIsEmptyElement(reader : XMLTextReader) : Int32 + fun xmlTextReaderConstValue(reader : XMLTextReader) : UInt8* end diff --git a/src/xml/reader.cr b/src/xml/reader.cr index e9291a480c..2b1ab9310d 100644 --- a/src/xml/reader.cr +++ b/src/xml/reader.cr @@ -1,40 +1,38 @@ require "./libxml2" -module Xml - class Reader - def initialize(str : String) - input = LibXML.xmlParserInputBufferCreateStatic(str, str.bytesize, 1) - @reader = LibXML.xmlNewTextReader(input, "") - end +class XML::Reader + def initialize(str : String) + input = LibXML.xmlParserInputBufferCreateStatic(str, str.bytesize, 1) + @reader = LibXML.xmlNewTextReader(input, "") + end - def initialize(io : IO) - input = LibXML.xmlParserInputBufferCreateIO( - ->(context, buffer, length) { Box(IO).unbox(context).read(Slice.new(buffer, length)).to_i }, - ->(context) { Box(IO).unbox(context).close; 0 }, - Box(IO).box(io), - 1 - ) - @reader = LibXML.xmlNewTextReader(input, "") - end + def initialize(io : IO) + input = LibXML.xmlParserInputBufferCreateIO( + ->(context, buffer, length) { Box(IO).unbox(context).read(Slice.new(buffer, length)).to_i }, + ->(context) { Box(IO).unbox(context).close; 0 }, + Box(IO).box(io), + 1 + ) + @reader = LibXML.xmlNewTextReader(input, "") + end - def read - LibXML.xmlTextReaderRead(@reader) == 1 - end + def read + LibXML.xmlTextReaderRead(@reader) == 1 + end - def node_type - LibXML.xmlTextReaderNodeType(@reader) - end + def node_type + LibXML.xmlTextReaderNodeType(@reader) + end - def name - String.new(LibXML.xmlTextReaderConstName(@reader)) - end + def name + String.new(LibXML.xmlTextReaderConstName(@reader)) + end - def is_empty_element? - LibXML.xmlTextReaderIsEmptyElement(@reader) == 1 - end + def is_empty_element? + LibXML.xmlTextReaderIsEmptyElement(@reader) == 1 + end - def value - String.new(LibXML.xmlTextReaderConstValue(@reader)) - end + def value + String.new(LibXML.xmlTextReaderConstValue(@reader)) end end diff --git a/src/xml/type.cr b/src/xml/type.cr index 963546c744..a47e5eba93 100644 --- a/src/xml/type.cr +++ b/src/xml/type.cr @@ -1,4 +1,4 @@ -module Xml +module XML enum Type None = 0 Element = 1 @@ -17,6 +17,6 @@ module Xml SignificantWhitespace = 14 EndElement = 15 EndEntity = 16 - XmlDeclaration = 17 + Declaration = 17 end end diff --git a/src/yaml/lib_yaml.cr b/src/yaml/lib_yaml.cr index 81b136e205..b556135c3e 100644 --- a/src/yaml/lib_yaml.cr +++ b/src/yaml/lib_yaml.cr @@ -1,5 +1,5 @@ @[Link("yaml")] -lib LibYaml +lib LibYAML PARSER_SIZE = 480 type Parser = Void* diff --git a/src/yaml/parser.cr b/src/yaml/parser.cr index 2da7c84349..5a9c161af9 100644 --- a/src/yaml/parser.cr +++ b/src/yaml/parser.cr @@ -1,29 +1,29 @@ -class Yaml::Parser +class YAML::Parser def initialize(content) - @parser = Pointer(Void).malloc(LibYaml::PARSER_SIZE) as LibYaml::Parser* - @event = LibYaml::Event.new - @anchors = {} of String => Yaml::Type + @parser = Pointer(Void).malloc(LibYAML::PARSER_SIZE) as LibYAML::Parser* + @event = LibYAML::Event.new + @anchors = {} of String => YAML::Type - LibYaml.yaml_parser_initialize(@parser) - LibYaml.yaml_parser_set_input_string(@parser, content, content.bytesize) + LibYAML.yaml_parser_initialize(@parser) + LibYAML.yaml_parser_set_input_string(@parser, content, content.bytesize) next_event - raise "Expected STREAM_START" unless @event.type == LibYaml::EventType::STREAM_START + raise "Expected STREAM_START" unless @event.type == LibYAML::EventType::STREAM_START end def close - LibYaml.yaml_parser_delete(@parser) - LibYaml.yaml_event_delete(pointerof(@event)) + LibYAML.yaml_parser_delete(@parser) + LibYAML.yaml_event_delete(pointerof(@event)) end def parse_all - documents = [] of Yaml::Type + documents = [] of YAML::Type loop do next_event case @event.type - when LibYaml::EventType::STREAM_END + when LibYAML::EventType::STREAM_END return documents - when LibYaml::EventType::DOCUMENT_START + when LibYAML::EventType::DOCUMENT_START documents << parse_document else raise "Unexpected event: #{@event.type}" @@ -34,9 +34,9 @@ class Yaml::Parser def parse next_event case @event.type - when LibYaml::EventType::STREAM_END + when LibYAML::EventType::STREAM_END nil - when LibYaml::EventType::DOCUMENT_START + when LibYAML::EventType::DOCUMENT_START parse_document else raise "Unexpected event: #{@event.type}" @@ -47,21 +47,21 @@ class Yaml::Parser next_event value = parse_node next_event - raise "Expected DOCUMENT_END" unless @event.type == LibYaml::EventType::DOCUMENT_END + raise "Expected DOCUMENT_END" unless @event.type == LibYAML::EventType::DOCUMENT_END value end def parse_node case @event.type - when LibYaml::EventType::SCALAR + when LibYAML::EventType::SCALAR String.new(@event.data.scalar.value).tap do |scalar| anchor scalar, &.scalar end - when LibYaml::EventType::ALIAS + when LibYAML::EventType::ALIAS @anchors[String.new(@event.data.alias.anchor)] - when LibYaml::EventType::SEQUENCE_START + when LibYAML::EventType::SEQUENCE_START parse_sequence - when LibYaml::EventType::MAPPING_START + when LibYAML::EventType::MAPPING_START parse_mapping else raise "Unexpected event #{event_to_s(@event.type)}" @@ -69,13 +69,13 @@ class Yaml::Parser end def parse_sequence - sequence = [] of Yaml::Type + sequence = [] of YAML::Type anchor sequence, &.sequence_start loop do next_event case @event.type - when LibYaml::EventType::SEQUENCE_END + when LibYAML::EventType::SEQUENCE_END return sequence else sequence << parse_node @@ -84,11 +84,11 @@ class Yaml::Parser end def parse_mapping - mapping = {} of Yaml::Type => Yaml::Type + mapping = {} of YAML::Type => YAML::Type loop do next_event case @event.type - when LibYaml::EventType::MAPPING_END + when LibYAML::EventType::MAPPING_END return mapping else key = parse_node @@ -106,22 +106,22 @@ class Yaml::Parser def event_to_s(event_type) case event_type - when LibYaml::EventType::NONE then "NONE" - when LibYaml::EventType::STREAM_START then "STREAM_START" - when LibYaml::EventType::STREAM_END then "STREAM_END" - when LibYaml::EventType::DOCUMENT_START then "DOCUMENT_START" - when LibYaml::EventType::DOCUMENT_END then "DOCUMENT_END" - when LibYaml::EventType::ALIAS then "ALIAS" - when LibYaml::EventType::SCALAR then "SCALAR" - when LibYaml::EventType::SEQUENCE_START then "SEQUENCE_START" - when LibYaml::EventType::SEQUENCE_END then "SEQUENCE_END" - when LibYaml::EventType::MAPPING_START then "MAPPING_START" - when LibYaml::EventType::MAPPING_END then "MAPPING_END" + when LibYAML::EventType::NONE then "NONE" + when LibYAML::EventType::STREAM_START then "STREAM_START" + when LibYAML::EventType::STREAM_END then "STREAM_END" + when LibYAML::EventType::DOCUMENT_START then "DOCUMENT_START" + when LibYAML::EventType::DOCUMENT_END then "DOCUMENT_END" + when LibYAML::EventType::ALIAS then "ALIAS" + when LibYAML::EventType::SCALAR then "SCALAR" + when LibYAML::EventType::SEQUENCE_START then "SEQUENCE_START" + when LibYAML::EventType::SEQUENCE_END then "SEQUENCE_END" + when LibYAML::EventType::MAPPING_START then "MAPPING_START" + when LibYAML::EventType::MAPPING_END then "MAPPING_END" end end def next_event - LibYaml.yaml_event_delete(pointerof(@event)) - LibYaml.yaml_parser_parse(@parser, pointerof(@event)) + LibYAML.yaml_event_delete(pointerof(@event)) + LibYAML.yaml_parser_parse(@parser, pointerof(@event)) end end diff --git a/src/yaml/yaml.cr b/src/yaml/yaml.cr index b965032ba8..86af646184 100644 --- a/src/yaml/yaml.cr +++ b/src/yaml/yaml.cr @@ -1,10 +1,10 @@ require "./*" -module Yaml +module YAML alias Type = String | Hash(Type, Type) | Array(Type) | Nil def self.load(data) - parser = Yaml::Parser.new(data) + parser = YAML::Parser.new(data) begin parser.parse ensure @@ -13,7 +13,7 @@ module Yaml end def self.load_all(data) - parser = Yaml::Parser.new(data) + parser = YAML::Parser.new(data) begin parser.parse_all ensure -- GitLab