-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
122 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
# rison-hs | ||
Haskel library for parsing and rendering RISON strings. | ||
Haskell library for parsing and rendering [RISON](https://github.com/Nanonid/rison) strings. | ||
|
||
The parsing part is functional, the rison string gets parsed into Aeson | ||
Value objects. | ||
Rison gets parsed into and serialized from Aeson [Value](http://hackage.haskell.org/package/aeson-0.11.2.0/docs/Data-Aeson-Types.html#t:Value) objects. | ||
|
||
The rendering part is not yet implemented. | ||
Implementation partly inspired by [Aeson](https://github.com/bos/aeson). | ||
|
||
See the RISON spec at https://github.com/Nanonid/rison |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
module Data.Rison ( | ||
decode | ||
decode | ||
, encode | ||
) where | ||
|
||
import Data.Aeson ( Value(..) ) | ||
import qualified Data.Attoparsec.ByteString as A | ||
import Data.Attoparsec.ByteString.Char8 ( Parser ) | ||
import Data.ByteString ( ByteString ) | ||
import Data.Rison.Parser | ||
import Data.Rison.Writer | ||
|
||
decode :: ByteString -> Either String Value | ||
decode input = A.parseOnly rison input | ||
decode = A.parseOnly rison | ||
|
||
encode :: Value -> ByteString | ||
encode = write |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Data.Rison.Writer ( write ) where | ||
|
||
import Data.Aeson ( Value(..) ) | ||
import Data.ByteString ( ByteString ) | ||
import qualified Data.HashMap.Strict as H | ||
import qualified Data.List as L | ||
import Data.Maybe ( fromMaybe ) | ||
import Data.Monoid ( (<>) ) | ||
import Data.Scientific ( toRealFloat ) | ||
import qualified Data.Text as T | ||
import Data.Text.Encoding ( encodeUtf8 | ||
, decodeUtf8 ) | ||
import Data.Text.Format ( Only(..) | ||
, format | ||
, shortest ) | ||
import Data.Text.Internal.Builder ( toLazyText ) | ||
import qualified Data.Text.Lazy as LT | ||
import qualified Data.Vector as V | ||
|
||
write :: Value -> ByteString | ||
write Null = "!n" | ||
write (Bool True) = "!t" | ||
write (Bool False) = "!f" | ||
write (Number n) = encodeUtf8 . LT.toStrict . toLazyText . shortest $ n | ||
|
||
write (String s) = quot <> encodeUtf8 (T.foldr esc "" s) <> quot | ||
where | ||
esc c acc | c == '!' = "!!" <> acc | ||
| c == '\\' = "!\\" <> acc | ||
| otherwise = c `T.cons` acc | ||
|
||
quot = if T.null . T.filter escChars $ s | ||
then "" | ||
else "'" | ||
|
||
escChars '\\' = True | ||
escChars '!' = True | ||
escChars _ = False | ||
|
||
write (Object m) = encodeUtf8 $ | ||
"(" <> T.intercalate "," (fmap pair sortedKeys) <> ")" | ||
where | ||
sortedKeys = L.sort $ H.keys m | ||
pair k = k <> ":" <> (decodeUtf8 . write . fromMaybe "" . H.lookup k $ m) | ||
|
||
write (Array v) = encodeUtf8 $ | ||
"!(" <> T.intercalate "," | ||
(decodeUtf8 . write <$> V.toList v) <> ")" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters