Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'd recommend that you use the new <a href="http://hackage.haskell.org/package/aeson" rel="noreferrer">aeson</a> package instead of the <a href="http://hackage.haskell.org/package/json" rel="noreferrer">json</a> package, as the former performs much better. Here's how you'd convert a JSON object to a Haskell record, using aeson:</p> <pre><code>{-# LANGUAGE OverloadedStrings #-} module Example where import Control.Applicative import Control.Monad import Data.Aeson data Tweet = Tweet { from_user :: String, to_user_id :: String, profile_image_url :: String, created_at :: String, id_str :: String, source :: String, to_user_id_str :: String, from_user_id_str :: String, from_user_id :: String, text :: String, metadata :: String } instance FromJSON Tweet where parseJSON (Object v) = Tweet &lt;$&gt; v .: "from_user" &lt;*&gt; v .: "to_user_id" &lt;*&gt; v .: "profile_image_url" &lt;*&gt; v .: "created_at" &lt;*&gt; v .: "id_str" &lt;*&gt; v .: "source" &lt;*&gt; v .: "to_user_id_str" &lt;*&gt; v .: "from_user_id_str" &lt;*&gt; v .: "from_user_id" &lt;*&gt; v .: "text" &lt;*&gt; v .: "metadata" -- A non-Object value is of the wrong type, so use mzero to fail. parseJSON _ = mzero </code></pre> <p>Then use <code>Data.Aeson.json</code> to get a <a href="http://hackage.haskell.org/package/attoparsec" rel="noreferrer">attoparsec</a> parser that converts a <code>ByteString</code> into a <code>Value</code>. The call <code>fromJSON</code> on the <code>Value</code> to attempt to parse it into your record. Note that there are two different parsers involved in these two steps, a <code>Data.Attoparsec.Parser</code> parser for converting the <code>ByteString</code> into a generic JSON <code>Value</code> and then a <code>Data.Aeson.Types.Parser</code> parser for converting the JSON value into a record. Note that both steps can fail:</p> <ul> <li>The first parser can fail if the <code>ByteString</code> isn't a valid JSON value.</li> <li>The second parser can fail if the (valid) JSON value doesn't contain one of the fields you mentioned in your <code>fromJSON</code> implementation. </li> </ul> <p>The aeson package prefers the new Unicode type <code>Text</code> (defined in the <a href="http://hackage.haskell.org/package/text" rel="noreferrer">text</a> package) to the more old school <code>String</code> type. The <code>Text</code> type has a much more memory efficient representation than <code>String</code> and generally performs better. I'd recommend that you change the <code>Tweet</code> type to use <code>Text</code> instead of <code>String</code>.</p> <p>If you ever need to convert between <code>String</code> and <code>Text</code>, use the <code>pack</code> and <code>unpack</code> functions defined in <code>Data.Text</code>. Note that such conversions require O(n) time, so avoid them as much as possible (i.e. always use <code>Text</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload