Skip to content

Commit

Permalink
separate json chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Jun 11, 2021
1 parent 7abd2ed commit aee1f07
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 54 deletions.
1 change: 1 addition & 0 deletions crystal/crystal.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"classes.md",
"regexes.md",
"testing.md",
"json.md",
"sqlite.md",
"time.md",
"other.md"
Expand Down
8 changes: 8 additions & 0 deletions crystal/examples/assign_to_variable.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
x = "one"
p! x
p! typeof(x)

x = 1
p! x
p! typeof(x)

4 changes: 4 additions & 0 deletions crystal/examples/assign_to_variable.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
x # => "one"
typeof(x) # => String
x # => 1
typeof(x) # => Int32
7 changes: 7 additions & 0 deletions crystal/examples/hashes/hash_and_types.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
person = {
"name" => "Foo Bar",
"number" => 42,
}
p! person
puts typeof(person) # Hash(String, Int32 | String)

2 changes: 2 additions & 0 deletions crystal/examples/hashes/hash_and_types.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
person # => {"name" => "Foo Bar", "number" => 42}
Hash(String, Int32 | String)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 0 additions & 13 deletions crystal/examples/try.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions crystal/hashes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

39 changes: 39 additions & 0 deletions crystal/json.md
Original file line number Diff line number Diff line change
@@ -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)




43 changes: 2 additions & 41 deletions crystal/other.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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}

Expand Down

0 comments on commit aee1f07

Please sign in to comment.