packages feed

text-json-qq 0.1 → 0.1.1

raw patch · 2 files changed

+79/−30 lines, 2 filesPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

API changes (from Hackage documentation)

- Text.JSON.QQ: class ToJsonOrId a
- Text.JSON.QQ: instance ToJsonOrId Integer
- Text.JSON.QQ: instance ToJsonOrId JSValue
- Text.JSON.QQ: instance ToJsonOrId String
- Text.JSON.QQ: toJsonOrId :: (ToJsonOrId a) => a -> JSValue

Files

src/Text/JSON/QQ.hs view
@@ -1,4 +1,4 @@-{-# OPTIONS_GHC -fglasgow-exts -XTemplateHaskell -XQuasiQuotes #-}+{-# OPTIONS_GHC -fglasgow-exts -XTemplateHaskell -XQuasiQuotes -XUndecidableInstances #-}  -- | This package expose the function @jsonQQ@ that compile time converts json code into a @Text.JSON.JSValue@. --    @jsonQQ@ got the signature@@ -17,20 +17,28 @@ --     --    The quasiquatation can also bind to variables like --    ---    > myCode = [$jsonQQ | {age: <<age>>, name: <<name>>} |]+--    > myCode = [$jsonQQ| {age: <<age>>, name: <<name>>} |] --    > where age = 34 :: Integer --    >       name = "Pelle" --    ---    where a function  @toJsonOrId@ (that is also exported by @Text.JSON.QQ@) will be called on @age@ and @name@ runtime.+--    where the function  @toJSON@ will be called on @age@ and @name@ runtime.+--    +--    You can use a similar syntax if you want to insert a value of type JSValue like+--    +--    > myCode = [$jsonQQ| {"age": <<<age>>>} |]+--    +--    If you want to replace the name of the key in a hash you'll use the $-syntax:+--    +--    > foo = [$jsonQQ| {$bar: 42} |]+--    > bar = "age"+--      module Text.JSON.QQ (-  jsonQQ,-  ToJsonOrId (..)+  jsonQQ ) where  import Language.Haskell.TH import Language.Haskell.TH.Quote--- import Language.Haskell.Meta.Parse  import Data.Data import Data.Maybe@@ -39,10 +47,7 @@ import Text.JSON.Generic  import Data.Ratio--- import Parsec import Text.ParserCombinators.Parsec--- import qualified Data.ByteString.Lazy as L--- import Codec.Binary.UTF8.String (decode) import Text.ParserCombinators.Parsec.Error  jsonQQ :: QuasiQuoter@@ -80,8 +85,9 @@     where       jsList :: [Exp] -- [(String,JSValue)]       jsList = map objs2list (objs)-      objs2list :: (String,JsonValue) -> Exp-      objs2list (k,v) = TupE [LitE (StringL k), toExp v]+      objs2list :: (HashKey,JsonValue) -> Exp+      objs2list (HashStringKey k,v) = TupE [LitE (StringL k), toExp v]+      objs2list (HashVarKey k,v) = TupE [VarE $ mkName k, toExp v]    toExp (JsonArray arr) =     AppE (ConE $ mkName "Text.JSON.Types.JSArray") (ListE $ map toExp arr)@@ -90,22 +96,19 @@     AppE (AppE (ConE $ mkName "Text.JSON.Types.JSRational") (ConE $ mkName (if b then "True" else "False"))) (InfixE (Just (LitE (IntegerL $ numerator rat))) (VarE $ mkName "Data.Ratio.%") (Just (LitE (IntegerL $ denominator rat))))        toExp (JsonVar v) =-    AppE (VarE $ mkName "Text.JSON.QQ.toJsonOrId") (VarE $ mkName v)+    AppE (VarE $ mkName "Text.JSON.Generic.toJSON") (VarE $ mkName v) +  toExp (JsonIdVar v) =+    VarE $ mkName v+   toExp (JsonBool b) =     AppE (ConE $ mkName "Text.JSON.Types.JSBool") (ConE $ mkName (if b then "True" else "False")) -class ToJsonOrId a where-  toJsonOrId :: a -> JSValue--instance ToJsonOrId JSValue where-  toJsonOrId = id--instance ToJsonOrId String where-  toJsonOrId txt = Text.JSON.JSString $ Text.JSON.toJSString txt+-------+-- ToJsonOrId -instance ToJsonOrId Integer where-  toJsonOrId int = Text.JSON.JSRational True (int % 1)+-- class ToJsonOrId a where+--   toJsonOrId :: a -> JSValue  ------- -- Internal representation@@ -114,11 +117,20 @@   JsonNull   | JsonString String   | JsonNumber Bool Rational-  | JsonObject [(String,JsonValue)]+  | JsonObject [(HashKey,JsonValue)] -- [(String,JsonValue)]   | JsonArray [JsonValue]   | JsonVar String+  | JsonIdVar String   | JsonBool Bool +data HashKey =+  HashVarKey String+  | HashStringKey String++-- data JsonHashPair =+--   JsonStringPair String JsonValue+--   | JsonVarPair String JsonValue+ ------ -- Grammar -- jp = json parsec@@ -131,7 +143,7 @@ jpValue :: CharParser st JsonValue jpValue = do   spaces-  res <- jpTrue <|> jpFalse <|> try jpVar <|> jpNull <|> jpString <|> jpObject <|> try jpNumber  <|> jpArray+  res <- jpTrue <|> jpFalse <|> try jpVar <|> try jpIdVar <|> jpNull <|> jpString <|> jpObject <|> try jpNumber  <|> jpArray   spaces   return res @@ -145,6 +157,13 @@   string "false"   return $ JsonBool False +jpIdVar :: CharParser st JsonValue+jpIdVar = do+  string "<<<"+  sym <- symbol+  string ">>>"+  return $ JsonIdVar sym+ jpVar :: CharParser st JsonValue jpVar = do   string "<<"@@ -166,9 +185,8 @@  jpNumber :: CharParser st JsonValue  jpNumber = do-  isMinus <- optionMaybe (char '-')   val <- float-  return $ JsonNumber (not $ isJust isMinus) (toRational val)+  return $ JsonNumber False (toRational val)  jpObject :: CharParser st JsonValue jpObject = do@@ -179,10 +197,10 @@   char '}'   return $ JsonObject $ list   where-    jpHash :: CharParser st (String,JsonValue)+    jpHash :: CharParser st (HashKey,JsonValue) -- (String,JsonValue)     jpHash = do       spaces-      name <- symbol+      name <- varKey <|> symbolKey <|> quotedStringKey       spaces       char ':'       spaces@@ -190,6 +208,22 @@       spaces       return (name,value) +symbolKey :: CharParser st HashKey+symbolKey = do+  sym <- symbol+  return $ HashStringKey sym++quotedStringKey :: CharParser st HashKey+quotedStringKey = do+  quo <- quotedString+  return $ HashStringKey quo++varKey :: CharParser st HashKey+varKey = do+  char '$'+  sym <- symbol+  return $ HashVarKey sym+ jpArray :: CharParser st JsonValue jpArray = do   char '['@@ -204,10 +238,11 @@  float :: CharParser st Double  float = do+  isMinus <- option ' ' (char '-')   d <- many1 digit   o <- option "" withDot   e <- option "" withE-  return $ (read $ d ++ o ++ e :: Double)+  return $ (read $ isMinus : d ++ o ++ e :: Double)  withE = do   e <- char 'e' <|> char 'E'@@ -219,6 +254,20 @@   o <- char '.'   d <- many digit   return $ o:d++-- jsonSymbol :: CharParser st String+-- jsonSymbol = do+--   symbol <|> symbol'+--   where+--     symbol' = do+--       char '"'++quotedString :: CharParser st String+quotedString = do+  char '"'+  sym <- try $ option [""] $ many chars+  char '"'+  return $ concat sym   symbol :: CharParser st String symbol = many1 (noneOf "\\ \":;><") -- alphaNum -- should be replaced!
text-json-qq.cabal view
@@ -7,7 +7,7 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented.-Version:             0.1+Version:             0.1.1  -- A short (one-line) description of the package. Synopsis:            Json Quasiquatation for Haskell.