如何使用aeson-schema包?

时间:2013-06-11 15:39:29

标签: haskell

aeson-schema 是一个用于根据JSON模式验证JSON数据的包。 有没有人举例说明如何使用它?

2 个答案:

答案 0 :(得分:1)

有一个基本的例子in the documentation

答案 1 :(得分:0)

我的问题中有一个完整的工作示例: In aeson-schemas how do you construct an Object of a SchemaType without encoding to text and decoding back?

  
#!/usr/bin/env stack
{- stack
    runghc
    --resolver lts-14.15
    --package aeson-schemas-1.0.3
    --package aeson
    --package text
-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeFamilies #-}

import Data.Aeson (object, (.=), Value, decode, encode)
import Data.Aeson.Schema
import Data.Aeson.Text (encodeToLazyText)
import Data.Maybe (fromJust)
import qualified Data.Text.IO as T
import Data.Text(Text)
import Data.Text.Lazy (toStrict)


main :: IO ()
main = do
  let example = makeExample $ object [ "example" .= ("Example" :: Text) ]
  useExample example


useExample :: Object Example -> IO ()
useExample example = T.putStrLn $ toStrict $ encodeToLazyText $ object [
    "example" .= [get| example.example|]
  ]

makeExample :: Value -> Object Example
makeExample = fromJust . decode . encode


type Example = [schema|
  {
    example: Text,
  }
|]

还支持更复杂的数据结构,包括对象中具有可空值的数据结构。

下面是https://httpbin.org/json的示例:

{
  "slideshow": {
    "author": "Yours Truly",
    "date": "date of publication",
    "slides": [
      {
        "title": "Wake up to WonderWidgets!",
        "type": "all"
      },
      {
        "items": [
          "Why <em>WonderWidgets</em> are great",
          "Who <em>buys</em> WonderWidgets"
        ],
        "title": "Overview",
        "type": "all"
      }
    ],
    "title": "Sample Slide Show"
  }
}

示例中的幻灯片中的 items 键具有可为空的值(可以忽略或将其定义为null)。

以下是使用aeson-schemas表示JSON的方法:

type Slideshow = [schema|
  {
    slideshow: {
      author: Text,
      date: Text,
      slides: List {
        title: Text,
        type: Text,
        items: Maybe List Text
      },
      title: Text
    }
  }
|]

注意items: Maybe List Text,用于处理可为空/可选的项目键。

这是一个完整的工作示例,用于从httpbin.org示例中提取JSON并仅与作者一起打印JSON。

创建具有以下内容的文件印刷作者

#!/usr/bin/env stack
{- stack
    runghc
    --resolver lts-14.15
    --package aeson-schemas-1.0.3
    --package aeson
    --package text
    --package req
-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TypeFamilies #-}

import Data.Aeson (object, (.=), Value, decode, encode)
import Data.Aeson.Schema
import Data.Aeson.Text (encodeToLazyText)
import Data.Maybe (fromJust)
import Data.Text(Text)
import qualified Data.Text.IO as T
import Data.Text.Lazy (toStrict)
import Network.HTTP.Req


main :: IO ()
main = do
  slideshow <- loadSlideshow
  useSlideshow slideshow


loadSlideshow :: IO (Object Slideshow)
loadSlideshow =
  runReq defaultHttpConfig $ do
    r <- req GET
      (https "httpbin.org" /: "json")
      NoReqBody
      jsonResponse
      mempty
    return (responseBody r)


useSlideshow :: Object Slideshow -> IO ()
useSlideshow slideshow =
  T.putStrLn $ toStrict $ encodeToLazyText $ object [
    "author" .= [get| slideshow.slideshow.author|]
  ]


type Slideshow = [schema|
  {
    slideshow: {
      author: Text,
      date: Text,
      slides: List {
        title: Text,
        type: Text,
        items: Maybe List Text
      },
      title: Text
    }
  }
|]

使其可执行:

chmod +x print-author

运行它:

./print-author

您应该从程序中获得以下输出:

{"author":"Yours Truly"}