Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Which way is better to handle JSON with Crystal
Search
Takuya Arita
August 01, 2015
Programming
1.1k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Which way is better to handle JSON with Crystal
Takuya Arita
August 01, 2015
More Decks by Takuya Arita
See All by Takuya Arita
Hosted Redash の EOL と移行ツール redash-migrate / You should know about Hosted Redash EOL and redash-migrate
ariarijp
0
3.3k
Redash で踏み出す BI 導入はじめの一歩 / DataEngineeringStudy Redash
ariarijp
1
2.7k
Redash のいま / Redash in 2021
ariarijp
2
1.1k
Redash 導入・運用を4年ぐらいやって変わったこと・変わらなかったこと / Changed and unchanged things with Redash and me
ariarijp
0
1.4k
5年前の自分に伝えたい広告 API 周辺の歩き方 / SOT Tech Night vol.1
ariarijp
1
540
使われていない、を知る / How do we know unused things in the real world?
ariarijp
0
470
MULTI_ORG について / What is MULTI_ORG in Redash
ariarijp
1
3.9k
あらためて振り返る Redash の使いどころ / Why I strongly recommend Redash
ariarijp
2
1.3k
PHP Data Structures
ariarijp
1
570
Other Decks in Programming
See All in Programming
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
350
jQueryをバージョンアップする前に使いたいjQuery Migrate
matsuo_atsushi
0
560
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
110
正しくソフトウェアを作る、前提を疑うための認知の視点 / doubt-premise
minodriven
21
6.8k
RTSPクライアントを自作してみた話
simotin13
0
620
dRuby over BLE
makicamel
2
380
さぁV100、メモリをお食べ・・・
nilpe
0
150
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
200
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
160
Oxlintのカスタムルールの現況
syumai
6
1.1k
その問い、本当に正しいですか?AI時代のエンジニアに必要な哲学と認知科学 / ai-philosophy-cognitive-science
minodriven
11
5.9k
Featured
See All Featured
Between Models and Reality
mayunak
4
340
Optimizing for Happiness
mojombo
378
71k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
220
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
230
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
850
Crafting Experiences
bethany
1
180
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.1k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Everyday Curiosity
cassininazir
0
230
Automating Front-end Workflow
addyosmani
1370
210k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
170
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Transcript
Which way is better to handle JSON with Crystal @ariarijp
w !BSJBSJKQ 5BLVZB"SJUB w $PDPMBCMF *OD w 4PGUXBSF&OHJOFFS "CPVUNF
Today’s Lightning talk
The idea from ཬ.rb 1st anniversary https://nishinipporirb.doorkeeper.jp/events/25958
None
I talked about my first Crystal program
Parse JSON using Crystal
None
I compared Ruby and Crystal
None
Crystal is awesome. But I don’t like Java like type
casting
Next morning
None
@asterite commented on my repository (Amazingly, @asterite read Japanese!) https://github.com/ariarijp/ninirb-1st-anniversary-lt-crystal/commit/2d0062c4a1dc4cabaeb6884591c1771d768c0ce8
As a result
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
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
Use JSON Mapping!
Crystal is Awesome!
end