0

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

4

2 回答 2

2

文档中有一个基本示例。

于 2013-06-11T16:13:13.660 回答
1

我在这里的问题中有一个完整的工作示例: 在 aeson-schemas 中,您如何构造 SchemaType 的对象而不编码为文本并解码回?

#!/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。

创建具有以下内容的文件print-author :

#!/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"}
于 2019-12-01T19:17:16.223 回答