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 FromValue
/ ToValue
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.
- Dose not support multiple doucments in one file.
import GHC.Generics import qualified Z.Data.YAML as YAML import qualified Z.Data.Text as T data Person = Person { name :: T.Text , age :: Int , magic :: Bool } deriving (Show, Generic) deriving anyclass (YAML.FromValue, YAML.ToValue) > YAML.decode @[Person] "- name: Erik Weiszn age: 52n magic: Truen" > Right [Person {name = "Erik Weisz", age = 52, magic = True}]
Synopsis
- decodeFromFile :: (HasCallStack, FromValue a) => CBytes -> IO a
- decodeValueFromFile :: HasCallStack => CBytes -> IO Value
- decode :: FromValue a => Bytes -> Either YAMLParseException a
- decodeValue :: Bytes -> Either YAMLParseException Value
- encodeToFile :: (HasCallStack, ToValue a) => YAMLFormatOpts -> CBytes -> a -> IO ()
- encodeValueToFile :: HasCallStack => YAMLFormatOpts -> CBytes -> Value -> IO ()
- encode :: (HasCallStack, ToValue a) => YAMLFormatOpts -> a -> Text
- encodeValue :: HasCallStack => YAMLFormatOpts -> Value -> Text
- data YAMLParseError
- data YAMLParseException
- parseSingleDoucment :: HasCallStack => Source MarkedEvent -> IO Value
- parseAllDocuments :: HasCallStack => Source MarkedEvent -> IO [Value]
- buildSingleDocument :: HasCallStack => Sink Event -> Value -> IO ()
- buildValue :: HasCallStack => Sink Event -> Value -> IO ()
- class FromValue a where
- class ToValue a where
- data Value
decode and encode using YAML
decodeFromFile :: (HasCallStack, FromValue a) => CBytes -> IO a Source #
Decode a FromValue
instance from file.
decodeValueFromFile :: HasCallStack => CBytes -> IO Value Source #
Decode a Value
from file.
decode :: FromValue a => Bytes -> Either YAMLParseException a Source #
Decode a FromValue
instance from bytes.
decodeValue :: Bytes -> Either YAMLParseException Value Source #
Decode a Value
from bytes.
encodeToFile :: (HasCallStack, ToValue a) => YAMLFormatOpts -> CBytes -> a -> IO () Source #
Encode a ToValue
instance to file.
encodeValueToFile :: HasCallStack => YAMLFormatOpts -> CBytes -> Value -> IO () Source #
Encode a Value
to file.
encode :: (HasCallStack, ToValue a) => YAMLFormatOpts -> a -> Text Source #
Encode a ToValue
instance as UTF8 text.
encodeValue :: HasCallStack => YAMLFormatOpts -> Value -> Text Source #
Encode a Value
as UTF8 text.
data YAMLParseError Source #
UnknownAlias Mark Mark Anchor | |
UnexpectedEvent MarkedEvent | |
NonStringKey Mark Mark Text | |
NonStringKeyAlias Mark Mark Anchor | |
UnexpectedEventEnd |
Instances
Eq YAMLParseError Source # | |
Defined in Z.Data.YAML (==) :: YAMLParseError -> YAMLParseError -> Bool # (/=) :: YAMLParseError -> YAMLParseError -> Bool # | |
Show YAMLParseError Source # | |
Defined in Z.Data.YAML showsPrec :: Int -> YAMLParseError -> ShowS # show :: YAMLParseError -> String # showList :: [YAMLParseError] -> ShowS # | |
Exception YAMLParseError Source # | |
Defined in Z.Data.YAML |
data YAMLParseException Source #
YAMLParseException YAMLParseError CallStack | |
YAMLConvertException ConvertError CallStack | |
MultipleDocuments CallStack |
Instances
Show YAMLParseException Source # | |
Defined in Z.Data.YAML showsPrec :: Int -> YAMLParseException -> ShowS # show :: YAMLParseException -> String # showList :: [YAMLParseException] -> ShowS # | |
Exception YAMLParseException Source # | |
Defined in Z.Data.YAML |
streaming parser and builder
parseSingleDoucment :: HasCallStack => Source MarkedEvent -> IO Value Source #
parseAllDocuments :: HasCallStack => Source MarkedEvent -> IO [Value] Source #
buildSingleDocument :: HasCallStack => Sink Event -> Value -> IO () Source #
Write a value as a YAML document stream.
Since: 0.11.2.0
buildValue :: HasCallStack => Sink Event -> Value -> IO () Source #
Write a value as a list of Event
s(without document start/end, stream start/end).
Since: 0.11.2.0
re-exports
Nothing
Instances
Typeclass for converting to JSON Value
.
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 |