diff --git a/crystal/crystal.json b/crystal/crystal.json index 07624d0a8..95c887553 100755 --- a/crystal/crystal.json +++ b/crystal/crystal.json @@ -18,6 +18,7 @@ "classes.md", "regexes.md", "testing.md", + "json.md", "sqlite.md", "time.md", "other.md" diff --git a/crystal/examples/assign_to_variable.cr b/crystal/examples/assign_to_variable.cr new file mode 100644 index 000000000..684e7764e --- /dev/null +++ b/crystal/examples/assign_to_variable.cr @@ -0,0 +1,8 @@ +x = "one" +p! x +p! typeof(x) + +x = 1 +p! x +p! typeof(x) + diff --git a/crystal/examples/assign_to_variable.out b/crystal/examples/assign_to_variable.out new file mode 100644 index 000000000..c1c9fd803 --- /dev/null +++ b/crystal/examples/assign_to_variable.out @@ -0,0 +1,4 @@ +x # => "one" +typeof(x) # => String +x # => 1 +typeof(x) # => Int32 diff --git a/crystal/examples/hashes/hash_and_types.cr b/crystal/examples/hashes/hash_and_types.cr new file mode 100644 index 000000000..bf3b3bd77 --- /dev/null +++ b/crystal/examples/hashes/hash_and_types.cr @@ -0,0 +1,7 @@ +person = { + "name" => "Foo Bar", + "number" => 42, +} +p! person +puts typeof(person) # Hash(String, Int32 | String) + diff --git a/crystal/examples/hashes/hash_and_types.out b/crystal/examples/hashes/hash_and_types.out new file mode 100644 index 000000000..3bb2487cf --- /dev/null +++ b/crystal/examples/hashes/hash_and_types.out @@ -0,0 +1,2 @@ +person # => {"name" => "Foo Bar", "number" => 42} +Hash(String, Int32 | String) diff --git a/crystal/examples/json.cr b/crystal/examples/json/json.cr similarity index 100% rename from crystal/examples/json.cr rename to crystal/examples/json/json.cr diff --git a/crystal/examples/json_to_array.cr b/crystal/examples/json/json_to_array.cr similarity index 100% rename from crystal/examples/json_to_array.cr rename to crystal/examples/json/json_to_array.cr diff --git a/crystal/examples/json_to_named_tuple.cr b/crystal/examples/json/json_to_named_tuple.cr similarity index 100% rename from crystal/examples/json_to_named_tuple.cr rename to crystal/examples/json/json_to_named_tuple.cr diff --git a/crystal/examples/math.json b/crystal/examples/json/math.json similarity index 100% rename from crystal/examples/math.json rename to crystal/examples/json/math.json diff --git a/crystal/examples/read_math_json.cr b/crystal/examples/json/read_math_json.cr similarity index 100% rename from crystal/examples/read_math_json.cr rename to crystal/examples/json/read_math_json.cr diff --git a/crystal/examples/try.cr b/crystal/examples/try.cr index bf8678d62..60671ce43 100644 --- a/crystal/examples/try.cr +++ b/crystal/examples/try.cr @@ -1,16 +1,3 @@ -# counter = Int32|Nil - -# counter = 2 -# puts counter + 1 - - -# person = { -# "name" => "Foo Bar", -# "number" => 42, -# } -# p! person -# puts typeof(person) # Hash(String, Int32 | String) - # joe = {} of String => Int32 | String # joe["name"] = "Joe" # joe["number"] = 23 diff --git a/crystal/hashes.md b/crystal/hashes.md index 155d22c55..33af6a4ef 100644 --- a/crystal/hashes.md +++ b/crystal/hashes.md @@ -21,3 +21,10 @@ ![](examples/hashes/empty_hash_string_int32.cr) ![](examples/hashes/empty_hash_string_int32_bool.cr) + +## Hash and types +{id: hash-and-types} + +![](examples/hashes/hash_and_types.cr) +![](examples/hashes/hash_and_types.out) + diff --git a/crystal/json.md b/crystal/json.md new file mode 100644 index 000000000..c05784dc6 --- /dev/null +++ b/crystal/json.md @@ -0,0 +1,39 @@ +# JSON +{id: json} + +## JSON (to_json, parse) +{id: parse-json} +{i: json} +{i: to_json} +{i: parse} + +* Round trip with JSON +* Have to `require "json"` for the method to be added. + +![](examples/json/json.cr) + +## JSON to NamedTuple +{id: json-to-namedtuple} +{i: NamedTuple} +{i: from_json} +{i: JSON} + +![](examples/json/json_to_named_tuple.cr) + +## Reading a JSON file +{id: reading-a-json-file} +{i: NamedTuple} + +![](examples/json/math.json) +![](examples/json/read_math_json.cr) + +## JSON to Array +{id: json-to-array} +{i: JSON} +{i: from_json} + +![](examples/json/json_to_array.cr) + + + + diff --git a/crystal/other.md b/crystal/other.md index 66dd10f86..030d5044a 100644 --- a/crystal/other.md +++ b/crystal/other.md @@ -11,23 +11,8 @@ ## No type-checking? {id: no-typechecking} -``` -x = "one" -x = 1 -p! x -p! typeof(x) -``` - -## JSON (to_json, parse) -{id: json} -{i: json} -{i: to_json} -{i: parse} - -* Round trip with JSON -* Have to `require "json"` for the method to be added. - -![](examples/json.cr) +![](examples/assign_to_variable.cr) +![](examples/assign_to_variable.out) ## HTTP Client {id: http-client} @@ -192,35 +177,11 @@ crystal spec ![](examples/gravatar.cr) -## JSON to NamedTuple -{id: json-to-namedtuple} -{i: NamedTuple} -{i: from_json} -{i: JSON} - -![](examples/json_to_named_tuple.cr) - -## Reading a JSON file -{id: reading-a-json-file} -{i: NamedTuple} - -![](examples/math.json) -![](examples/read_math_json.cr) - - ## Try Crystal {id: try} ![](examples/try.cr) -## JSON to Array -{id: json-to-array} -{i: JSON} -{i: from_json} - -![](examples/json_to_array.cr) - - ## Check if variable is nil? {id: nil}