    {-# LANGUAGE CPP                        #-}
    {-# LANGUAGE DeriveGeneric              #-}
    {-# LANGUAGE FlexibleContexts           #-}
    {-# LANGUAGE FlexibleInstances          #-}
    {-# LANGUAGE GeneralizedNewtypeDeriving #-}
    {-# LANGUAGE NoImplicitPrelude          #-}
    {-# LANGUAGE OverloadedStrings          #-}
    {-# LANGUAGE ScopedTypeVariables        #-}
    {- |
       Module      : Data.Ipynb
       Copyright   : Copyright (C) 2019 John MacFarlane
       License     : BSD3

       Maintainer  : John MacFarlane <jgm@berkeley.edu>
       Stability   : alpha
       Portability : portable

    Data structure and JSON serializers for ipynb (Jupyter notebook) format.
    Version 4 of the format is documented here:
    <https://nbformat.readthedocs.io/en/latest/format_description.html>.

    The library supports both version 4 ('Notebook NbV4') and version 3
    ('Notebook NbV3') of nbformat.  Note that this is a phantom type: the
    `NbV3` or `NbV4` parameter only affects JSON serialization,
    not the data structure itself.  So code that manipulates
    notebooks can be polymorphic, operating on `Notebook a`.

    -}
    module Data.Ipynb ( Notebook(..)
                      , NbV3
                      , NbV4
                      , JSONMeta(..)
                      , Cell(..)
                      , Source(..)
                      , CellType(..)
                      , Output(..)
                      , MimeType
                      , MimeData(..)
                      , MimeBundle(..)
                      , MimeAttachments(..)
                      , breakLines
                      )
    where
    import Control.Applicative ((<|>))
    import Control.Monad (when)
    import Data.Aeson as Aeson
    import qualified Data.Aeson.Types as Aeson
    import Data.ByteString (ByteString)
    import qualified Data.ByteString as B
    import qualified Data.ByteString.Base64 as Base64
    import Data.Char (isSpace)
    import Data.List (partition, sortOn)
    import qualified Data.Map as M
    import Data.Maybe (mapMaybe)
    import Data.Text (Text)
    import qualified Data.Text as T
    import qualified Data.Text.Encoding as TE
    import GHC.Generics
    import Prelude
    #if MIN_VERSION_base(4,11,0)
    #else
    import Data.Semigroup
    #endif
    #if MIN_VERSION_aeson(2,0,0)
    import qualified Data.Aeson.KeyMap as KM
    #else
    import qualified Data.HashMap.Strict as KM
    #endif
    import Data.String
    import qualified Data.Set as Set

    -- | Indexes 'Notebook' for serialization as nbformat version 3.
    data NbV3

    -- | Indexes 'Notebook' for serialization as nbformat version 4.
    data NbV4

    -- | A Jupyter notebook.
    data Notebook a = Notebook
      { forall a. Notebook a -> JSONMeta
    notebookMetadata :: JSONMeta
      , forall a. Notebook a -> (Int, Int)
    notebookFormat   :: (Int, Int)
      , forall a. Notebook a -> [Cell a]
    notebookCells    :: [Cell a]
      } deriving (Int -> Notebook a -> ShowS
    [Notebook a] -> ShowS
    Notebook a -> String
    (Int -> Notebook a -> ShowS)
    -> (Notebook a -> String)
    -> ([Notebook a] -> ShowS)
    -> Show (Notebook a)
    forall a. Int -> Notebook a -> ShowS
    forall a. [Notebook a] -> ShowS
    forall a. Notebook a -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall a. Int -> Notebook a -> ShowS
    showsPrec :: Int -> Notebook a -> ShowS
    $cshow :: forall a. Notebook a -> String
    show :: Notebook a -> String
    $cshowList :: forall a. [Notebook a] -> ShowS
    showList :: [Notebook a] -> ShowS
    Show, Notebook a -> Notebook a -> Bool
    (Notebook a -> Notebook a -> Bool)
    -> (Notebook a -> Notebook a -> Bool) -> Eq (Notebook a)
    forall a. Notebook a -> Notebook a -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall a. Notebook a -> Notebook a -> Bool
    == :: Notebook a -> Notebook a -> Bool
    $c/= :: forall a. Notebook a -> Notebook a -> Bool
    /= :: Notebook a -> Notebook a -> Bool
    Eq, (forall x. Notebook a -> Rep (Notebook a) x)
    -> (forall x. Rep (Notebook a) x -> Notebook a)
    -> Generic (Notebook a)
    forall x. Rep (Notebook a) x -> Notebook a
    forall x. Notebook a -> Rep (Notebook a) x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    forall a x. Rep (Notebook a) x -> Notebook a
    forall a x. Notebook a -> Rep (Notebook a) x
    $cfrom :: forall a x. Notebook a -> Rep (Notebook a) x
    from :: forall x. Notebook a -> Rep (Notebook a) x
    $cto :: forall a x. Rep (Notebook a) x -> Notebook a
    to :: forall x. Rep (Notebook a) x -> Notebook a
    Generic)

    instance Semigroup (Notebook a) where
      Notebook JSONMeta
    m1 (Int, Int)
    f1 [Cell a]
    c1 <> :: Notebook a -> Notebook a -> Notebook a
    <> Notebook JSONMeta
    m2 (Int, Int)
    f2 [Cell a]
    c2 =
        JSONMeta -> (Int, Int) -> [Cell a] -> Notebook a
    forall a. JSONMeta -> (Int, Int) -> [Cell a] -> Notebook a
    Notebook (JSONMeta
    m1 JSONMeta -> JSONMeta -> JSONMeta
    forall a. Semigroup a => a -> a -> a
    <> JSONMeta
    m2) ((Int, Int) -> (Int, Int) -> (Int, Int)
    forall a. Ord a => a -> a -> a
    max (Int, Int)
    f1 (Int, Int)
    f2) ([Cell a]
    c1 [Cell a] -> [Cell a] -> [Cell a]
    forall a. Semigroup a => a -> a -> a
    <> [Cell a]
    c2)

    instance Monoid (Notebook a) where
      mempty :: Notebook a
    mempty = JSONMeta -> (Int, Int) -> [Cell a] -> Notebook a
    forall a. JSONMeta -> (Int, Int) -> [Cell a] -> Notebook a
    Notebook JSONMeta
    forall a. Monoid a => a
    mempty (Int
    0, Int
    0) [Cell a]
    forall a. Monoid a => a
    mempty
    #if MIN_VERSION_base(4,11,0)
    #else
      mappend = (<>)
    #endif

    instance FromJSON (Notebook NbV4) where
      parseJSON :: Value -> Parser (Notebook NbV4)
    parseJSON = String
    -> (Object -> Parser (Notebook NbV4))
    -> Value
    -> Parser (Notebook NbV4)
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
    withObject String
    "Notebook" ((Object -> Parser (Notebook NbV4))
     -> Value -> Parser (Notebook NbV4))
    -> (Object -> Parser (Notebook NbV4))
    -> Value
    -> Parser (Notebook NbV4)
    forall a b. (a -> b) -> a -> b
    $ \Object
    v -> do
        fmt <- Object
    v Object -> Key -> Parser (Maybe Int)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "nbformat" Parser (Maybe Int) -> Int -> Parser Int
    forall a. Parser (Maybe a) -> a -> Parser a
    .!= Int
    0
        when (fmt < 4 || fmt > 4) $ fail "expected nbformat == 4"
        fmtminor <- v .:? "nbformat_minor" .!= 0
        metadata <- v .:? "metadata" .!= mempty
        cells <- v .: "cells"
        return
          Notebook{ notebookMetadata = metadata
                  , notebookFormat = (fmt, fmtminor)
                  , notebookCells    = cells
                  }

    instance FromJSON (Notebook NbV3) where
      parseJSON :: Value -> Parser (Notebook NbV3)
    parseJSON = String
    -> (Object -> Parser (Notebook NbV3))
    -> Value
    -> Parser (Notebook NbV3)
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
    withObject String
    "Notebook" ((Object -> Parser (Notebook NbV3))
     -> Value -> Parser (Notebook NbV3))
    -> (Object -> Parser (Notebook NbV3))
    -> Value
    -> Parser (Notebook NbV3)
    forall a b. (a -> b) -> a -> b
    $ \Object
    v -> do
        fmt <- Object
    v Object -> Key -> Parser (Maybe Int)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "nbformat" Parser (Maybe Int) -> Int -> Parser Int
    forall a. Parser (Maybe a) -> a -> Parser a
    .!= Int
    0
        when (fmt > 3) $ fail "expected nbformat <= 3"
        fmtminor <- v .:? "nbformat_minor" .!= 0
        metadata <- v .:? "metadata" .!= mempty
        worksheets <- v .: "worksheets"
        -- NOTE: we ignore metadata on worksheets: is this ever used?
        cells <- mconcat <$> mapM (.: "cells") worksheets
        return
          Notebook{ notebookMetadata = metadata
                  , notebookFormat = (fmt, fmtminor)
                  , notebookCells = cells
                  }

    instance ToJSON (Notebook NbV4) where
     toJSON :: Notebook NbV4 -> Value
    toJSON Notebook NbV4
    n = [Pair] -> Value
    object
       [ Key
    "nbformat" Key -> Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Int, Int) -> Int
    forall a b. (a, b) -> a
    fst (Notebook NbV4 -> (Int, Int)
    forall a. Notebook a -> (Int, Int)
    notebookFormat Notebook NbV4
    n)
       , Key
    "nbformat_minor" Key -> Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Int, Int) -> Int
    forall a b. (a, b) -> b
    snd (Notebook NbV4 -> (Int, Int)
    forall a. Notebook a -> (Int, Int)
    notebookFormat Notebook NbV4
    n)
       , Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Notebook NbV4 -> JSONMeta
    forall a. Notebook a -> JSONMeta
    notebookMetadata Notebook NbV4
    n
       , Key
    "cells" Key -> [Cell NbV4] -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (if Notebook NbV4 -> (Int, Int)
    forall a. Notebook a -> (Int, Int)
    notebookFormat Notebook NbV4
    n (Int, Int) -> (Int, Int) -> Bool
    forall a. Ord a => a -> a -> Bool
    >= (Int
    4,Int
    1)
                        then [Cell NbV4] -> [Cell NbV4]
    forall a. a -> a
    id
                        else (Cell NbV4 -> Cell NbV4) -> [Cell NbV4] -> [Cell NbV4]
    forall a b. (a -> b) -> [a] -> [b]
    map (\Cell NbV4
    c -> Cell NbV4
    c{ cellAttachments = Nothing }))
                    (Notebook NbV4 -> [Cell NbV4]
    forall a. Notebook a -> [Cell a]
    notebookCells Notebook NbV4
    n)
       ]

    instance ToJSON (Notebook NbV3) where
     toJSON :: Notebook NbV3 -> Value
    toJSON Notebook NbV3
    n = [Pair] -> Value
    object
       [ Key
    "nbformat" Key -> Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Int, Int) -> Int
    forall a b. (a, b) -> a
    fst (Notebook NbV3 -> (Int, Int)
    forall a. Notebook a -> (Int, Int)
    notebookFormat Notebook NbV3
    n)
       , Key
    "nbformat_minor" Key -> Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Int, Int) -> Int
    forall a b. (a, b) -> b
    snd (Notebook NbV3 -> (Int, Int)
    forall a. Notebook a -> (Int, Int)
    notebookFormat Notebook NbV3
    n)
       , Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Notebook NbV3 -> JSONMeta
    forall a. Notebook a -> JSONMeta
    notebookMetadata Notebook NbV3
    n
       , Key
    "worksheets" Key -> [Value] -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .=
         [ [Pair] -> Value
    object
           [ Key
    "cells" Key -> [Cell NbV3] -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (if Notebook NbV3 -> (Int, Int)
    forall a. Notebook a -> (Int, Int)
    notebookFormat Notebook NbV3
    n (Int, Int) -> (Int, Int) -> Bool
    forall a. Ord a => a -> a -> Bool
    >= (Int
    4,Int
    1)
                            then [Cell NbV3] -> [Cell NbV3]
    forall a. a -> a
    id
                            else (Cell NbV3 -> Cell NbV3) -> [Cell NbV3] -> [Cell NbV3]
    forall a b. (a -> b) -> [a] -> [b]
    map (\Cell NbV3
    c -> Cell NbV3
    c{ cellAttachments = Nothing }))
                        (Notebook NbV3 -> [Cell NbV3]
    forall a. Notebook a -> [Cell a]
    notebookCells Notebook NbV3
    n)
           , Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (JSONMeta
    forall a. Monoid a => a
    mempty :: JSONMeta) -- see above in FromJSON instance
           ]
         ]
       ]

    newtype JSONMeta = JSONMeta (M.Map Text Value)
      deriving (Int -> JSONMeta -> ShowS
    [JSONMeta] -> ShowS
    JSONMeta -> String
    (Int -> JSONMeta -> ShowS)
    -> (JSONMeta -> String) -> ([JSONMeta] -> ShowS) -> Show JSONMeta
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> JSONMeta -> ShowS
    showsPrec :: Int -> JSONMeta -> ShowS
    $cshow :: JSONMeta -> String
    show :: JSONMeta -> String
    $cshowList :: [JSONMeta] -> ShowS
    showList :: [JSONMeta] -> ShowS
    Show, JSONMeta -> JSONMeta -> Bool
    (JSONMeta -> JSONMeta -> Bool)
    -> (JSONMeta -> JSONMeta -> Bool) -> Eq JSONMeta
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: JSONMeta -> JSONMeta -> Bool
    == :: JSONMeta -> JSONMeta -> Bool
    $c/= :: JSONMeta -> JSONMeta -> Bool
    /= :: JSONMeta -> JSONMeta -> Bool
    Eq, Eq JSONMeta
    Eq JSONMeta =>
    (JSONMeta -> JSONMeta -> Ordering)
    -> (JSONMeta -> JSONMeta -> Bool)
    -> (JSONMeta -> JSONMeta -> Bool)
    -> (JSONMeta -> JSONMeta -> Bool)
    -> (JSONMeta -> JSONMeta -> Bool)
    -> (JSONMeta -> JSONMeta -> JSONMeta)
    -> (JSONMeta -> JSONMeta -> JSONMeta)
    -> Ord JSONMeta
    JSONMeta -> JSONMeta -> Bool
    JSONMeta -> JSONMeta -> Ordering
    JSONMeta -> JSONMeta -> JSONMeta
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: JSONMeta -> JSONMeta -> Ordering
    compare :: JSONMeta -> JSONMeta -> Ordering
    $c< :: JSONMeta -> JSONMeta -> Bool
    < :: JSONMeta -> JSONMeta -> Bool
    $c<= :: JSONMeta -> JSONMeta -> Bool
    <= :: JSONMeta -> JSONMeta -> Bool
    $c> :: JSONMeta -> JSONMeta -> Bool
    > :: JSONMeta -> JSONMeta -> Bool
    $c>= :: JSONMeta -> JSONMeta -> Bool
    >= :: JSONMeta -> JSONMeta -> Bool
    $cmax :: JSONMeta -> JSONMeta -> JSONMeta
    max :: JSONMeta -> JSONMeta -> JSONMeta
    $cmin :: JSONMeta -> JSONMeta -> JSONMeta
    min :: JSONMeta -> JSONMeta -> JSONMeta
    Ord, (forall x. JSONMeta -> Rep JSONMeta x)
    -> (forall x. Rep JSONMeta x -> JSONMeta) -> Generic JSONMeta
    forall x. Rep JSONMeta x -> JSONMeta
    forall x. JSONMeta -> Rep JSONMeta x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. JSONMeta -> Rep JSONMeta x
    from :: forall x. JSONMeta -> Rep JSONMeta x
    $cto :: forall x. Rep JSONMeta x -> JSONMeta
    to :: forall x. Rep JSONMeta x -> JSONMeta
    Generic, NonEmpty JSONMeta -> JSONMeta
    JSONMeta -> JSONMeta -> JSONMeta
    (JSONMeta -> JSONMeta -> JSONMeta)
    -> (NonEmpty JSONMeta -> JSONMeta)
    -> (forall b. Integral b => b -> JSONMeta -> JSONMeta)
    -> Semigroup JSONMeta
    forall b. Integral b => b -> JSONMeta -> JSONMeta
    forall a.
    (a -> a -> a)
    -> (NonEmpty a -> a)
    -> (forall b. Integral b => b -> a -> a)
    -> Semigroup a
    $c<> :: JSONMeta -> JSONMeta -> JSONMeta
    <> :: JSONMeta -> JSONMeta -> JSONMeta
    $csconcat :: NonEmpty JSONMeta -> JSONMeta
    sconcat :: NonEmpty JSONMeta -> JSONMeta
    $cstimes :: forall b. Integral b => b -> JSONMeta -> JSONMeta
    stimes :: forall b. Integral b => b -> JSONMeta -> JSONMeta
    Semigroup, Semigroup JSONMeta
    JSONMeta
    Semigroup JSONMeta =>
    JSONMeta
    -> (JSONMeta -> JSONMeta -> JSONMeta)
    -> ([JSONMeta] -> JSONMeta)
    -> Monoid JSONMeta
    [JSONMeta] -> JSONMeta
    JSONMeta -> JSONMeta -> JSONMeta
    forall a.
    Semigroup a =>
    a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
    $cmempty :: JSONMeta
    mempty :: JSONMeta
    $cmappend :: JSONMeta -> JSONMeta -> JSONMeta
    mappend :: JSONMeta -> JSONMeta -> JSONMeta
    $cmconcat :: [JSONMeta] -> JSONMeta
    mconcat :: [JSONMeta] -> JSONMeta
    Monoid, Maybe JSONMeta
    Value -> Parser [JSONMeta]
    Value -> Parser JSONMeta
    (Value -> Parser JSONMeta)
    -> (Value -> Parser [JSONMeta])
    -> Maybe JSONMeta
    -> FromJSON JSONMeta
    forall a.
    (Value -> Parser a)
    -> (Value -> Parser [a]) -> Maybe a -> FromJSON a
    $cparseJSON :: Value -> Parser JSONMeta
    parseJSON :: Value -> Parser JSONMeta
    $cparseJSONList :: Value -> Parser [JSONMeta]
    parseJSONList :: Value -> Parser [JSONMeta]
    $comittedField :: Maybe JSONMeta
    omittedField :: Maybe JSONMeta
    FromJSON)

    instance ToJSON JSONMeta where
       toJSON :: JSONMeta -> Value
    toJSON = Options -> JSONMeta -> Value
    forall a.
    (Generic a, GToJSON' Value Zero (Rep a)) =>
    Options -> a -> Value
    genericToJSON Options
    defaultOptions
       toEncoding :: JSONMeta -> Encoding
    toEncoding = Options -> JSONMeta -> Encoding
    forall a.
    (Generic a, GToJSON' Encoding Zero (Rep a)) =>
    Options -> a -> Encoding
    genericToEncoding Options
    defaultOptions

    -- | A 'Source' is a textual content which may be
    -- represented in JSON either as a single string
    -- or as a list of strings (which are concatenated).
    newtype Source = Source{ Source -> [Text]
    unSource :: [Text] }
      deriving (Int -> Source -> ShowS
    [Source] -> ShowS
    Source -> String
    (Int -> Source -> ShowS)
    -> (Source -> String) -> ([Source] -> ShowS) -> Show Source
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> Source -> ShowS
    showsPrec :: Int -> Source -> ShowS
    $cshow :: Source -> String
    show :: Source -> String
    $cshowList :: [Source] -> ShowS
    showList :: [Source] -> ShowS
    Show, Source -> Source -> Bool
    (Source -> Source -> Bool)
    -> (Source -> Source -> Bool) -> Eq Source
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: Source -> Source -> Bool
    == :: Source -> Source -> Bool
    $c/= :: Source -> Source -> Bool
    /= :: Source -> Source -> Bool
    Eq, (forall x. Source -> Rep Source x)
    -> (forall x. Rep Source x -> Source) -> Generic Source
    forall x. Rep Source x -> Source
    forall x. Source -> Rep Source x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. Source -> Rep Source x
    from :: forall x. Source -> Rep Source x
    $cto :: forall x. Rep Source x -> Source
    to :: forall x. Rep Source x -> Source
    Generic, NonEmpty Source -> Source
    Source -> Source -> Source
    (Source -> Source -> Source)
    -> (NonEmpty Source -> Source)
    -> (forall b. Integral b => b -> Source -> Source)
    -> Semigroup Source
    forall b. Integral b => b -> Source -> Source
    forall a.
    (a -> a -> a)
    -> (NonEmpty a -> a)
    -> (forall b. Integral b => b -> a -> a)
    -> Semigroup a
    $c<> :: Source -> Source -> Source
    <> :: Source -> Source -> Source
    $csconcat :: NonEmpty Source -> Source
    sconcat :: NonEmpty Source -> Source
    $cstimes :: forall b. Integral b => b -> Source -> Source
    stimes :: forall b. Integral b => b -> Source -> Source
    Semigroup, Semigroup Source
    Source
    Semigroup Source =>
    Source
    -> (Source -> Source -> Source)
    -> ([Source] -> Source)
    -> Monoid Source
    [Source] -> Source
    Source -> Source -> Source
    forall a.
    Semigroup a =>
    a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
    $cmempty :: Source
    mempty :: Source
    $cmappend :: Source -> Source -> Source
    mappend :: Source -> Source -> Source
    $cmconcat :: [Source] -> Source
    mconcat :: [Source] -> Source
    Monoid)

    instance FromJSON Source where
      parseJSON :: Value -> Parser Source
    parseJSON Value
    v = do
        ts <- Value -> Parser [Text]
    forall a. FromJSON a => Value -> Parser a
    parseJSON Value
    v Parser [Text] -> Parser [Text] -> Parser [Text]
    forall a. Parser a -> Parser a -> Parser a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> (Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[]) (Text -> [Text]) -> Parser Text -> Parser [Text]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Value -> Parser Text
    forall a. FromJSON a => Value -> Parser a
    parseJSON Value
    v
        return $ Source ts

    instance ToJSON Source where
      toJSON :: Source -> Value
    toJSON (Source [Text]
    ts) = [Text] -> Value
    forall a. ToJSON a => a -> Value
    toJSON [Text]
    ts

    newtype MimeAttachments = MimeAttachments (M.Map Text MimeBundle)
      deriving (Int -> MimeAttachments -> ShowS
    [MimeAttachments] -> ShowS
    MimeAttachments -> String
    (Int -> MimeAttachments -> ShowS)
    -> (MimeAttachments -> String)
    -> ([MimeAttachments] -> ShowS)
    -> Show MimeAttachments
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> MimeAttachments -> ShowS
    showsPrec :: Int -> MimeAttachments -> ShowS
    $cshow :: MimeAttachments -> String
    show :: MimeAttachments -> String
    $cshowList :: [MimeAttachments] -> ShowS
    showList :: [MimeAttachments] -> ShowS
    Show, MimeAttachments -> MimeAttachments -> Bool
    (MimeAttachments -> MimeAttachments -> Bool)
    -> (MimeAttachments -> MimeAttachments -> Bool)
    -> Eq MimeAttachments
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: MimeAttachments -> MimeAttachments -> Bool
    == :: MimeAttachments -> MimeAttachments -> Bool
    $c/= :: MimeAttachments -> MimeAttachments -> Bool
    /= :: MimeAttachments -> MimeAttachments -> Bool
    Eq, Eq MimeAttachments
    Eq MimeAttachments =>
    (MimeAttachments -> MimeAttachments -> Ordering)
    -> (MimeAttachments -> MimeAttachments -> Bool)
    -> (MimeAttachments -> MimeAttachments -> Bool)
    -> (MimeAttachments -> MimeAttachments -> Bool)
    -> (MimeAttachments -> MimeAttachments -> Bool)
    -> (MimeAttachments -> MimeAttachments -> MimeAttachments)
    -> (MimeAttachments -> MimeAttachments -> MimeAttachments)
    -> Ord MimeAttachments
    MimeAttachments -> MimeAttachments -> Bool
    MimeAttachments -> MimeAttachments -> Ordering
    MimeAttachments -> MimeAttachments -> MimeAttachments
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: MimeAttachments -> MimeAttachments -> Ordering
    compare :: MimeAttachments -> MimeAttachments -> Ordering
    $c< :: MimeAttachments -> MimeAttachments -> Bool
    < :: MimeAttachments -> MimeAttachments -> Bool
    $c<= :: MimeAttachments -> MimeAttachments -> Bool
    <= :: MimeAttachments -> MimeAttachments -> Bool
    $c> :: MimeAttachments -> MimeAttachments -> Bool
    > :: MimeAttachments -> MimeAttachments -> Bool
    $c>= :: MimeAttachments -> MimeAttachments -> Bool
    >= :: MimeAttachments -> MimeAttachments -> Bool
    $cmax :: MimeAttachments -> MimeAttachments -> MimeAttachments
    max :: MimeAttachments -> MimeAttachments -> MimeAttachments
    $cmin :: MimeAttachments -> MimeAttachments -> MimeAttachments
    min :: MimeAttachments -> MimeAttachments -> MimeAttachments
    Ord, (forall x. MimeAttachments -> Rep MimeAttachments x)
    -> (forall x. Rep MimeAttachments x -> MimeAttachments)
    -> Generic MimeAttachments
    forall x. Rep MimeAttachments x -> MimeAttachments
    forall x. MimeAttachments -> Rep MimeAttachments x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. MimeAttachments -> Rep MimeAttachments x
    from :: forall x. MimeAttachments -> Rep MimeAttachments x
    $cto :: forall x. Rep MimeAttachments x -> MimeAttachments
    to :: forall x. Rep MimeAttachments x -> MimeAttachments
    Generic, NonEmpty MimeAttachments -> MimeAttachments
    MimeAttachments -> MimeAttachments -> MimeAttachments
    (MimeAttachments -> MimeAttachments -> MimeAttachments)
    -> (NonEmpty MimeAttachments -> MimeAttachments)
    -> (forall b.
        Integral b =>
        b -> MimeAttachments -> MimeAttachments)
    -> Semigroup MimeAttachments
    forall b. Integral b => b -> MimeAttachments -> MimeAttachments
    forall a.
    (a -> a -> a)
    -> (NonEmpty a -> a)
    -> (forall b. Integral b => b -> a -> a)
    -> Semigroup a
    $c<> :: MimeAttachments -> MimeAttachments -> MimeAttachments
    <> :: MimeAttachments -> MimeAttachments -> MimeAttachments
    $csconcat :: NonEmpty MimeAttachments -> MimeAttachments
    sconcat :: NonEmpty MimeAttachments -> MimeAttachments
    $cstimes :: forall b. Integral b => b -> MimeAttachments -> MimeAttachments
    stimes :: forall b. Integral b => b -> MimeAttachments -> MimeAttachments
    Semigroup, Semigroup MimeAttachments
    MimeAttachments
    Semigroup MimeAttachments =>
    MimeAttachments
    -> (MimeAttachments -> MimeAttachments -> MimeAttachments)
    -> ([MimeAttachments] -> MimeAttachments)
    -> Monoid MimeAttachments
    [MimeAttachments] -> MimeAttachments
    MimeAttachments -> MimeAttachments -> MimeAttachments
    forall a.
    Semigroup a =>
    a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
    $cmempty :: MimeAttachments
    mempty :: MimeAttachments
    $cmappend :: MimeAttachments -> MimeAttachments -> MimeAttachments
    mappend :: MimeAttachments -> MimeAttachments -> MimeAttachments
    $cmconcat :: [MimeAttachments] -> MimeAttachments
    mconcat :: [MimeAttachments] -> MimeAttachments
    Monoid, Maybe MimeAttachments
    Value -> Parser [MimeAttachments]
    Value -> Parser MimeAttachments
    (Value -> Parser MimeAttachments)
    -> (Value -> Parser [MimeAttachments])
    -> Maybe MimeAttachments
    -> FromJSON MimeAttachments
    forall a.
    (Value -> Parser a)
    -> (Value -> Parser [a]) -> Maybe a -> FromJSON a
    $cparseJSON :: Value -> Parser MimeAttachments
    parseJSON :: Value -> Parser MimeAttachments
    $cparseJSONList :: Value -> Parser [MimeAttachments]
    parseJSONList :: Value -> Parser [MimeAttachments]
    $comittedField :: Maybe MimeAttachments
    omittedField :: Maybe MimeAttachments
    FromJSON)

    instance ToJSON MimeAttachments where
       toJSON :: MimeAttachments -> Value
    toJSON = Options -> MimeAttachments -> Value
    forall a.
    (Generic a, GToJSON' Value Zero (Rep a)) =>
    Options -> a -> Value
    genericToJSON Options
    defaultOptions
       toEncoding :: MimeAttachments -> Encoding
    toEncoding = Options -> MimeAttachments -> Encoding
    forall a.
    (Generic a, GToJSON' Encoding Zero (Rep a)) =>
    Options -> a -> Encoding
    genericToEncoding Options
    defaultOptions

    -- | A Jupyter notebook cell.
    data Cell a = Cell
      { forall a. Cell a -> CellType a
    cellType        :: CellType a
      , forall a. Cell a -> Maybe Text
    cellId          :: Maybe Text
      , forall a. Cell a -> Source
    cellSource      :: Source
      , forall a. Cell a -> JSONMeta
    cellMetadata    :: JSONMeta
      , forall a. Cell a -> Maybe MimeAttachments
    cellAttachments :: Maybe MimeAttachments
    } deriving (Int -> Cell a -> ShowS
    [Cell a] -> ShowS
    Cell a -> String
    (Int -> Cell a -> ShowS)
    -> (Cell a -> String) -> ([Cell a] -> ShowS) -> Show (Cell a)
    forall a. Int -> Cell a -> ShowS
    forall a. [Cell a] -> ShowS
    forall a. Cell a -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall a. Int -> Cell a -> ShowS
    showsPrec :: Int -> Cell a -> ShowS
    $cshow :: forall a. Cell a -> String
    show :: Cell a -> String
    $cshowList :: forall a. [Cell a] -> ShowS
    showList :: [Cell a] -> ShowS
    Show, Cell a -> Cell a -> Bool
    (Cell a -> Cell a -> Bool)
    -> (Cell a -> Cell a -> Bool) -> Eq (Cell a)
    forall a. Cell a -> Cell a -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall a. Cell a -> Cell a -> Bool
    == :: Cell a -> Cell a -> Bool
    $c/= :: forall a. Cell a -> Cell a -> Bool
    /= :: Cell a -> Cell a -> Bool
    Eq, (forall x. Cell a -> Rep (Cell a) x)
    -> (forall x. Rep (Cell a) x -> Cell a) -> Generic (Cell a)
    forall x. Rep (Cell a) x -> Cell a
    forall x. Cell a -> Rep (Cell a) x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    forall a x. Rep (Cell a) x -> Cell a
    forall a x. Cell a -> Rep (Cell a) x
    $cfrom :: forall a x. Cell a -> Rep (Cell a) x
    from :: forall x. Cell a -> Rep (Cell a) x
    $cto :: forall a x. Rep (Cell a) x -> Cell a
    to :: forall x. Rep (Cell a) x -> Cell a
    Generic)

    instance FromJSON (Cell NbV4) where
      parseJSON :: Value -> Parser (Cell NbV4)
    parseJSON = String
    -> (Object -> Parser (Cell NbV4)) -> Value -> Parser (Cell NbV4)
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
    withObject String
    "Cell" ((Object -> Parser (Cell NbV4)) -> Value -> Parser (Cell NbV4))
    -> (Object -> Parser (Cell NbV4)) -> Value -> Parser (Cell NbV4)
    forall a b. (a -> b) -> a -> b
    $ \Object
    v -> do
        ty <- Object
    v Object -> Key -> Parser String
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "cell_type"
        cell_id <- v.:? "id"
        cell_type <-
          case ty of
            String
    "markdown" -> CellType NbV4 -> Parser (CellType NbV4)
    forall a. a -> Parser a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure CellType NbV4
    forall a. CellType a
    Markdown
            String
    "raw" -> CellType NbV4 -> Parser (CellType NbV4)
    forall a. a -> Parser a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure CellType NbV4
    forall a. CellType a
    Raw
            String
    "code" ->
              Maybe Int -> [Output NbV4] -> CellType NbV4
    forall a. Maybe Int -> [Output a] -> CellType a
    Code
                (Maybe Int -> [Output NbV4] -> CellType NbV4)
    -> Parser (Maybe Int) -> Parser ([Output NbV4] -> CellType NbV4)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser (Maybe Int)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "execution_count"
                Parser ([Output NbV4] -> CellType NbV4)
    -> Parser [Output NbV4] -> Parser (CellType NbV4)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser [Output NbV4]
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "outputs"
            String
    _ -> String -> Parser (CellType NbV4)
    forall a. String -> Parser a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> Parser (CellType NbV4))
    -> String -> Parser (CellType NbV4)
    forall a b. (a -> b) -> a -> b
    $ String
    "Unknown cell_type " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ String
    ty
        metadata <- v .: "metadata"
        attachments <- v .:? "attachments"
        source <- v .: "source"
        return
          Cell{ cellType = cell_type
              , cellId = cell_id
              , cellMetadata = metadata
              , cellAttachments = attachments
              , cellSource = source
              }

    instance FromJSON (Cell NbV3) where
      parseJSON :: Value -> Parser (Cell NbV3)
    parseJSON = String
    -> (Object -> Parser (Cell NbV3)) -> Value -> Parser (Cell NbV3)
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
    withObject String
    "Cell" ((Object -> Parser (Cell NbV3)) -> Value -> Parser (Cell NbV3))
    -> (Object -> Parser (Cell NbV3)) -> Value -> Parser (Cell NbV3)
    forall a b. (a -> b) -> a -> b
    $ \Object
    v -> do
        ty <- Object
    v Object -> Key -> Parser String
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "cell_type"
        cell_type <-
          case ty of
            String
    "markdown" -> CellType NbV3 -> Parser (CellType NbV3)
    forall a. a -> Parser a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure CellType NbV3
    forall a. CellType a
    Markdown
            String
    "heading" -> Int -> CellType NbV3
    forall a. Int -> CellType a
    Heading (Int -> CellType NbV3) -> Parser Int -> Parser (CellType NbV3)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser Int
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "level"
            String
    "raw" -> CellType NbV3 -> Parser (CellType NbV3)
    forall a. a -> Parser a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure CellType NbV3
    forall a. CellType a
    Raw
            String
    "code" ->
              Maybe Int -> [Output NbV3] -> CellType NbV3
    forall a. Maybe Int -> [Output a] -> CellType a
    Code
                (Maybe Int -> [Output NbV3] -> CellType NbV3)
    -> Parser (Maybe Int) -> Parser ([Output NbV3] -> CellType NbV3)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser (Maybe Int)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "prompt_number"
                Parser ([Output NbV3] -> CellType NbV3)
    -> Parser [Output NbV3] -> Parser (CellType NbV3)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser [Output NbV3]
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "outputs"
            String
    _ -> String -> Parser (CellType NbV3)
    forall a. String -> Parser a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> Parser (CellType NbV3))
    -> String -> Parser (CellType NbV3)
    forall a b. (a -> b) -> a -> b
    $ String
    "Unknown cell_type " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ String
    ty
        metadata <- parseV3Metadata v
        source <- if ty == "code"
                     then v .: "input"
                     else v .: "source"
        return
          Cell{ cellType = cell_type
              , cellId = Nothing
              , cellMetadata = metadata
              , cellAttachments = Nothing
              , cellSource = source
              }

    -- note that execution_count can't be omitted!
    instance ToJSON (Cell NbV4) where
     toJSON :: Cell NbV4 -> Value
    toJSON Cell NbV4
    c = [Pair] -> Value
    object ([Pair] -> Value) -> [Pair] -> Value
    forall a b. (a -> b) -> a -> b
    $
       (Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV4 -> JSONMeta
    forall a. Cell a -> JSONMeta
    cellMetadata Cell NbV4
    c) Pair -> [Pair] -> [Pair]
    forall a. a -> [a] -> [a]
    :
       [Pair] -> (Text -> [Pair]) -> Maybe Text -> [Pair]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\Text
    x -> [Key
    "id" Key -> Maybe Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV4 -> Maybe Text
    forall a. Cell a -> Maybe Text
    cellId Cell NbV4
    c]) (Cell NbV4 -> Maybe Text
    forall a. Cell a -> Maybe Text
    cellId Cell NbV4
    c) [Pair] -> [Pair] -> [Pair]
    forall a. [a] -> [a] -> [a]
    ++
       [Pair]
    -> (MimeAttachments -> [Pair]) -> Maybe MimeAttachments -> [Pair]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\MimeAttachments
    x -> [Key
    "attachments" Key -> MimeAttachments -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= MimeAttachments
    x]) (Cell NbV4 -> Maybe MimeAttachments
    forall a. Cell a -> Maybe MimeAttachments
    cellAttachments Cell NbV4
    c) [Pair] -> [Pair] -> [Pair]
    forall a. [a] -> [a] -> [a]
    ++
       case Cell NbV4 -> CellType NbV4
    forall a. Cell a -> CellType a
    cellType Cell NbV4
    c of
         CellType NbV4
    Markdown -> [ Key
    "cell_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "markdown" :: Text)
                     , Key
    "source" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV4 -> Source
    forall a. Cell a -> Source
    cellSource Cell NbV4
    c ]
         Heading Int
    lev ->
                    [ Key
    "cell_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "markdown" :: Text)
                    , Key
    "source" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .=
                         ([Text] -> Source
    Source ([Text] -> Source) -> (Source -> [Text]) -> Source -> Source
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> [Text]
    breakLines (Text -> [Text]) -> (Source -> Text) -> Source -> [Text]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                          ((Int -> Text -> Text
    T.replicate Int
    lev Text
    "#" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    " ") Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>) (Text -> Text) -> (Source -> Text) -> Source -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                          [Text] -> Text
    forall a. Monoid a => [a] -> a
    mconcat ([Text] -> Text) -> (Source -> [Text]) -> Source -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Source -> [Text]
    unSource) (Cell NbV4 -> Source
    forall a. Cell a -> Source
    cellSource Cell NbV4
    c)
                     ]
         CellType NbV4
    Raw      -> [ Key
    "cell_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "raw" :: Text)
                     , Key
    "source" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV4 -> Source
    forall a. Cell a -> Source
    cellSource Cell NbV4
    c
                     ]
         Code{
             codeExecutionCount :: forall a. CellType a -> Maybe Int
    codeExecutionCount = Maybe Int
    ec
           , codeOutputs :: forall a. CellType a -> [Output a]
    codeOutputs = [Output NbV4]
    outs
           }      -> [ Key
    "cell_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "code" :: Text)
                     , Key
    "execution_count" Key -> Maybe Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Maybe Int
    ec
                     , Key
    "outputs" Key -> [Output NbV4] -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= [Output NbV4]
    outs
                     , Key
    "source" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV4 -> Source
    forall a. Cell a -> Source
    cellSource Cell NbV4
    c
                     ]

    instance ToJSON (Cell NbV3) where
     toJSON :: Cell NbV3 -> Value
    toJSON Cell NbV3
    c =
      [Pair] -> Value
    object ([Pair] -> Value) -> [Pair] -> Value
    forall a b. (a -> b) -> a -> b
    $
       JSONMeta -> [Pair]
    metadataToV3Pairs (Cell NbV3 -> JSONMeta
    forall a. Cell a -> JSONMeta
    cellMetadata Cell NbV3
    c) [Pair] -> [Pair] -> [Pair]
    forall a. [a] -> [a] -> [a]
    ++
       case Cell NbV3 -> CellType NbV3
    forall a. Cell a -> CellType a
    cellType Cell NbV3
    c of
         CellType NbV3
    Markdown    -> [ Key
    "cell_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "markdown" :: Text)
                        , Key
    "source" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV3 -> Source
    forall a. Cell a -> Source
    cellSource Cell NbV3
    c
                        ]
         Heading Int
    lev -> [ Key
    "cell_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "heading" :: Text)
                        , Key
    "level" Key -> Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Int
    lev
                        , Key
    "source" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV3 -> Source
    forall a. Cell a -> Source
    cellSource Cell NbV3
    c
                        ]
         CellType NbV3
    Raw         -> [ Key
    "cell_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "raw" :: Text)
                        , Key
    "source" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV3 -> Source
    forall a. Cell a -> Source
    cellSource Cell NbV3
    c
                        ]
         Code{
             codeExecutionCount :: forall a. CellType a -> Maybe Int
    codeExecutionCount = Maybe Int
    ec
           , codeOutputs :: forall a. CellType a -> [Output a]
    codeOutputs = [Output NbV3]
    outs
           }      -> [ Key
    "cell_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "code" :: Text)
                     , Key
    "input" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Cell NbV3 -> Source
    forall a. Cell a -> Source
    cellSource Cell NbV3
    c
                     , Key
    "outputs" Key -> [Output NbV3] -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= [Output NbV3]
    outs
                     ] [Pair] -> [Pair] -> [Pair]
    forall a. [a] -> [a] -> [a]
    ++
                     [Pair] -> (Int -> [Pair]) -> Maybe Int -> [Pair]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\Int
    n -> [Key
    "prompt_number" Key -> Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Int
    n]) Maybe Int
    ec

    -- in v3, certain metadata fields occur in the main cell object.
    -- e.g. collapsed, language.
    metadataToV3Pairs :: JSONMeta -> [Aeson.Pair]
    metadataToV3Pairs :: JSONMeta -> [Pair]
    metadataToV3Pairs (JSONMeta Map Text Value
    meta) =
      (Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Map Text Value -> JSONMeta
    JSONMeta ([(Text, Value)] -> Map Text Value
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList [(Text, Value)]
    regMeta)) Pair -> [Pair] -> [Pair]
    forall a. a -> [a] -> [a]
    : ((Text, Value) -> Pair) -> [(Text, Value)] -> [Pair]
    forall a b. (a -> b) -> [a] -> [b]
    map (Text, Value) -> Pair
    forall {e} {kv} {v}. (KeyValue e kv, ToJSON v) => (Text, v) -> kv
    toPair [(Text, Value)]
    extraMeta
      where ([(Text, Value)]
    extraMeta, [(Text, Value)]
    regMeta) = ((Text, Value) -> Bool)
    -> [(Text, Value)] -> ([(Text, Value)], [(Text, Value)])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    partition (Text, Value) -> Bool
    forall {b}. (Text, b) -> Bool
    isExtraMeta ([(Text, Value)] -> ([(Text, Value)], [(Text, Value)]))
    -> [(Text, Value)] -> ([(Text, Value)], [(Text, Value)])
    forall a b. (a -> b) -> a -> b
    $ Map Text Value -> [(Text, Value)]
    forall k a. Map k a -> [(k, a)]
    M.toList Map Text Value
    meta
            toPair :: (Text, v) -> kv
    toPair (Text
    k,v
    v) = (String -> Key
    forall a. IsString a => String -> a
    fromString (Text -> String
    T.unpack Text
    k)) Key -> v -> kv
    forall v. ToJSON v => Key -> v -> kv
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= v
    v
            isExtraMeta :: (Text, b) -> Bool
    isExtraMeta (Text
    k,b
    _) = Text
    k Text -> Set Text -> Bool
    forall a. Ord a => a -> Set a -> Bool
    `Set.member` Set Text
    v3MetaInMainCell

    v3MetaInMainCell :: Set.Set Text
    v3MetaInMainCell :: Set Text
    v3MetaInMainCell = [Text] -> Set Text
    forall a. Ord a => [a] -> Set a
    Set.fromList [Text
    "collapsed", Text
    "language"]

    parseV3Metadata :: Aeson.Object -> Aeson.Parser JSONMeta
    parseV3Metadata :: Object -> Parser JSONMeta
    parseV3Metadata Object
    v = do
      meta <- Object
    v Object -> Key -> Parser (Maybe JSONMeta)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "metadata" Parser (Maybe JSONMeta) -> JSONMeta -> Parser JSONMeta
    forall a. Parser (Maybe a) -> a -> Parser a
    .!= JSONMeta
    forall a. Monoid a => a
    mempty
      vm <- parseJSON (Object v)
      let extraMeta = Map Text Value -> JSONMeta
    JSONMeta (Map Text Value -> Set Text -> Map Text Value
    forall k a. Ord k => Map k a -> Set k -> Map k a
    M.restrictKeys Map Text Value
    vm Set Text
    v3MetaInMainCell)
      return (meta <> extraMeta)

    -- | Information about the type of a notebook cell, plus
    -- data specific to that type.  note that 'Heading' is
    -- for v3 only; a 'Heading' will be rendered as 'Markdown'
    -- in v4.
    data CellType a =
        Markdown
      | Heading -- V3 only
        { forall a. CellType a -> Int
    headingLevel  :: Int
        }
      | Raw
      | Code
        { forall a. CellType a -> Maybe Int
    codeExecutionCount :: Maybe Int
        , forall a. CellType a -> [Output a]
    codeOutputs        :: [Output a]
        }
      deriving (Int -> CellType a -> ShowS
    [CellType a] -> ShowS
    CellType a -> String
    (Int -> CellType a -> ShowS)
    -> (CellType a -> String)
    -> ([CellType a] -> ShowS)
    -> Show (CellType a)
    forall a. Int -> CellType a -> ShowS
    forall a. [CellType a] -> ShowS
    forall a. CellType a -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall a. Int -> CellType a -> ShowS
    showsPrec :: Int -> CellType a -> ShowS
    $cshow :: forall a. CellType a -> String
    show :: CellType a -> String
    $cshowList :: forall a. [CellType a] -> ShowS
    showList :: [CellType a] -> ShowS
    Show, CellType a -> CellType a -> Bool
    (CellType a -> CellType a -> Bool)
    -> (CellType a -> CellType a -> Bool) -> Eq (CellType a)
    forall a. CellType a -> CellType a -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall a. CellType a -> CellType a -> Bool
    == :: CellType a -> CellType a -> Bool
    $c/= :: forall a. CellType a -> CellType a -> Bool
    /= :: CellType a -> CellType a -> Bool
    Eq, (forall x. CellType a -> Rep (CellType a) x)
    -> (forall x. Rep (CellType a) x -> CellType a)
    -> Generic (CellType a)
    forall x. Rep (CellType a) x -> CellType a
    forall x. CellType a -> Rep (CellType a) x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    forall a x. Rep (CellType a) x -> CellType a
    forall a x. CellType a -> Rep (CellType a) x
    $cfrom :: forall a x. CellType a -> Rep (CellType a) x
    from :: forall x. CellType a -> Rep (CellType a) x
    $cto :: forall a x. Rep (CellType a) x -> CellType a
    to :: forall x. Rep (CellType a) x -> CellType a
    Generic)

    -- | Output from a Code cell.
    data Output a =
        Stream
        { forall a. Output a -> Text
    streamName :: Text
        , forall a. Output a -> Source
    streamText :: Source }
      | DisplayData
        { forall a. Output a -> MimeBundle
    displayData     :: MimeBundle
        , forall a. Output a -> JSONMeta
    displayMetadata :: JSONMeta
        }
      | ExecuteResult
        { forall a. Output a -> Int
    executeCount    :: Int
        , forall a. Output a -> MimeBundle
    executeData     :: MimeBundle
        , forall a. Output a -> JSONMeta
    executeMetadata :: JSONMeta
        }
      | Err
        { forall a. Output a -> Text
    errName      :: Text
        , forall a. Output a -> Text
    errValue     :: Text
        , forall a. Output a -> [Text]
    errTraceback :: [Text]
        }
      deriving (Int -> Output a -> ShowS
    [Output a] -> ShowS
    Output a -> String
    (Int -> Output a -> ShowS)
    -> (Output a -> String) -> ([Output a] -> ShowS) -> Show (Output a)
    forall a. Int -> Output a -> ShowS
    forall a. [Output a] -> ShowS
    forall a. Output a -> String
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: forall a. Int -> Output a -> ShowS
    showsPrec :: Int -> Output a -> ShowS
    $cshow :: forall a. Output a -> String
    show :: Output a -> String
    $cshowList :: forall a. [Output a] -> ShowS
    showList :: [Output a] -> ShowS
    Show, Output a -> Output a -> Bool
    (Output a -> Output a -> Bool)
    -> (Output a -> Output a -> Bool) -> Eq (Output a)
    forall a. Output a -> Output a -> Bool
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: forall a. Output a -> Output a -> Bool
    == :: Output a -> Output a -> Bool
    $c/= :: forall a. Output a -> Output a -> Bool
    /= :: Output a -> Output a -> Bool
    Eq, (forall x. Output a -> Rep (Output a) x)
    -> (forall x. Rep (Output a) x -> Output a) -> Generic (Output a)
    forall x. Rep (Output a) x -> Output a
    forall x. Output a -> Rep (Output a) x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    forall a x. Rep (Output a) x -> Output a
    forall a x. Output a -> Rep (Output a) x
    $cfrom :: forall a x. Output a -> Rep (Output a) x
    from :: forall x. Output a -> Rep (Output a) x
    $cto :: forall a x. Rep (Output a) x -> Output a
    to :: forall x. Rep (Output a) x -> Output a
    Generic)

    instance FromJSON (Output NbV4) where
      parseJSON :: Value -> Parser (Output NbV4)
    parseJSON = String
    -> (Object -> Parser (Output NbV4))
    -> Value
    -> Parser (Output NbV4)
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
    withObject String
    "Object" ((Object -> Parser (Output NbV4)) -> Value -> Parser (Output NbV4))
    -> (Object -> Parser (Output NbV4))
    -> Value
    -> Parser (Output NbV4)
    forall a b. (a -> b) -> a -> b
    $ \Object
    v -> do
        ty <- Object
    v Object -> Key -> Parser String
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "output_type"
        case ty of
          String
    "stream" ->
            Text -> Source -> Output NbV4
    forall a. Text -> Source -> Output a
    Stream
              (Text -> Source -> Output NbV4)
    -> Parser Text -> Parser (Source -> Output NbV4)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser Text
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "name"
              Parser (Source -> Output NbV4)
    -> Parser Source -> Parser (Output NbV4)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser Source
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "text"
          String
    "display_data" ->
            MimeBundle -> JSONMeta -> Output NbV4
    forall a. MimeBundle -> JSONMeta -> Output a
    DisplayData
              (MimeBundle -> JSONMeta -> Output NbV4)
    -> Parser MimeBundle -> Parser (JSONMeta -> Output NbV4)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser MimeBundle
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "data"
              Parser (JSONMeta -> Output NbV4)
    -> Parser JSONMeta -> Parser (Output NbV4)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser (Maybe JSONMeta)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "metadata" Parser (Maybe JSONMeta) -> JSONMeta -> Parser JSONMeta
    forall a. Parser (Maybe a) -> a -> Parser a
    .!= JSONMeta
    forall a. Monoid a => a
    mempty
          String
    "execute_result" ->
            Int -> MimeBundle -> JSONMeta -> Output NbV4
    forall a. Int -> MimeBundle -> JSONMeta -> Output a
    ExecuteResult
              (Int -> MimeBundle -> JSONMeta -> Output NbV4)
    -> Parser Int -> Parser (MimeBundle -> JSONMeta -> Output NbV4)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser Int
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "execution_count"
              Parser (MimeBundle -> JSONMeta -> Output NbV4)
    -> Parser MimeBundle -> Parser (JSONMeta -> Output NbV4)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser MimeBundle
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "data"
              Parser (JSONMeta -> Output NbV4)
    -> Parser JSONMeta -> Parser (Output NbV4)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser (Maybe JSONMeta)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "metadata" Parser (Maybe JSONMeta) -> JSONMeta -> Parser JSONMeta
    forall a. Parser (Maybe a) -> a -> Parser a
    .!= JSONMeta
    forall a. Monoid a => a
    mempty
          String
    "error" ->
            Text -> Text -> [Text] -> Output NbV4
    forall a. Text -> Text -> [Text] -> Output a
    Err
              (Text -> Text -> [Text] -> Output NbV4)
    -> Parser Text -> Parser (Text -> [Text] -> Output NbV4)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser Text
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "ename"
              Parser (Text -> [Text] -> Output NbV4)
    -> Parser Text -> Parser ([Text] -> Output NbV4)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser Text
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "evalue"
              Parser ([Text] -> Output NbV4)
    -> Parser [Text] -> Parser (Output NbV4)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser [Text]
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "traceback"
          String
    _ -> String -> Parser (Output NbV4)
    forall a. String -> Parser a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> Parser (Output NbV4)) -> String -> Parser (Output NbV4)
    forall a b. (a -> b) -> a -> b
    $ String
    "Unknown object_type " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ String
    ty

    instance FromJSON (Output NbV3) where
      parseJSON :: Value -> Parser (Output NbV3)
    parseJSON = String
    -> (Object -> Parser (Output NbV3))
    -> Value
    -> Parser (Output NbV3)
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
    withObject String
    "Object" ((Object -> Parser (Output NbV3)) -> Value -> Parser (Output NbV3))
    -> (Object -> Parser (Output NbV3))
    -> Value
    -> Parser (Output NbV3)
    forall a b. (a -> b) -> a -> b
    $ \Object
    v -> do
        ty <- Object
    v Object -> Key -> Parser String
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "output_type"
        case ty of
          String
    "stream" ->
            Text -> Source -> Output NbV3
    forall a. Text -> Source -> Output a
    Stream
              (Text -> Source -> Output NbV3)
    -> Parser Text -> Parser (Source -> Output NbV3)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser Text
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "stream"
              Parser (Source -> Output NbV3)
    -> Parser Source -> Parser (Output NbV3)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser Source
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "text"
          String
    "display_data" ->
            MimeBundle -> JSONMeta -> Output NbV3
    forall a. MimeBundle -> JSONMeta -> Output a
    DisplayData
              (MimeBundle -> JSONMeta -> Output NbV3)
    -> Parser MimeBundle -> Parser (JSONMeta -> Output NbV3)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object -> Parser MimeBundle
    extractNbV3Data Object
    v
              Parser (JSONMeta -> Output NbV3)
    -> Parser JSONMeta -> Parser (Output NbV3)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser (Maybe JSONMeta)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "metadata" Parser (Maybe JSONMeta) -> JSONMeta -> Parser JSONMeta
    forall a. Parser (Maybe a) -> a -> Parser a
    .!= JSONMeta
    forall a. Monoid a => a
    mempty
          String
    "pyout" ->
            Int -> MimeBundle -> JSONMeta -> Output NbV3
    forall a. Int -> MimeBundle -> JSONMeta -> Output a
    ExecuteResult
              (Int -> MimeBundle -> JSONMeta -> Output NbV3)
    -> Parser Int -> Parser (MimeBundle -> JSONMeta -> Output NbV3)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser Int
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "prompt_number"
              Parser (MimeBundle -> JSONMeta -> Output NbV3)
    -> Parser MimeBundle -> Parser (JSONMeta -> Output NbV3)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object -> Parser MimeBundle
    extractNbV3Data Object
    v
              Parser (JSONMeta -> Output NbV3)
    -> Parser JSONMeta -> Parser (Output NbV3)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser (Maybe JSONMeta)
    forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
    .:? Key
    "metadata" Parser (Maybe JSONMeta) -> JSONMeta -> Parser JSONMeta
    forall a. Parser (Maybe a) -> a -> Parser a
    .!= JSONMeta
    forall a. Monoid a => a
    mempty
          String
    "pyerr" ->
            Text -> Text -> [Text] -> Output NbV3
    forall a. Text -> Text -> [Text] -> Output a
    Err
              (Text -> Text -> [Text] -> Output NbV3)
    -> Parser Text -> Parser (Text -> [Text] -> Output NbV3)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Object
    v Object -> Key -> Parser Text
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "ename"
              Parser (Text -> [Text] -> Output NbV3)
    -> Parser Text -> Parser ([Text] -> Output NbV3)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser Text
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "evalue"
              Parser ([Text] -> Output NbV3)
    -> Parser [Text] -> Parser (Output NbV3)
    forall a b. Parser (a -> b) -> Parser a -> Parser b
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
    <*> Object
    v Object -> Key -> Parser [Text]
    forall a. FromJSON a => Object -> Key -> Parser a
    .: Key
    "traceback"
          String
    _ -> String -> Parser (Output NbV3)
    forall a. String -> Parser a
    forall (m :: * -> *) a. MonadFail m => String -> m a
    fail (String -> Parser (Output NbV3)) -> String -> Parser (Output NbV3)
    forall a b. (a -> b) -> a -> b
    $ String
    "Unknown object_type " String -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ String
    ty

    -- Remove keys output_type, prompt_number, metadata;
    -- change short keys like text and png to mime types.
    extractNbV3Data :: Aeson.Object -> Aeson.Parser MimeBundle
    extractNbV3Data :: Object -> Parser MimeBundle
    extractNbV3Data Object
    v = do
      let go :: (a, b) -> Maybe (a, b)
    go (a
    "output_type", b
    _)   = Maybe (a, b)
    forall a. Maybe a
    Nothing
          go (a
    "metadata", b
    _)      = Maybe (a, b)
    forall a. Maybe a
    Nothing
          go (a
    "prompt_number", b
    _) = Maybe (a, b)
    forall a. Maybe a
    Nothing
          go (a
    "text", b
    x)          = (a, b) -> Maybe (a, b)
    forall a. a -> Maybe a
    Just (a
    "text/plain", b
    x)
          go (a
    "latex", b
    x)         = (a, b) -> Maybe (a, b)
    forall a. a -> Maybe a
    Just (a
    "text/latex", b
    x)
          go (a
    "html", b
    x)          = (a, b) -> Maybe (a, b)
    forall a. a -> Maybe a
    Just (a
    "text/html", b
    x)
          go (a
    "png", b
    x)           = (a, b) -> Maybe (a, b)
    forall a. a -> Maybe a
    Just (a
    "image/png", b
    x)
          go (a
    "jpeg", b
    x)          = (a, b) -> Maybe (a, b)
    forall a. a -> Maybe a
    Just (a
    "image/jpeg", b
    x)
          go (a
    "javascript", b
    x)    = (a, b) -> Maybe (a, b)
    forall a. a -> Maybe a
    Just (a
    "application/javascript", b
    x)
          go (a
    _, b
    _)               = Maybe (a, b)
    forall a. Maybe a
    Nothing -- TODO complete list? where documented?
      Value -> Parser MimeBundle
    forall a. FromJSON a => Value -> Parser a
    parseJSON (Object -> Value
    Object (Object -> Value) -> (Object -> Object) -> Object -> Value
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Pair] -> Object
    forall v. [(Key, v)] -> KeyMap v
    KM.fromList ([Pair] -> Object) -> (Object -> [Pair]) -> Object -> Object
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Pair -> Maybe Pair) -> [Pair] -> [Pair]
    forall a b. (a -> Maybe b) -> [a] -> [b]
    mapMaybe Pair -> Maybe Pair
    forall {a} {a} {b}.
    (Eq a, IsString a, IsString a) =>
    (a, b) -> Maybe (a, b)
    go ([Pair] -> [Pair]) -> (Object -> [Pair]) -> Object -> [Pair]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Object -> [Pair]
    forall v. KeyMap v -> [(Key, v)]
    KM.toList (Object -> Value) -> Object -> Value
    forall a b. (a -> b) -> a -> b
    $ Object
    v)

    instance ToJSON (Output NbV4) where
      toJSON :: Output NbV4 -> Value
    toJSON s :: Output NbV4
    s@Stream{} = [Pair] -> Value
    object
        [ Key
    "output_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "stream" :: Text)
        , Key
    "name" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> Text
    forall a. Output a -> Text
    streamName Output NbV4
    s
        , Key
    "text" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> Source
    forall a. Output a -> Source
    streamText Output NbV4
    s
        ]
      toJSON d :: Output NbV4
    d@DisplayData{} = [Pair] -> Value
    object
        [ Key
    "output_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "display_data" :: Text)
        , Key
    "data" Key -> MimeBundle -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> MimeBundle
    forall a. Output a -> MimeBundle
    displayData Output NbV4
    d
        , Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> JSONMeta
    forall a. Output a -> JSONMeta
    displayMetadata Output NbV4
    d
        ]
      toJSON e :: Output NbV4
    e@ExecuteResult{} = [Pair] -> Value
    object
        [ Key
    "output_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "execute_result" :: Text)
        , Key
    "execution_count" Key -> Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> Int
    forall a. Output a -> Int
    executeCount Output NbV4
    e
        , Key
    "data" Key -> MimeBundle -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> MimeBundle
    forall a. Output a -> MimeBundle
    executeData Output NbV4
    e
        , Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> JSONMeta
    forall a. Output a -> JSONMeta
    executeMetadata Output NbV4
    e
        ]
      toJSON e :: Output NbV4
    e@Err{} = [Pair] -> Value
    object
        [ Key
    "output_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "error" :: Text)
        , Key
    "ename" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> Text
    forall a. Output a -> Text
    errName Output NbV4
    e
        , Key
    "evalue" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> Text
    forall a. Output a -> Text
    errValue Output NbV4
    e
        , Key
    "traceback" Key -> [Text] -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV4 -> [Text]
    forall a. Output a -> [Text]
    errTraceback Output NbV4
    e
        ]

    instance ToJSON (Output NbV3) where
      toJSON :: Output NbV3 -> Value
    toJSON s :: Output NbV3
    s@Stream{} = [Pair] -> Value
    object
        [ Key
    "output_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "stream" :: Text)
        , Key
    "stream" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> Text
    forall a. Output a -> Text
    streamName Output NbV3
    s
        , Key
    "text" Key -> Source -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> Source
    forall a. Output a -> Source
    streamText Output NbV3
    s
        ]
      toJSON d :: Output NbV3
    d@DisplayData{} =
        Value -> Value
    adjustV3DataFields (Value -> Value) -> Value -> Value
    forall a b. (a -> b) -> a -> b
    $ [Pair] -> Value
    object
        [ Key
    "output_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "display_data" :: Text)
        , Key
    "data" Key -> MimeBundle -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> MimeBundle
    forall a. Output a -> MimeBundle
    displayData Output NbV3
    d
        , Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> JSONMeta
    forall a. Output a -> JSONMeta
    displayMetadata Output NbV3
    d ]
      toJSON e :: Output NbV3
    e@ExecuteResult{} =
        Value -> Value
    adjustV3DataFields (Value -> Value) -> Value -> Value
    forall a b. (a -> b) -> a -> b
    $ [Pair] -> Value
    object
        [ Key
    "output_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "pyout" :: Text)
        , Key
    "prompt_number" Key -> Int -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> Int
    forall a. Output a -> Int
    executeCount Output NbV3
    e
        , Key
    "data" Key -> MimeBundle -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> MimeBundle
    forall a. Output a -> MimeBundle
    executeData Output NbV3
    e
        , Key
    "metadata" Key -> JSONMeta -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> JSONMeta
    forall a. Output a -> JSONMeta
    executeMetadata Output NbV3
    e ]
      toJSON e :: Output NbV3
    e@Err{} = [Pair] -> Value
    object
        [ Key
    "output_type" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= (Text
    "pyerr" :: Text)
        , Key
    "ename" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> Text
    forall a. Output a -> Text
    errName Output NbV3
    e
        , Key
    "evalue" Key -> Text -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> Text
    forall a. Output a -> Text
    errValue Output NbV3
    e
        , Key
    "traceback" Key -> [Text] -> Pair
    forall v. ToJSON v => Key -> v -> Pair
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    .= Output NbV3 -> [Text]
    forall a. Output a -> [Text]
    errTraceback Output NbV3
    e
        ]

    adjustV3DataFields :: Value -> Value
    adjustV3DataFields :: Value -> Value
    adjustV3DataFields (Object Object
    hm) =
      case Key -> Object -> Maybe Value
    forall v. Key -> KeyMap v -> Maybe v
    KM.lookup Key
    "data" Object
    hm of
        Just (Object Object
    dm) -> Object -> Value
    Object (Object -> Value) -> Object -> Value
    forall a b. (a -> b) -> a -> b
    $
          Key -> Object -> Object
    forall v. Key -> KeyMap v -> KeyMap v
    KM.delete Key
    "data" (Object -> Object) -> Object -> Object
    forall a b. (a -> b) -> a -> b
    $ (Pair -> Object -> Object) -> Object -> [Pair] -> Object
    forall a b. (a -> b -> b) -> b -> [a] -> b
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr
          (\(Key
    k, Value
    v) -> Key -> Value -> Object -> Object
    forall v. Key -> v -> KeyMap v -> KeyMap v
    KM.insert (Key -> Key
    forall {a}. (Eq a, IsString a) => a -> a
    modKey Key
    k) Value
    v) Object
    hm
          (Object -> [Pair]
    forall v. KeyMap v -> [(Key, v)]
    KM.toList Object
    dm)
        Maybe Value
    _ -> Object -> Value
    Object Object
    hm
      where  modKey :: a -> a
    modKey a
    "text/plain"             = a
    "text"
             modKey a
    "text/latex"             = a
    "latex"
             modKey a
    "text/html"              = a
    "html"
             modKey a
    "image/jpeg"             = a
    "jpeg"
             modKey a
    "image/png"              = a
    "png"
             modKey a
    "application/javascript" = a
    "javascript"
             modKey a
    x                        = a
    x
    adjustV3DataFields Value
    x = Value
    x

    -- | Data in an execution result or display data cell.
    data MimeData =
        BinaryData ByteString
      | TextualData Text
      | JsonData Value
      deriving (Int -> MimeData -> ShowS
    [MimeData] -> ShowS
    MimeData -> String
    (Int -> MimeData -> ShowS)
    -> (MimeData -> String) -> ([MimeData] -> ShowS) -> Show MimeData
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> MimeData -> ShowS
    showsPrec :: Int -> MimeData -> ShowS
    $cshow :: MimeData -> String
    show :: MimeData -> String
    $cshowList :: [MimeData] -> ShowS
    showList :: [MimeData] -> ShowS
    Show, MimeData -> MimeData -> Bool
    (MimeData -> MimeData -> Bool)
    -> (MimeData -> MimeData -> Bool) -> Eq MimeData
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: MimeData -> MimeData -> Bool
    == :: MimeData -> MimeData -> Bool
    $c/= :: MimeData -> MimeData -> Bool
    /= :: MimeData -> MimeData -> Bool
    Eq, Eq MimeData
    Eq MimeData =>
    (MimeData -> MimeData -> Ordering)
    -> (MimeData -> MimeData -> Bool)
    -> (MimeData -> MimeData -> Bool)
    -> (MimeData -> MimeData -> Bool)
    -> (MimeData -> MimeData -> Bool)
    -> (MimeData -> MimeData -> MimeData)
    -> (MimeData -> MimeData -> MimeData)
    -> Ord MimeData
    MimeData -> MimeData -> Bool
    MimeData -> MimeData -> Ordering
    MimeData -> MimeData -> MimeData
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: MimeData -> MimeData -> Ordering
    compare :: MimeData -> MimeData -> Ordering
    $c< :: MimeData -> MimeData -> Bool
    < :: MimeData -> MimeData -> Bool
    $c<= :: MimeData -> MimeData -> Bool
    <= :: MimeData -> MimeData -> Bool
    $c> :: MimeData -> MimeData -> Bool
    > :: MimeData -> MimeData -> Bool
    $c>= :: MimeData -> MimeData -> Bool
    >= :: MimeData -> MimeData -> Bool
    $cmax :: MimeData -> MimeData -> MimeData
    max :: MimeData -> MimeData -> MimeData
    $cmin :: MimeData -> MimeData -> MimeData
    min :: MimeData -> MimeData -> MimeData
    Ord, (forall x. MimeData -> Rep MimeData x)
    -> (forall x. Rep MimeData x -> MimeData) -> Generic MimeData
    forall x. Rep MimeData x -> MimeData
    forall x. MimeData -> Rep MimeData x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. MimeData -> Rep MimeData x
    from :: forall x. MimeData -> Rep MimeData x
    $cto :: forall x. Rep MimeData x -> MimeData
    to :: forall x. Rep MimeData x -> MimeData
    Generic)

    instance ToJSON MimeData where
      toJSON :: MimeData -> Value
    toJSON = Value -> Value
    forall a. ToJSON a => a -> Value
    toJSON (Value -> Value) -> (MimeData -> Value) -> MimeData -> Value
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . MimeData -> Value
    mimeDataToValue
      toEncoding :: MimeData -> Encoding
    toEncoding = Value -> Encoding
    forall a. ToJSON a => a -> Encoding
    toEncoding (Value -> Encoding) -> (MimeData -> Value) -> MimeData -> Encoding
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . MimeData -> Value
    mimeDataToValue

    mimeDataToValue :: MimeData -> Value
    mimeDataToValue :: MimeData -> Value
    mimeDataToValue (BinaryData ByteString
    bs) =
      Text -> Value
    forall a. ToJSON a => a -> Value
    toJSON (Text -> Value) -> (ByteString -> Text) -> ByteString -> Value
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
        ByteString -> Text
    TE.decodeUtf8 (ByteString -> Text)
    -> (ByteString -> ByteString) -> ByteString -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
        (ByteString -> ByteString -> ByteString
    forall a. Semigroup a => a -> a -> a
    <> ByteString
    "\n") (ByteString -> ByteString)
    -> (ByteString -> ByteString) -> ByteString -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
        ByteString -> [ByteString] -> ByteString
    B.intercalate ByteString
    "\n" ([ByteString] -> ByteString)
    -> (ByteString -> [ByteString]) -> ByteString -> ByteString
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .  Int -> ByteString -> [ByteString]
    chunksOf Int
    76 (ByteString -> [ByteString])
    -> (ByteString -> ByteString) -> ByteString -> [ByteString]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
        ByteString -> ByteString
    Base64.encode
        (ByteString -> Value) -> ByteString -> Value
    forall a b. (a -> b) -> a -> b
    $ ByteString
    bs
    mimeDataToValue (JsonData Value
    v) = Value
    v
    mimeDataToValue (TextualData Text
    t) = [Text] -> Value
    forall a. ToJSON a => a -> Value
    toJSON (Text -> [Text]
    breakLines Text
    t)

    type MimeType = Text

    -- | A 'MimeBundle' wraps a map from mime types to mime data.
    newtype MimeBundle = MimeBundle{ MimeBundle -> Map Text MimeData
    unMimeBundle :: M.Map MimeType MimeData }
      deriving (Int -> MimeBundle -> ShowS
    [MimeBundle] -> ShowS
    MimeBundle -> String
    (Int -> MimeBundle -> ShowS)
    -> (MimeBundle -> String)
    -> ([MimeBundle] -> ShowS)
    -> Show MimeBundle
    forall a.
    (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
    $cshowsPrec :: Int -> MimeBundle -> ShowS
    showsPrec :: Int -> MimeBundle -> ShowS
    $cshow :: MimeBundle -> String
    show :: MimeBundle -> String
    $cshowList :: [MimeBundle] -> ShowS
    showList :: [MimeBundle] -> ShowS
    Show, MimeBundle -> MimeBundle -> Bool
    (MimeBundle -> MimeBundle -> Bool)
    -> (MimeBundle -> MimeBundle -> Bool) -> Eq MimeBundle
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: MimeBundle -> MimeBundle -> Bool
    == :: MimeBundle -> MimeBundle -> Bool
    $c/= :: MimeBundle -> MimeBundle -> Bool
    /= :: MimeBundle -> MimeBundle -> Bool
    Eq, Eq MimeBundle
    Eq MimeBundle =>
    (MimeBundle -> MimeBundle -> Ordering)
    -> (MimeBundle -> MimeBundle -> Bool)
    -> (MimeBundle -> MimeBundle -> Bool)
    -> (MimeBundle -> MimeBundle -> Bool)
    -> (MimeBundle -> MimeBundle -> Bool)
    -> (MimeBundle -> MimeBundle -> MimeBundle)
    -> (MimeBundle -> MimeBundle -> MimeBundle)
    -> Ord MimeBundle
    MimeBundle -> MimeBundle -> Bool
    MimeBundle -> MimeBundle -> Ordering
    MimeBundle -> MimeBundle -> MimeBundle
    forall a.
    Eq a =>
    (a -> a -> Ordering)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> Bool)
    -> (a -> a -> a)
    -> (a -> a -> a)
    -> Ord a
    $ccompare :: MimeBundle -> MimeBundle -> Ordering
    compare :: MimeBundle -> MimeBundle -> Ordering
    $c< :: MimeBundle -> MimeBundle -> Bool
    < :: MimeBundle -> MimeBundle -> Bool
    $c<= :: MimeBundle -> MimeBundle -> Bool
    <= :: MimeBundle -> MimeBundle -> Bool
    $c> :: MimeBundle -> MimeBundle -> Bool
    > :: MimeBundle -> MimeBundle -> Bool
    $c>= :: MimeBundle -> MimeBundle -> Bool
    >= :: MimeBundle -> MimeBundle -> Bool
    $cmax :: MimeBundle -> MimeBundle -> MimeBundle
    max :: MimeBundle -> MimeBundle -> MimeBundle
    $cmin :: MimeBundle -> MimeBundle -> MimeBundle
    min :: MimeBundle -> MimeBundle -> MimeBundle
    Ord, (forall x. MimeBundle -> Rep MimeBundle x)
    -> (forall x. Rep MimeBundle x -> MimeBundle) -> Generic MimeBundle
    forall x. Rep MimeBundle x -> MimeBundle
    forall x. MimeBundle -> Rep MimeBundle x
    forall a.
    (forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
    $cfrom :: forall x. MimeBundle -> Rep MimeBundle x
    from :: forall x. MimeBundle -> Rep MimeBundle x
    $cto :: forall x. Rep MimeBundle x -> MimeBundle
    to :: forall x. Rep MimeBundle x -> MimeBundle
    Generic, NonEmpty MimeBundle -> MimeBundle
    MimeBundle -> MimeBundle -> MimeBundle
    (MimeBundle -> MimeBundle -> MimeBundle)
    -> (NonEmpty MimeBundle -> MimeBundle)
    -> (forall b. Integral b => b -> MimeBundle -> MimeBundle)
    -> Semigroup MimeBundle
    forall b. Integral b => b -> MimeBundle -> MimeBundle
    forall a.
    (a -> a -> a)
    -> (NonEmpty a -> a)
    -> (forall b. Integral b => b -> a -> a)
    -> Semigroup a
    $c<> :: MimeBundle -> MimeBundle -> MimeBundle
    <> :: MimeBundle -> MimeBundle -> MimeBundle
    $csconcat :: NonEmpty MimeBundle -> MimeBundle
    sconcat :: NonEmpty MimeBundle -> MimeBundle
    $cstimes :: forall b. Integral b => b -> MimeBundle -> MimeBundle
    stimes :: forall b. Integral b => b -> MimeBundle -> MimeBundle
    Semigroup, Semigroup MimeBundle
    MimeBundle
    Semigroup MimeBundle =>
    MimeBundle
    -> (MimeBundle -> MimeBundle -> MimeBundle)
    -> ([MimeBundle] -> MimeBundle)
    -> Monoid MimeBundle
    [MimeBundle] -> MimeBundle
    MimeBundle -> MimeBundle -> MimeBundle
    forall a.
    Semigroup a =>
    a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
    $cmempty :: MimeBundle
    mempty :: MimeBundle
    $cmappend :: MimeBundle -> MimeBundle -> MimeBundle
    mappend :: MimeBundle -> MimeBundle -> MimeBundle
    $cmconcat :: [MimeBundle] -> MimeBundle
    mconcat :: [MimeBundle] -> MimeBundle
    Monoid)

    instance FromJSON MimeBundle where
      parseJSON :: Value -> Parser MimeBundle
    parseJSON Value
    v = do
        m <- Value -> Parser (Map Text Value)
    forall a. FromJSON a => Value -> Parser a
    parseJSON Value
    v Parser (Map Text Value)
    -> (Map Text Value -> Parser [(Text, MimeData)])
    -> Parser [(Text, MimeData)]
    forall a b. Parser a -> (a -> Parser b) -> Parser b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= ((Text, Value) -> Parser (Text, MimeData))
    -> [(Text, Value)] -> Parser [(Text, MimeData)]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
    mapM (Text, Value) -> Parser (Text, MimeData)
    pairToMimeData ([(Text, Value)] -> Parser [(Text, MimeData)])
    -> (Map Text Value -> [(Text, Value)])
    -> Map Text Value
    -> Parser [(Text, MimeData)]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Map Text Value -> [(Text, Value)]
    forall k a. Map k a -> [(k, a)]
    M.toList
        return $ MimeBundle $ M.fromList m

    pairToMimeData :: (MimeType, Value) -> Aeson.Parser (MimeType, MimeData)
    pairToMimeData :: (Text, Value) -> Parser (Text, MimeData)
    pairToMimeData (Text
    mt, Value
    v)
      | Text
    mt Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    "application/json" Bool -> Bool -> Bool
    ||
        Text
    "+json" Text -> Text -> Bool
    `T.isSuffixOf` Text
    mt = (Text, MimeData) -> Parser (Text, MimeData)
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text
    mt, Value -> MimeData
    JsonData Value
    v)
    pairToMimeData (Text
    mt, Value
    v) = do
      t <- Value -> Parser Text
    forall a. FromJSON a => Value -> Parser a
    parseJSON Value
    v Parser Text -> Parser Text -> Parser Text
    forall a. Parser a -> Parser a -> Parser a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> ([Text] -> Text
    forall a. Monoid a => [a] -> a
    mconcat ([Text] -> Text) -> Parser [Text] -> Parser Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Value -> Parser [Text]
    forall a. FromJSON a => Value -> Parser a
    parseJSON Value
    v)
      let mimeprefix = (Char -> Bool) -> Text -> Text
    T.takeWhile (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /=Char
    '/') Text
    mt
      if mimeprefix == "text"
         then return (mt, TextualData t)
         else
           case Base64.decode (TE.encodeUtf8 (T.filter (not . isSpace) t)) of
                Left String
    _  -> (Text, MimeData) -> Parser (Text, MimeData)
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text
    mt, Text -> MimeData
    TextualData Text
    t)
                Right ByteString
    b -> (Text, MimeData) -> Parser (Text, MimeData)
    forall a. a -> Parser a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Text
    mt, ByteString -> MimeData
    BinaryData ByteString
    b)

    instance ToJSON MimeBundle where
      toJSON :: MimeBundle -> Value
    toJSON (MimeBundle Map Text MimeData
    m) =
        Map Text MimeData -> Value
    forall a. ToJSON a => a -> Value
    toJSON Map Text MimeData
    m
      toEncoding :: MimeBundle -> Encoding
    toEncoding (MimeBundle Map Text MimeData
    m) =  -- ensure deterministic (sorted) order
        Series -> Encoding
    pairs (Series -> Encoding) -> Series -> Encoding
    forall a b. (a -> b) -> a -> b
    $ [Series] -> Series
    forall a. Monoid a => [a] -> a
    mconcat ([Series] -> Series) -> [Series] -> Series
    forall a b. (a -> b) -> a -> b
    $ ((Text, MimeData) -> Series) -> [(Text, MimeData)] -> [Series]
    forall a b. (a -> b) -> [a] -> [b]
    map (\(Text
    k,MimeData
    v) -> (String -> Key
    forall a. IsString a => String -> a
    fromString (Text -> String
    T.unpack Text
    k) Key -> MimeData -> Series
    forall v. ToJSON v => Key -> v -> Series
    forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
    Aeson..= MimeData
    v))
                              (((Text, MimeData) -> Text)
    -> [(Text, MimeData)] -> [(Text, MimeData)]
    forall b a. Ord b => (a -> b) -> [a] -> [a]
    sortOn (Text, MimeData) -> Text
    forall a b. (a, b) -> a
    fst (Map Text MimeData -> [(Text, MimeData)]
    forall k a. Map k a -> [(k, a)]
    M.toList Map Text MimeData
    m))

    chunksOf :: Int -> ByteString -> [ByteString]
    chunksOf :: Int -> ByteString -> [ByteString]
    chunksOf Int
    k ByteString
    s
       | ByteString -> Bool
    B.null ByteString
    s = []
       | Bool
    otherwise =
         let (ByteString
    h,ByteString
    t) = Int -> ByteString -> (ByteString, ByteString)
    B.splitAt Int
    k ByteString
    s
         in ByteString
    h ByteString -> [ByteString] -> [ByteString]
    forall a. a -> [a] -> [a]
    : Int -> ByteString -> [ByteString]
    chunksOf Int
    k ByteString
    t

    -- | Break up a string into a list of strings, each representing
    -- one line of the string (including trailing newline if any).
    breakLines :: Text -> [Text]
    breakLines :: Text -> [Text]
    breakLines Text
    t =
      let (Text
    x, Text
    y) = (Char -> Bool) -> Text -> (Text, Text)
    T.break (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    ==Char
    '\n') Text
    t
      in  case Text -> Maybe (Char, Text)
    T.uncons Text
    y of
             Maybe (Char, Text)
    Nothing        -> if Text -> Bool
    T.null Text
    x then [] else [Text
    x]
             Just (Char
    c, Text
    rest) -> (Text
    x Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Text
    T.singleton Char
    c) Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    : Text -> [Text]
    breakLines Text
    rest