一个人如何使用aeson-schema软件包?

问题描述 投票:-1回答:2

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

haskell aeson
2个回答
2
投票

有一个基本示例in the documentation


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"
  }
}

在示例中,slides中的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,用于处理可为空/可选的items键。

这是一个完整的工作示例,用于从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"}
© www.soinside.com 2019 - 2024. All rights reserved.