JSON-Combinator 0.2.6 → 0.2.7
raw patch · 8 files changed
+102/−2 lines, 8 filesdep +aesondep +attoparsecdep +textPVP ok
version bump matches the API change (PVP)
Dependencies added: aeson, attoparsec, text, vector
API changes (from Hackage documentation)
+ Text.JSON.Interact: instance Interact Text
+ Text.JSON.InteractFile: instance InteractFile Text
+ Text.JSON.JSONField: instance JSONField Value Text
+ Text.JSON.JSONLike: instance Data a => Data (TextMap a)
+ Text.JSON.JSONLike: instance Eq a => Eq (TextMap a)
+ Text.JSON.JSONLike: instance Foldable TextMap
+ Text.JSON.JSONLike: instance Functor TextMap
+ Text.JSON.JSONLike: instance JSONLike Value Text Vector TextMap
+ Text.JSON.JSONLike: instance Monoid (TextMap a)
+ Text.JSON.JSONLike: instance Read a => Read (TextMap a)
+ Text.JSON.JSONLike: instance Show a => Show (TextMap a)
+ Text.JSON.JSONLike: instance Traversable TextMap
+ Text.JSON.JSONLike: instance Typeable1 TextMap
+ Text.JSON.JSONParse: instance JSONParse Value ByteString (Either ([String], String) (ByteString -> Result Value))
+ Text.JSON.JSONPrepend: instance JSONPrepend Value Text
+ Text.JSON.JSONPrint: instance JSONPrint Value (Result [Char])
Files
- JSON-Combinator.cabal +8/−2
- Text/JSON/Interact.hs +7/−0
- Text/JSON/InteractFile.hs +7/−0
- Text/JSON/JSONField.hs +17/−0
- Text/JSON/JSONLike.hs +36/−0
- Text/JSON/JSONParse.hs +10/−0
- Text/JSON/JSONPrepend.hs +10/−0
- Text/JSON/JSONPrint.hs +7/−0
JSON-Combinator.cabal view
@@ -1,5 +1,5 @@ Name: JSON-Combinator-Version: 0.2.6+Version: 0.2.7 License: BSD3 License-File: LICENSE Author: Tony Morris <ʇǝu˙sıɹɹoɯʇ@ןןǝʞsɐɥ>@@ -10,13 +10,15 @@ A combinator library on top of a generalised JSON type. The highest-level module is @Text.JSON.Combinator@ and it is expected that no other module need to be explicitly imported. .- Instances for the combinators are provided for three libraries available on hackage:+ Instances for the combinators are provided for four libraries available on hackage: . * json (@Text.JSON@) . * JSONb (@Text.JSONb@) . * hjson (@Text.HJson@)+ .+ * aeson (@Data.Aeson@) Cabal-version: >= 1.2 Build-Type: Simple @@ -29,6 +31,10 @@ , JSONb , json , hjson+ , vector+ , text+ , attoparsec+ , aeson , containers , parsec , bytestring
Text/JSON/Interact.hs view
@@ -8,6 +8,8 @@ import Data.ByteString import qualified Data.ByteString as S+import Data.Text+import qualified Data.Text.IO as T class Interact z where getContents' ::@@ -33,3 +35,8 @@ putStr' = S.putStr +instance Interact Text where+ getContents' =+ T.getContents+ putStr' =+ T.putStr
Text/JSON/InteractFile.hs view
@@ -8,6 +8,8 @@ import Data.ByteString import qualified Data.ByteString as S+import Data.Text+import qualified Data.Text.IO as T class InteractFile z where readFile' ::@@ -30,3 +32,8 @@ writeFile' = S.writeFile +instance InteractFile Text where+ readFile' =+ T.readFile+ writeFile' =+ T.writeFile
Text/JSON/JSONField.hs view
@@ -11,10 +11,12 @@ import Text.JSONb import qualified Text.JSON as J import qualified Text.HJson as H+import qualified Data.Aeson.Types as A import Text.JSON.Types import Text.JSON.Failure import Control.Failure import qualified Data.Map as M+import Data.Text -- | Accessing JSON object fields. class JSONField j f | j -> f where@@ -83,3 +85,18 @@ values j = failure (ExpectedObject j) +instance JSONField A.Value Text where+ field f (A.Object o) =+ case M.lookup f o of+ Nothing -> failure (NoSuchFieldOrExpectedObject_NoSuchField f :: NoSuchFieldOrExpectedObject Text A.Value)+ Just x -> return x+ field _ j =+ failure (NoSuchFieldOrExpectedObject_ExpectedObject j :: NoSuchFieldOrExpectedObject Text A.Value)+ fields (A.Object o) =+ return (M.keys o)+ fields j =+ failure (ExpectedObject j)+ values (A.Object o) =+ return (fmap snd (M.toList o))+ values j =+ failure (ExpectedObject j)
Text/JSON/JSONLike.hs view
@@ -12,11 +12,14 @@ import Text.JSONb import qualified Text.JSON as J import qualified Text.HJson as H+import qualified Data.Aeson.Types as A import Text.JSON.Types import qualified Data.Map as M import Data.Foldable import Data.Traversable import Data.Monoid+import Data.Text+import Data.Vector import Data.Data -- | The generalisation of a JSON object.@@ -146,6 +149,39 @@ H.JArray jobject = H.JObject . runStringMap++newtype TextMap a =+ TextMap {+ runTextMap :: M.Map Text a+ } deriving (Eq, Show, Read, Functor, Foldable, Traversable, Monoid, Data, Typeable)++instance JSONLike A.Value Text Vector TextMap where+ foldJSON n _ _ _ _ _ _ A.Null =+ n+ foldJSON _ t f _ _ _ _ (A.Bool b) =+ if b then t else f+ foldJSON _ _ _ r _ _ _ (A.Number r') =+ r (toRational r')+ foldJSON _ _ _ _ s _ _ (A.String s') =+ s s'+ foldJSON _ _ _ _ _ a _ (A.Array a') =+ a a'+ foldJSON _ _ _ _ _ _ o (A.Object o') =+ o (TextMap o')+ jnull =+ A.Null+ jtrue =+ A.Bool True+ jfalse =+ A.Bool False+ jnumber =+ A.Number . fromRational+ jstring =+ A.String+ jarray =+ A.Array+ jobject =+ A.Object . runTextMap -- orphan instance, boooo instance Functor J.JSObject where
Text/JSON/JSONParse.hs view
@@ -11,6 +11,9 @@ import Text.JSONb import qualified Text.JSON as J import qualified Text.HJson as H+import qualified Data.Aeson.Types as A+import Data.Aeson.Parser+import qualified Data.Attoparsec as AP import Text.JSON.Parsec import qualified Text.Parsec.Prim as P import qualified Text.Parsec.Error as E@@ -34,6 +37,13 @@ instance JSONParse H.Json [Char] E.ParseError where parseJSON = P.runP H.jsonParser []++instance JSONParse A.Value ByteString (Either ([String], String) (ByteString -> AP.Result A.Value)) where+ parseJSON _ z =+ case AP.parse value z of+ AP.Fail _ r s -> Left (Left (r, s))+ AP.Partial c -> Left (Right c)+ AP.Done _ x -> Right x -- | Parse a value with an empty source name. parseJSON' ::
Text/JSON/JSONPrepend.hs view
@@ -11,8 +11,11 @@ import Text.JSONb import qualified Text.JSON as J import qualified Text.HJson as H+import qualified Data.Aeson.Types as A import Text.JSON.Types import qualified Data.Map as M+import qualified Data.Vector as V+import Data.Text -- | Prepending values to existing JSON association values. class JSONPrepend j s | j -> s where@@ -44,3 +47,10 @@ _ ->: x = x k -->>: H.JArray x = H.JArray (k : x) _ -->>: x = x++instance JSONPrepend A.Value Text where+ (k, v) ->: A.Object x = A.Object (M.insert k v x)+ _ ->: x = x+ k -->>: A.Array x = A.Array (V.cons k x)+ _ -->>: x = x+
Text/JSON/JSONPrint.hs view
@@ -10,6 +10,8 @@ import Text.JSONb import qualified Text.JSON as J import qualified Text.HJson as H+import qualified Data.Aeson.Types as A+import Data.Aeson.Generic import Text.JSON.Pretty import Text.HJson.Pretty @@ -31,3 +33,8 @@ instance JSONPrint H.Json [Char] where printJSON = toString " "++instance JSONPrint A.Value (A.Result [Char]) where+ printJSON =+ fromJSON+