json2 0.8.2 → 0.8.3
raw patch · 3 files changed
+26/−25 lines, 3 files
Files
- json2.cabal +1/−1
- src/Data/JSON2.hs +25/−23
- src/Data/JSON2/Instances/Time.hs +0/−1
json2.cabal view
@@ -1,5 +1,5 @@ Name: json2-Version: 0.8.2+Version: 0.8.3 Synopsis: Library provides support for JSON. Category: Data, Text, JSON Description: This library provides support for JSON.
src/Data/JSON2.hs view
@@ -3,16 +3,8 @@ {-# LANGUAGE IncoherentInstances #-} {-# LANGUAGE DeriveDataTypeable #-} {-|-1. /Pretty prints/ -Re-export module Data.JSON2.Pretty. Example of use:--@- *ghci> pp $ mkObj [(show x, x) | x <- [0..7]]- {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7}-@--2. /Renders JSON to String/+1. /Renders JSON to String/ Haskell value has a JSON string: @@ -31,7 +23,7 @@ :: Map String Int @ -3. /Conversion haskell values from and to JSON/+2. /Conversion haskell values from and to JSON/ This module provides many instances classes `FromJson` and `ToJson` for haskell data types.@@ -77,8 +69,9 @@ -} module Data.JSON2- ( -- * Re-export module for pretty printing- module Data.JSON2.Pretty + ( -- * Re-export modules+ module Data.JSON2.Pretty+ , module Data.JSON2.Internal -- * Base data types , Json (..) , Jsons (..)@@ -92,6 +85,8 @@ , emptyObj, (.=), mkObj -- * Merges JSON objects , (+=), merges, mergeRec+ -- * Query+ , projectionObj ) where @@ -108,6 +103,7 @@ import qualified Data.Map as Map import Data.Set (Set) import qualified Data.Set as Set+import Data.Maybe (catMaybes) import qualified Data.ByteString as B (ByteString) import qualified Data.ByteString.Lazy as L (ByteString)@@ -139,20 +135,17 @@ -- Building JSON objects -- | Create empty `Json` object.------ > pp $ emptyObj == {} emptyObj :: Json emptyObj = JObject Map.empty -- | Create single `Json` object.------ > pp ("key" .= (Just False)) == {"key": false} (.=) :: (ToJson v, Typeable v) => String -> v -> Json k .= v = JObject $ Map.singleton k (toJson v) -- | Create `Json` object from list. ----- > pp $ mkObj [("a", "old"), ("a", "new"), ("bb", "other")] == {"a": "new", "bb": "other"}+-- > ghci> pp $ mkObj [("a", "old"), ("a", "new"), ("bb", "other")]+-- > {"a": "new", "bb": "other"} mkObj :: (ToJson v, Typeable v) => [(String ,v)] -> Json mkObj xs = JObject $ Map.fromList (map (\(k,v) -> (k, toJson v)) xs) @@ -160,11 +153,9 @@ -- | Merge two `JObject`. Other `Json` values interpreted as `emptyObj`. ----- > pp $ ("a" .= "old") += ("a" .= "new") += ("bb" .= "other") == {"a": "new", "bb": "other"}------ > obj += emptyObj == emptyObj += obj--- > obj += obj == emptyObj += obj == obj += emptyObj --- > obj1 += (obj2 += obj3) == (obj1 += obj2) += obj3+-- > ghci > pp $ ("a" .= "old") += ("a" .= "new") += ("bb" .= "other")+-- > {"a": "new", "bb": "other"}+ (+=) :: Json -> Json -> Json (+=) (JObject x ) (JObject y) = JObject $ Map.union y x (+=) (JObject x ) _ = JObject x@@ -173,7 +164,8 @@ -- | Merge `Json` objects from list. ----- > pp $ merges [("a" .= "old"), ("a" .= "new"), ("bb" .= "other")] == {"a": "new", "bb": "other"}+-- > ghci> pp $ merges [("a" .= "old"), ("a" .= "new"), ("bb" .= "other")]+-- > {"a": "new", "bb": "other"} merges :: [Json] -> Json merges = foldl (+=) emptyObj @@ -185,6 +177,16 @@ mergeRec' (JObject x) (JObject y) = JObject $ Map.unionWith mergeRec' x y mergeRec' _ js = js +-- | Projection `Json` object to list of `Json` .+--+-- > > pp $ projectionObj ["b", "c", "b"] $ mkObj [("a",1),("b", 2), ("c", 3)]+-- > [2, 3, 2]+projectionObj :: [String] -> Json -> Jsons+projectionObj ps (JObject m) = case (nub ps) \\ (Map.keys m) of+ [] -> catMaybes $ map (\k -> Map.lookup k m) ps+ _ -> []+ where ks = Map.keys m+projectionObj _ _ = [] ------------------------- Instances -----------------------------
src/Data/JSON2/Instances/Time.hs view
@@ -20,7 +20,6 @@ module Data.JSON2.Instances.Time where import Data.JSON2-import Data.JSON2.Internal import Data.Time import Data.Time.Clock.POSIX(POSIXTime) -- POSIXTime synonym NominalDiffTime import System.Locale (iso8601DateFormat, defaultTimeLocale)