hjson 1.1.3 → 1.2
raw patch · 3 files changed
+24/−8 lines, 3 files
Files
- Text/HJson.hs +3/−6
- Text/HJson/Pretty.hs +19/−0
- hjson.cabal +2/−2
Text/HJson.hs view
@@ -71,12 +71,9 @@ fromJson (JString s) = Just s fromJson _ = Nothing instance (Jsonable a) => Jsonable (Map.Map String a) where- toJson = JObject . Map.mapWithKey (\_ v -> (toJson v))- fromJson (JObject m) = Just $ Map.fromList $ catMaybes $ map (\(k, v) -> maybe (Nothing) (\jv -> Just (k, jv)) (fromJson v)) $ Map.toList m- fromJson _ = Nothing-instance (Jsonable a) => [a] where- toJson = undefined- fromJson = undefined+ toJson = JObject . Map.mapWithKey (\_ v -> (toJson v))+ fromJson (JObject m) = Just $ Map.fromList $ catMaybes $ map (\(k, v) -> maybe (Nothing) (\jv -> Just (k, jv)) (fromJson v)) $ Map.toList m+ fromJson _ = Nothing -- private functions
+ Text/HJson/Pretty.hs view
@@ -0,0 +1,19 @@+module Text.HJson.Pretty (toString) where++import Data.List+import qualified Data.Map as Map+import qualified Text.HJson as JSON+import Text.HJson (Json(..))++-- | Pretty-prints JSON with given indenter+toString :: String -> Json -> String+toString s j = toString' s 0 j++toString' :: String -> Integer -> Json -> String++toString' indenter levels (JArray xs) = "[" ++ (intercalate ", " $ map (toString' indenter levels) xs) ++ "]"++toString' indenter levels (JObject mp) = let+ currentIndent = (concat (genericReplicate levels indenter))+ in intercalate "\n" $ ["{", intercalate ",\n" (map (((currentIndent ++ indenter) ++) . (\(key, value) -> toString' indenter levels (JString key) ++ ": " ++ toString' indenter (levels + 1) value)) $ Map.toList mp), currentIndent ++ "}"]+toString' _ _ a = JSON.toString a
hjson.cabal view
@@ -1,5 +1,5 @@ Name: hjson-Version: 1.1.3+Version: 1.2 Synopsis: JSON parsing library Category: Text Description: JSON parsing library with simple and sane API@@ -15,5 +15,5 @@ Location: git://git.bitcheese.net/hjson library- Exposed-Modules: Text.HJson+ Exposed-Modules: Text.HJson, Text.HJson.Pretty Build-Depends: base >= 4 && < 5, parsec >= 3.0.0, containers, safe