1

我有带有“属性”字段的 json-schema 文件。

{
...
   "properties": {
       "id": {
           "description": "ID",
           "type": "integer",
           "required": true
        },
        ...
    }
}

如您所见,属性不是数组,而是对象。我想用 aeson 解析这个结构Map String Schema

我现在的代码如下所示:

{-# LANGUAGE OverloadedStrings #-}

module GimmeModels.Schema.JSONSchema.Types
where

import Data.Aeson
import Control.Applicative ((<$>), (<*>), empty)
import Data.Attoparsec (parse, Result(..), IResult( Done ))
import Data.Text (Text)
import Control.Monad (mzero)
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.Aeson.Types as T
import Data.Map (Map(..))

data Schema = Schema {
      schema      :: Maybe Text
    , id          :: Maybe Text 
    , _type       :: Maybe Type
    , description :: Maybe Text
    , extends     :: Maybe (Map String String)
    , items       :: Maybe Schema 
    , properties  :: Maybe (Map String Schema)
    , required    :: Maybe Bool 
    } deriving (Show)

type SchemaUrl = Text
type Type = Text

type PropsMap = Map String Schema

instance FromJSON Schema where 
     parseJSON (Object v) = 
        Schema <$> v .:? "$schema"
               <*> v .:? "id"
               <*> v .:? "type"
               <*> v .:? "description"
               <*> v .:? "extends"
               <*> v .:? "items"
               <*> v .:? "properties"
               <*> v .:? "required"
     parseJSON _ = empty

parseSchemaFromString :: String -> Maybe Schema
parseSchemaFromString s = 
    case parse json bs of
        (Done rest r) -> T.parseMaybe parseJSON r :: Maybe Schema 
        _ -> error "FAIL" 
    where bs = BS.pack s

它不起作用。如果我打开泛型和自动派生 FromJSON 接口,Aeson 会像魅力一样解析属性,但我确实需要手动映射“$schema”和“type”字段。

我应该怎么做才能正确解析它?

伙计们,经过8个小时的睡眠,我终于意识到问题所在了!在我的模式中有 field "type": ["string", "null"],但解析器假定“类型”是字符串。谢谢大家的帮助:)

4

0 回答 0