Slide 1

Slide 1 text

Which way is better to handle JSON with Crystal @ariarijp

Slide 2

Slide 2 text

w !BSJBSJKQ 5BLVZB"SJUB w $PDPMBCMF *OD w 4PGUXBSF&OHJOFFS "CPVUNF

Slide 3

Slide 3 text

Today’s Lightning talk

Slide 4

Slide 4 text

The idea from ੢೔฻ཬ.rb 1st anniversary https://nishinipporirb.doorkeeper.jp/events/25958

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

I talked about my first Crystal program

Slide 7

Slide 7 text

Parse JSON using Crystal

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

I compared Ruby and Crystal

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Crystal is awesome. But I don’t like Java like type casting

Slide 12

Slide 12 text

Next morning

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

@asterite commented on my repository (Amazingly, @asterite read Japanese!) https://github.com/ariarijp/ninirb-1st-anniversary-lt-crystal/commit/2d0062c4a1dc4cabaeb6884591c1771d768c0ce8

Slide 15

Slide 15 text

As a result

Slide 16

Slide 16 text

The legacy way require "http/client" require "json" place = ARGV[0] url = "http://api.openweathermap.org/data/2.5/weather?q=#{place}" resp = HTTP::Client.get(url) weather = JSON.parse(resp.body) pp (((weather as Hash)["weather"] as Array)[0] as Hash)["main"] puts "-" * 80 pp weather

Slide 17

Slide 17 text

Crystal way require "http/client" require "json" class WeatherResponse json_mapping({ weather: Array(Weather), }) class Weather json_mapping({ id: Int32, main: String, description: String, icon: String, }) end end place = ARGV[0] url = "http://api.openweathermap.org/data/2.5/weather?q=#{place}" resp = HTTP::Client.get(url) puts "-" * 80 weather_resp = WeatherResponse.from_json(resp.body) pp weather_resp pp weather_resp.weather[0].main

Slide 18

Slide 18 text

Use JSON Mapping!

Slide 19

Slide 19 text

Crystal is Awesome!

Slide 20

Slide 20 text

end