Z-YAML-0.3.3.0: YAML tools
Copyright(c) Dong Han 2020
LicenseBSD
Maintainerwinterland1989@gmail.com
Stabilityexperimental
Portabilitynon-portable
Safe HaskellNone
LanguageHaskell2010

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 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 #

Make a new YAML event sink, whose result are written to a file.

Note the file will be opened in O_APPEND .|. O_CREAT .|. O_WRONLY mode, bytes will be written after the end of the original file if there'are old bytes.

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

data YAMLError Source #

Instances

Instances details
Eq YAMLError Source # 
Instance details

Defined in Z.Data.YAML.FFI

Show YAMLError Source # 
Instance details

Defined in Z.Data.YAML.FFI

Generic YAMLError Source # 
Instance details

Defined in Z.Data.YAML.FFI

Associated Types

type Rep YAMLError :: Type -> Type #

Print YAMLError Source # 
Instance details

Defined in Z.Data.YAML.FFI

Methods

toUTF8BuilderP :: Int -> YAMLError -> Builder () #

Exception YAMLError Source # 
Instance details

Defined in Z.Data.YAML.FFI

type Rep YAMLError Source # 
Instance details

Defined in Z.Data.YAML.FFI

type Rep YAMLError = D1 ('MetaData "YAMLError" "Z.Data.YAML.FFI" "Z-YAML-0.3.3.0-KaULsWueam34jknx90zOm0" 'False) ((C1 ('MetaCons "ParseEventException" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 CBytes) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 CBytes) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Mark))) :+: (C1 ('MetaCons "ParseAliasEventWithEmptyAnchor" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Mark) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Mark)) :+: C1 ('MetaCons "ParseYAMLError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 YAMLParseError)))) :+: (C1 ('MetaCons "EmitEventException" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Event) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 CInt)) :+: (C1 ('MetaCons "EmitAliasEventWithEmptyAnchor" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OtherYAMLError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))))

data YAMLParseError Source #

Instances

Instances details
Eq YAMLParseError Source # 
Instance details

Defined in Z.Data.YAML.FFI

Show YAMLParseError Source # 
Instance details

Defined in Z.Data.YAML.FFI

Generic YAMLParseError Source # 
Instance details

Defined in Z.Data.YAML.FFI

Associated Types

type Rep YAMLParseError :: Type -> Type #

Print YAMLParseError Source # 
Instance details

Defined in Z.Data.YAML.FFI

Exception YAMLParseError Source # 
Instance details

Defined in Z.Data.YAML.FFI

type Rep YAMLParseError Source # 
Instance details

Defined in Z.Data.YAML.FFI

data ConvertError #

Error info with (JSON) Path info.

Instances

Instances details
Eq ConvertError 
Instance details

Defined in Z.Data.JSON.Converter

Ord ConvertError 
Instance details

Defined in Z.Data.JSON.Converter

Show ConvertError 
Instance details

Defined in Z.Data.JSON.Converter

Generic ConvertError 
Instance details

Defined in Z.Data.JSON.Converter

Associated Types

type Rep ConvertError :: Type -> Type #

Print ConvertError 
Instance details

Defined in Z.Data.JSON.Converter

NFData ConvertError 
Instance details

Defined in Z.Data.JSON.Converter

Methods

rnf :: ConvertError -> () #

type Rep ConvertError 
Instance details

Defined in Z.Data.JSON.Converter

type Rep ConvertError = D1 ('MetaData "ConvertError" "Z.Data.JSON.Converter" "Z-Data-0.8.1.0-43nnbD3qIHXEXOcVvJmdM1" 'False) (C1 ('MetaCons "ConvertError" 'PrefixI 'True) (S1 ('MetaSel ('Just "errPath") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [PathElement]) :*: S1 ('MetaSel ('Just "errMsg") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))

Re-Exports

class JSON a where #

Type class for encode & decode JSON.

Minimal complete definition

Nothing

Methods

fromValue :: Value -> Converter a #

toValue :: a -> Value #

encodeJSON :: a -> Builder () #

Instances

Instances details
JSON Bool 
Instance details

Defined in Z.Data.JSON.Base

JSON Char 
Instance details

Defined in Z.Data.JSON.Base

JSON Double 
Instance details

Defined in Z.Data.JSON.Base

JSON Float 
Instance details

Defined in Z.Data.JSON.Base

JSON Int 
Instance details

Defined in Z.Data.JSON.Base

JSON Int8 
Instance details

Defined in Z.Data.JSON.Base

JSON Int16 
Instance details

Defined in Z.Data.JSON.Base

JSON Int32 
Instance details

Defined in Z.Data.JSON.Base

JSON Int64 
Instance details

Defined in Z.Data.JSON.Base

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 Integer and provide your own instance using withScientific if you want to allow larger inputs.

Instance details

Defined in Z.Data.JSON.Base

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 Natural and provide your own instance using withScientific if you want to allow larger inputs.

Instance details

Defined in Z.Data.JSON.Base

JSON Ordering 
Instance details

Defined in Z.Data.JSON.Base

JSON Word 
Instance details

Defined in Z.Data.JSON.Base

JSON Word8 
Instance details

Defined in Z.Data.JSON.Base

JSON Word16 
Instance details

Defined in Z.Data.JSON.Base

JSON Word32 
Instance details

Defined in Z.Data.JSON.Base

JSON Word64 
Instance details

Defined in Z.Data.JSON.Base

JSON () 
Instance details

Defined in Z.Data.JSON.Base

Methods

fromValue :: Value -> Converter () #

toValue :: () -> Value #

encodeJSON :: () -> Builder () #

JSON Scientific

Note this instance doesn't reject large input

Instance details

Defined in Z.Data.JSON.Base

JSON ByteArray 
Instance details

Defined in Z.Data.JSON.Base

JSON CBytes

JSON instances check if CBytes is properly UTF8 encoded, if it is, decode/encode it as Text, otherwise as an object with a base64 field.

> encodeText ("hello" :: CBytes)
""hello""
> encodeText ("hello\NUL" :: CBytes)     -- \NUL is encoded as C0 80, which is illegal UTF8
"{"base64":"aGVsbG/AgA=="}"
Instance details

Defined in Z.Data.CBytes

JSON Value 
Instance details

Defined in Z.Data.JSON.Base

JSON FlatIntSet 
Instance details

Defined in Z.Data.JSON.Base

JSON Text 
Instance details

Defined in Z.Data.JSON.Base

JSON Bytes

This is an INCOHERENT instance, encode binary data with base64 encoding.

Instance details

Defined in Z.Data.JSON.Base

JSON FileEvent 
Instance details

Defined in Z.IO.FileSystem.Watch

JSON PathStyle 
Instance details

Defined in Z.IO.FileSystem.FilePath

JSON TimeVal 
Instance details

Defined in Z.IO.UV.FFI_Env

JSON ResUsage 
Instance details

Defined in Z.IO.UV.FFI_Env

JSON PID 
Instance details

Defined in Z.IO.UV.FFI_Env

JSON OSName 
Instance details

Defined in Z.IO.UV.FFI_Env

JSON PassWD 
Instance details

Defined in Z.IO.UV.FFI_Env

JSON CPUInfo 
Instance details

Defined in Z.IO.UV.FFI_Env

JSON TimeVal64 
Instance details

Defined in Z.IO.UV.FFI_Env

JSON DirEntType 
Instance details

Defined in Z.IO.UV.FFI

JSON UVTimeSpec 
Instance details

Defined in Z.IO.UV.FFI

JSON FStat 
Instance details

Defined in Z.IO.UV.FFI

JSON AccessResult 
Instance details

Defined in Z.IO.UV.FFI

JSON UID 
Instance details

Defined in Z.IO.UV.FFI

JSON GID 
Instance details

Defined in Z.IO.UV.FFI

JSON ProcessOptions 
Instance details

Defined in Z.IO.UV.FFI

JSON ProcessStdStream 
Instance details

Defined in Z.IO.UV.FFI

JSON SocketAddr 
Instance details

Defined in Z.IO.Network.SocketAddr

JSON IPv4 
Instance details

Defined in Z.IO.Network.SocketAddr

JSON IPv6 
Instance details

Defined in Z.IO.Network.SocketAddr

JSON PortNumber 
Instance details

Defined in Z.IO.Network.SocketAddr

JSON IntSet 
Instance details

Defined in Z.Data.JSON.Base

JSON Tag Source # 
Instance details

Defined in Z.Data.YAML.FFI

JSON Mark Source # 
Instance details

Defined in Z.Data.YAML.FFI

JSON MarkedEvent Source # 
Instance details

Defined in Z.Data.YAML.FFI

JSON Event Source # 
Instance details

Defined in Z.Data.YAML.FFI

JSON [Char]

This is an INCOHERENT instance, to provide JSON text encoding behaviour.

Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON [a] 
Instance details

Defined in Z.Data.JSON.Base

Methods

fromValue :: Value -> Converter [a] #

toValue :: [a] -> Value #

encodeJSON :: [a] -> Builder () #

JSON a => JSON (Maybe a) 
Instance details

Defined in Z.Data.JSON.Base

(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 withScientific if you want to allow larger inputs.

Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (FlatIntMap a) 
Instance details

Defined in Z.Data.JSON.Base

(Ord a, JSON a) => JSON (FlatSet a) 
Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (Vector a) 
Instance details

Defined in Z.Data.JSON.Base

(Prim a, JSON a) => JSON (PrimVector a) 
Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (Array a) 
Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (SmallArray a) 
Instance details

Defined in Z.Data.JSON.Base

(Prim a, JSON a) => JSON (PrimArray a) 
Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (NonEmpty a) 
Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (IntMap a) 
Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (Tree a) 
Instance details

Defined in Z.Data.JSON.Base

Methods

fromValue :: Value -> Converter (Tree a) #

toValue :: Tree a -> Value #

encodeJSON :: Tree a -> Builder () #

JSON a => JSON (Seq a) 
Instance details

Defined in Z.Data.JSON.Base

Methods

fromValue :: Value -> Converter (Seq a) #

toValue :: Seq a -> Value #

encodeJSON :: Seq a -> Builder () #

(Ord a, JSON a) => JSON (Set a) 
Instance details

Defined in Z.Data.JSON.Base

Methods

fromValue :: Value -> Converter (Set a) #

toValue :: Set a -> Value #

encodeJSON :: Set a -> Builder () #

(Eq a, Hashable a, JSON a) => JSON (HashSet a) 
Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (FlatMap Text a)

default instance prefer later key

Instance details

Defined in Z.Data.JSON.Base

(PrimUnlifted a, JSON a) => JSON (UnliftedArray a) 
Instance details

Defined in Z.Data.JSON.Base

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 withScientific if you want to allow larger inputs.

Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (Map Text a) 
Instance details

Defined in Z.Data.JSON.Base

JSON a => JSON (HashMap Text a)

default instance prefer later key

Instance details

Defined in Z.Data.JSON.Base

data Value #

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 Map type 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.

Instances

Instances details
Eq Value 
Instance details

Defined in Z.Data.JSON.Value

Methods

(==) :: Value -> Value -> Bool #

(/=) :: Value -> Value -> Bool #

Ord Value 
Instance details

Defined in Z.Data.JSON.Value

Methods

compare :: Value -> Value -> Ordering #

(<) :: Value -> Value -> Bool #

(<=) :: Value -> Value -> Bool #

(>) :: Value -> Value -> Bool #

(>=) :: Value -> Value -> Bool #

max :: Value -> Value -> Value #

min :: Value -> Value -> Value #

Show Value 
Instance details

Defined in Z.Data.JSON.Value

Methods

showsPrec :: Int -> Value -> ShowS #

show :: Value -> String #

showList :: [Value] -> ShowS #

Generic Value 
Instance details

Defined in Z.Data.JSON.Value

Associated Types

type Rep Value :: Type -> Type #

Methods

from :: Value -> Rep Value x #

to :: Rep Value x -> Value #

Arbitrary Value 
Instance details

Defined in Z.Data.JSON.Value

Methods

arbitrary :: Gen Value #

shrink :: Value -> [Value] #

JSON Value 
Instance details

Defined in Z.Data.JSON.Base

Print Value 
Instance details

Defined in Z.Data.JSON.Value

Methods

toUTF8BuilderP :: Int -> Value -> Builder () #

NFData Value 
Instance details

Defined in Z.Data.JSON.Value

Methods

rnf :: Value -> () #

type Rep Value 
Instance details

Defined in Z.Data.JSON.Value