Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jhedev/xml-conduit-generics
Library to create xml representation of data type based on xml-conduit and GHC Generics
https://github.com/jhedev/xml-conduit-generics
Last synced: 7 days ago
JSON representation
Library to create xml representation of data type based on xml-conduit and GHC Generics
- Host: GitHub
- URL: https://github.com/jhedev/xml-conduit-generics
- Owner: jhedev
- License: mit
- Created: 2014-01-17T13:59:47.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2014-01-17T14:21:44.000Z (almost 11 years ago)
- Last Synced: 2024-11-05T09:51:23.514Z (about 2 months ago)
- Language: Haskell
- Homepage:
- Size: 137 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
xml-conduit-generics
====================Small library based on xml-conduit to create xml representations for
your own data types. It's just thrown together and still missing a few
default instances.#Example:
If we have a datatype like the following```
data User = User {
firstName :: String,
lastName :: String,
age :: Int,
hobbies :: [String]
}
deriving (Show, Generic)
```
With ```deriving Generic``` we let GHC derive the generic representations.
If we add```
instance ToXml User
```
the compiler will create this instance for us. So let's create two users and
write the xml to a file```
main :: IO()
main = do
writeFile def { rsPretty = True } "users.xml" $ Document (Prologue [] Nothing []) root []
where
john = User "John" "Doe" 44 ["jokes", "laughing"]
jane = User "Jane" "Doe" 38 []
users = (toXml) john ++ (toXml jane)
root = Element "users" empty users
```
This will create a users.xml file with the following content:```
John
Doe
44
jokeslaughing
Jane
Doe
38
```
You see there is a problem with the content of the hobbies tags. This is because the representation of
a list is just the concatenation of the representation of the elements and a string is represented by its
value. So this will just concat all strings in the list. To avoid this behaviour we create a simple
wrapper for our hobbies:```
data Hobby = Hobby String deriving (Show, Generic)instance ToXml Hobby
```
And change the user data type as follows:```
data User = User {
firstName :: String,
lastName :: String,
age :: Int,
hobbies :: [Hobby]
}
deriving (Show, Generic)
```After updating john and jane to:
```
john = User "John" "Doe" 44 [Hobby "jokes", Hobby "laughing"]
jane = User "Jane" "Doe" 38 []
```Now our xml file will look like this:
```
John
Doe
44
jokes
laughing
Jane
Doe
38
```