{"id":27237,"date":"2020-05-30T20:29:17","date_gmt":"2020-05-30T18:29:17","guid":{"rendered":"https:\/\/pi3g.com\/?p=27237"},"modified":"2020-05-30T20:29:17","modified_gmt":"2020-05-30T18:29:17","slug":"dealing-with-nil-in-jsonany-in-crystal-lang","status":"publish","type":"post","link":"https:\/\/pi3g.com\/de\/dealing-with-nil-in-jsonany-in-crystal-lang\/","title":{"rendered":"Umgang mit NIL in JSON::Any in Crystal Lang"},"content":{"rendered":"<p>One of the challenges when developing with Crystal is that it is not as forgiving about data types as, say, Python or Ruby.<\/p>\n<p><strong>nil <\/strong>will keep you occupied.<\/p>\n<p>The following error message:<\/p>\n<blockquote>\n<h3>cast from Nil to String failed, at \/usr\/share\/crystal\/src\/json\/any.cr:220:5:220<\/h3>\n<\/blockquote>\n<p>can be solved by:<\/p>\n<blockquote>\n<p># cast to string, THEN check if it is nil<br \/>\nif !(shopify_data[spid][&#8220;billing_address&#8221;][&#8220;address2&#8221;].as_s? == nil)<br \/>&nbsp;&nbsp;&nbsp;&nbsp; name = shopify_data[spid][&#8220;billing_address&#8221;][&#8220;address2&#8221;].as_s<br \/>\nelse<br \/>&nbsp;&nbsp;&nbsp;&nbsp; name = &#8220;fallback_failed [taxgod]&#8221;<br \/>&nbsp;&nbsp;&nbsp;&nbsp; tg_info &lt;&lt; &#8220;shopify_data[spid][\\&#8221;billing_address\\&#8221;][\\&#8221;address2\\&#8221;] for &#8221; + spid + &#8221; is nil &#8221;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br \/>\nend<\/p>\n<\/blockquote>\n<p>\n(The above being a snipped from my internal application taxgod, which talks to the Shopify API \u2013 which will return JSON data, and some of this JSON data might be empty \/ nil.)<\/p>\n<p>As the case is, use as_s? to cast the JSON::Any type to a string or nil, and then check whether it is nil. If it is not nil, then you can proceed with an <strong>as_s<\/strong> cast (which will raise if the underlying value is nil ), and set an alternative value if it is nil.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Eine der Herausforderungen bei der Entwicklung mit Crystal ist, dass es nicht so nachsichtig mit Datentypen umgeht wie z.B. Python oder Ruby. nil wird Sie besch\u00e4ftigen. Die folgende Fehlermeldung: cast from Nil to String failed, at \/usr\/share\/crystal\/src\/json\/any.cr:220:5:220 kann wie folgt gel\u00f6st werden: # cast to string, DANN pr\u00fcfen, ob es nil ist, wenn...<\/p>","protected":false},"author":830,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[416],"tags":[409,477,800],"class_list":["post-27237","post","type-post","status-publish","format-standard","hentry","category-crystal-language","tag-crystal","tag-crystal-lang","tag-language"],"_links":{"self":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27237","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/users\/830"}],"replies":[{"embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/comments?post=27237"}],"version-history":[{"count":1,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27237\/revisions"}],"predecessor-version":[{"id":27238,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/posts\/27237\/revisions\/27238"}],"wp:attachment":[{"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/media?parent=27237"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/categories?post=27237"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pi3g.com\/de\/wp-json\/wp\/v2\/tags?post=27237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}