Copyright | (c) Dong Han 2020 |
---|---|
License | BSD |
Maintainer | winterland1989@gmail.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
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 Event
s(without document start/end, stream start/end).
Errors
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 #
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.
Nothing
Instances
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.
Object !(Vector (Text, Value)) | |
Array !(Vector Value) | |
String !Text | |
Number !Scientific | |
Bool !Bool | |
Null |