| Copyright | (c) Dong Han 2020 |
|---|---|
| License | BSD |
| Maintainer | winterland1989@gmail.com |
| Stability | experimental |
| Portability | non-portable |
| Safe Haskell | None |
| Language | Haskell2010 |
Z.Data.YAML
Description
Simple YAML codec using libYAML and JSON's JSON utilities.
The design choice to make things as simple as possible since YAML is a complex format, there're some limitations using this approach:
- Does not support complex keys.
- Does not support multiple doucments in one file.
import GHC.Generics
import qualified Z.Data.Text as T
import qualified Z.Data.YAML as YAML
data Person = Person
{ name :: T.Text
, age :: Int
, magic :: Bool
}
deriving (Show, Generic)
deriving anyclass (YAML.JSON)
> YAML.decode @[Person] "- name: Erik Weisz\n age: 52\n magic: True\n"
> Right [Person {name = "Erik Weisz", age = 52, magic = True}]
Synopsis
- decode :: forall a. (HasCallStack, JSON a) => Bytes -> Either DecodeError a
- encode :: (HasCallStack, JSON a) => YAMLFormatOpts -> a -> Text
- readYAMLFile :: forall a. (HasCallStack, JSON a) => CBytes -> IO a
- writeYAMLFile :: (HasCallStack, JSON a) => YAMLFormatOpts -> CBytes -> a -> IO ()
- initParser :: Bytes -> Resource (IO (Maybe MarkedEvent))
- initFileParser :: HasCallStack => CBytes -> Resource (IO (Maybe MarkedEvent))
- parseSingleDoucment :: HasCallStack => IO (Maybe MarkedEvent) -> IO Value
- parseAllDocuments :: HasCallStack => IO (Maybe MarkedEvent) -> IO [Value]
- initEmitter :: YAMLFormatOpts -> Resource (Ptr EmitterStruct, Event -> IO ())
- initFileEmitter :: HasCallStack => YAMLFormatOpts -> CBytes -> Resource (Event -> IO ())
- buildSingleDocument :: HasCallStack => (Event -> IO ()) -> Value -> IO ()
- buildValue :: HasCallStack => (Event -> IO ()) -> Value -> IO ()
- data YAMLError
- data YAMLParseError
- data ConvertError
- type DecodeError = Either YAMLError ConvertError
- class JSON a where
- data Value
Decode and encode using YAML
decode :: forall a. (HasCallStack, JSON a) => Bytes -> Either DecodeError a Source #
Decode a JSON instance from YAML bytes.
encode :: (HasCallStack, JSON a) => YAMLFormatOpts -> a -> Text Source #
Encode a JSON instance as UTF8 YAML text.
readYAMLFile :: forall a. (HasCallStack, JSON a) => CBytes -> IO a Source #
Decode a JSON instance from a YAML file.
writeYAMLFile :: (HasCallStack, JSON a) => YAMLFormatOpts -> CBytes -> a -> IO () Source #
Encode a JSON instance to YAML file.
Streaming parser and builder
initParser :: Bytes -> Resource (IO (Maybe MarkedEvent)) Source #
Create a source that yields marked events from a piece of YAML bytes.
initFileParser :: HasCallStack => CBytes -> Resource (IO (Maybe MarkedEvent)) Source #
Create a source that yields marked events from a piece of YAML bytes.
parseSingleDoucment :: HasCallStack => IO (Maybe MarkedEvent) -> IO Value Source #
Parse a single YAML document, throw OtherYAMLError if multiple documents are met.
parseAllDocuments :: HasCallStack => IO (Maybe MarkedEvent) -> IO [Value] Source #
Parse all YAML documents.
initEmitter :: YAMLFormatOpts -> Resource (Ptr EmitterStruct, Event -> IO ()) Source #
Make a new YAML event sink, whose result can be fetched via getEmitterResult.
initFileEmitter :: HasCallStack => YAMLFormatOpts -> CBytes -> Resource (Event -> IO ()) Source #
buildSingleDocument :: HasCallStack => (Event -> IO ()) -> Value -> IO () Source #
Write a value as a YAML document stream.
buildValue :: HasCallStack => (Event -> IO ()) -> Value -> IO () Source #
Write a value as a stream of Events(without document start/end, stream start/end).
Errors
Constructors
| ParseEventException CBytes CBytes Mark | problem, context, mark |
| ParseAliasEventWithEmptyAnchor Mark Mark | |
| ParseYAMLError YAMLParseError | custom parse error |
| EmitEventException Event CInt | |
| EmitAliasEventWithEmptyAnchor | |
| OtherYAMLError Text |
Instances
data YAMLParseError Source #
Constructors
| UnknownAlias MarkedEvent | |
| UnexpectedEvent MarkedEvent | |
| NonStringKey MarkedEvent | |
| NonStringKeyAlias MarkedEvent | |
| UnexpectedEventEnd |
Instances
data ConvertError #
Error info with (JSON) Path info.
Instances
type DecodeError = Either YAMLError ConvertError Source #
Re-Exports
Type class for encode & decode JSON.
Minimal complete definition
Nothing
Instances
| JSON Bool | |
| JSON Char | |
| JSON Double | |
| JSON Float | |
| JSON Int | |
| JSON Int8 | |
| JSON Int16 | |
| JSON Int32 | |
| JSON Int64 | |
| JSON Integer | This instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype |
| JSON Natural | This instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype |
| JSON Ordering | |
| JSON Word | |
| JSON Word8 | |
| JSON Word16 | |
| JSON Word32 | |
| JSON Word64 | |
| JSON () | |
| JSON Scientific | Note this instance doesn't reject large input |
Defined in Z.Data.JSON.Base Methods fromValue :: Value -> Converter Scientific # toValue :: Scientific -> Value # encodeJSON :: Scientific -> Builder () # | |
| JSON ByteArray | |
| JSON CBytes | JSON instances check if > encodeText ("hello" :: CBytes)
""hello""
> encodeText ("hello\NUL" :: CBytes) -- |
| JSON Value | |
| JSON FlatIntSet | |
Defined in Z.Data.JSON.Base Methods fromValue :: Value -> Converter FlatIntSet # toValue :: FlatIntSet -> Value # encodeJSON :: FlatIntSet -> Builder () # | |
| JSON Text | |
| JSON Bytes | This is an INCOHERENT instance, encode binary data with base64 encoding. |
| JSON FileEvent | |
| JSON PathStyle | |
| JSON TimeVal | |
| JSON ResUsage | |
| JSON PID | |
| JSON OSName | |
| JSON PassWD | |
| JSON CPUInfo | |
| JSON TimeVal64 | |
| JSON DirEntType | |
Defined in Z.IO.UV.FFI Methods fromValue :: Value -> Converter DirEntType # toValue :: DirEntType -> Value # encodeJSON :: DirEntType -> Builder () # | |
| JSON UVTimeSpec | |
Defined in Z.IO.UV.FFI Methods fromValue :: Value -> Converter UVTimeSpec # toValue :: UVTimeSpec -> Value # encodeJSON :: UVTimeSpec -> Builder () # | |
| JSON FStat | |
| JSON AccessResult | |
Defined in Z.IO.UV.FFI Methods fromValue :: Value -> Converter AccessResult # toValue :: AccessResult -> Value # encodeJSON :: AccessResult -> Builder () # | |
| JSON UID | |
| JSON GID | |
| JSON ProcessOptions | |
Defined in Z.IO.UV.FFI Methods fromValue :: Value -> Converter ProcessOptions # toValue :: ProcessOptions -> Value # encodeJSON :: ProcessOptions -> Builder () # | |
| JSON ProcessStdStream | |
Defined in Z.IO.UV.FFI Methods fromValue :: Value -> Converter ProcessStdStream # toValue :: ProcessStdStream -> Value # encodeJSON :: ProcessStdStream -> Builder () # | |
| JSON SocketAddr | |
Defined in Z.IO.Network.SocketAddr Methods fromValue :: Value -> Converter SocketAddr # toValue :: SocketAddr -> Value # encodeJSON :: SocketAddr -> Builder () # | |
| JSON IPv4 | |
| JSON IPv6 | |
| JSON PortNumber | |
Defined in Z.IO.Network.SocketAddr Methods fromValue :: Value -> Converter PortNumber # toValue :: PortNumber -> Value # encodeJSON :: PortNumber -> Builder () # | |
| JSON IntSet | |
| JSON Tag Source # | |
| JSON Mark Source # | |
| JSON MarkedEvent Source # | |
Defined in Z.Data.YAML.FFI Methods fromValue :: Value -> Converter MarkedEvent # toValue :: MarkedEvent -> Value # encodeJSON :: MarkedEvent -> Builder () # | |
| JSON Event Source # | |
| JSON [Char] | This is an INCOHERENT instance, to provide JSON text encoding behaviour. |
| JSON a => JSON [a] | |
| JSON a => JSON (Maybe a) | |
| (JSON a, Integral a) => JSON (Ratio a) | This instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Ratio and provide your own instance using |
| JSON a => JSON (FlatIntMap a) | |
Defined in Z.Data.JSON.Base Methods fromValue :: Value -> Converter (FlatIntMap a) # toValue :: FlatIntMap a -> Value # encodeJSON :: FlatIntMap a -> Builder () # | |
| (Ord a, JSON a) => JSON (FlatSet a) | |
| JSON a => JSON (Vector a) | |
| (Prim a, JSON a) => JSON (PrimVector a) | |
Defined in Z.Data.JSON.Base Methods fromValue :: Value -> Converter (PrimVector a) # toValue :: PrimVector a -> Value # encodeJSON :: PrimVector a -> Builder () # | |
| JSON a => JSON (Array a) | |
| JSON a => JSON (SmallArray a) | |
Defined in Z.Data.JSON.Base Methods fromValue :: Value -> Converter (SmallArray a) # toValue :: SmallArray a -> Value # encodeJSON :: SmallArray a -> Builder () # | |
| (Prim a, JSON a) => JSON (PrimArray a) | |
| JSON a => JSON (NonEmpty a) | |
| JSON a => JSON (IntMap a) | |
| JSON a => JSON (Tree a) | |
| JSON a => JSON (Seq a) | |
| (Ord a, JSON a) => JSON (Set a) | |
| (Eq a, Hashable a, JSON a) => JSON (HashSet a) | |
| JSON a => JSON (FlatMap Text a) | default instance prefer later key |
| (PrimUnlifted a, JSON a) => JSON (UnliftedArray a) | |
Defined in Z.Data.JSON.Base Methods fromValue :: Value -> Converter (UnliftedArray a) # toValue :: UnliftedArray a -> Value # encodeJSON :: UnliftedArray a -> Builder () # | |
| HasResolution a => JSON (Fixed a) | This instance includes a bounds check to prevent maliciously large inputs to fill up the memory of the target system. You can newtype Fixed and provide your own instance using |
| JSON a => JSON (Map Text a) | |
| JSON a => JSON (HashMap Text a) | default instance prefer later key |
A JSON value represented as a Haskell value.
The Object's payload is a key-value vector instead of a map, which parsed
directly from JSON document. This design choice has following advantages:
- Allow different strategies handling duplicated keys.
- Allow different
Maptype to do further parsing, e.g.FlatMap - Roundtrip without touching the original key-value order.
- Save time if constructing map is not neccessary, e.g. using a linear scan to find a key if only that key is needed.
Constructors
| Object !(Vector (Text, Value)) | |
| Array !(Vector Value) | |
| String !Text | |
| Number !Scientific | |
| Bool !Bool | |
| Null |