packages feed

hjson (empty) → 1.1

raw patch · 4 files changed

+217/−0 lines, 4 filesdep +basedep +containersdep +parsecsetup-changed

Dependencies added: base, containers, parsec, safe

Files

+ COPYING view
@@ -0,0 +1,13 @@+Copyright (c) 2009, Voker57++All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++The name of author may not be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,3 @@+import Distribution.Simple++main = defaultMain
+ Text/HJson.hs view
@@ -0,0 +1,181 @@+{-# LANGUAGE FlexibleInstances,OverlappingInstances,TypeSynonymInstances #-}++module Text.HJson (Json(..), Jsonable(..), fromString, toString, escapeJString) where++import Data.Char+import Data.List+import qualified Data.Map as Map+import Data.Maybe+import Data.Ratio+import Safe+import Text.ParserCombinators.Parsec++data Json = JString String+	| JNumber Rational+	| JObject (Map.Map String Json)+	| JBool Bool+	| JNull+	| JArray [Json] deriving (Eq, Show)++-- | Renders JSON to String+toString :: Json -> String+toString (JNumber r) | denominator r == 1 = show (numerator r)+	| otherwise = show (fromRational r :: Double)+toString (JString s) = "\"" ++ escapeJString s ++ "\""+toString (JObject l) = "{" ++ (intercalate ", " $ map (\(k, v) -> toString (JString k) ++ ": " ++ toString v) (Map.toList l)) ++ "}"+toString (JBool True) = "true"+toString (JBool False) = "false"+toString JNull = "null"+toString (JArray vs) = "[" ++ (intercalate ", " $ map (toString) vs) ++ "]"++-- | Parses JSON string+fromString :: String -> Either String Json+fromString s = either (Left . show) (Right) $ parse valueP "user input" s++-- | Escapes string for inclusion in JSON+escapeJString :: String -> String+escapeJString = concat . map (escapeJChar)++-- | Class of types that can be converted to or from JSON+class Jsonable a where+	toJson :: a -> Json+	fromJson :: Json -> Maybe a+	fromJson = const Nothing++-- Simple, but useful+instance Jsonable Json where+	toJson = id+	fromJson _ = Nothing+instance Jsonable Bool where+	toJson b = JBool b+	fromJson (JBool b) = Just b+	fromJson _ = Nothing+instance Jsonable Integer where+ 	toJson = jsonifyIntegral+ 	fromJson (JNumber i) = Just $ round i+	fromJson _ = Nothing+instance Jsonable Int where+ 	toJson = jsonifyIntegral+ 	fromJson (JNumber i) = Just $ round i+	fromJson _ = Nothing+instance Jsonable Double where+	toJson = jsonifyRealFrac+	fromJson (JNumber i) = Just $ fromRational i+	fromJson _ = Nothing+instance Jsonable Float where+	toJson = jsonifyRealFrac+	fromJson (JNumber i) = Just $ fromRational i+	fromJson _ = Nothing+instance Jsonable String where+	toJson = JString+	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++-- private functions++-- Here I manually did instances' job. You know who to blame for its incompleteness.+jsonifyRealFrac i = JNumber (approxRational i 1e-666)+jsonifyIntegral i = JNumber (fromIntegral i % 1)++escapeJChar '\n' = "\\n"+escapeJChar '\b' = "\\b"+escapeJChar '\f' = "\\f"+escapeJChar '\t' = "\\t"+escapeJChar '\r' = "\\r"+escapeJChar '\\' = "\\\\"+escapeJChar '"' = "\\\""+escapeJChar c = [c]++-- Parser++valueP = do+	spaces+	jsonV <- stringP <|> numberP <|> objectP <|> arrayP <|> boolP <|> nullP+	spaces+	return jsonV++objectP = do+	char '{'+	spaces+	values <- keyValueP `sepBy` commaP+	spaces+	char '}'+	return $ JObject (Map.fromList values)++commaP = do+	spaces+	char ','+	spaces++keyValueP = do+	spaces+	JString keyStringV <- stringP+	spaces+	char ':'+	spaces+	valueV <- valueP+	spaces+	return (keyStringV, valueV)++arrayP = do+	char '['+	spaces+	values <- valueP `sepBy` commaP+	spaces+	char ']'+	return $ JArray values++stringP = do+	char '"'+	str <- manyTill stringElementP (char '"')+	return $ JString str++stringElementP = do+	escapeSeqP <|> anyChar++escapeSeqP = do+	char '\\'+	(char '"') <|>+		(char '\\') <|>+		(char '/') <|>+		('\b' <$ char 'b') <|>+		('\f' <$ char 'f') <|>+		('\n' <$ char 'n') <|>+		('\r' <$ char 'r') <|>+		('\t' <$ char 't') <|>+		unicodeP++unicodeP = do+	digitsV <- count 4 hexDigit+	let numberV = read ("0x" ++ digitsV)+	return $ chr numberV++numberP = do+	minusV <- optionMaybe (char '-')+	digitsV <- many1 digit+	maybeFractionalV <- optionMaybe (char '.' >> many digit)+	exponentV <- optionMaybe (do+		oneOf "eE"+		signV <- optionMaybe (char '+' <|> char '-')+		eDigitsV <- many1 digit+		let readDigits = read eDigitsV :: Integer+		return $ case signV of+			Just '-' -> ('-', readDigits)+			otherwise -> ('+', readDigits))+	let fractionalV = fromMaybe "" maybeFractionalV+	let upV = read (digitsV ++ fractionalV) :: Integer+	let downV = 10 ^ genericLength fractionalV+	return $ case exponentV of+		Nothing -> JNumber (upV % downV)+		Just ('-', powr) -> JNumber (upV % (downV * 10 ^ powr))+		Just (_, powr) -> JNumber ((upV * 10 ^ powr) % downV)++boolP = (JBool True <$ string "true") <|> (JBool True <$ string "false")++nullP = JNull <$ string "null"++x <$ m = m >> return x
+ hjson.cabal view
@@ -0,0 +1,20 @@+Name: hjson+Version: 1.1+Synopsis: JSON parsing library+Category: Text+Description: JSON parsing library with simple and sane API+License: BSD3+License-file: COPYING+Author: Voker57+Maintainer: voker57@gmail.com+Homepage: http://bitcheese.net/wiki/code/hjpath+Build-type: Simple+Cabal-version: >= 1.6++Source-repository head+    Type: git+    Location: git://git.bitcheese.net/hjson++library+ Exposed-Modules: Text.HJson+ Build-Depends: base >= 4 && < 5, parsec >= 2.1, containers, safe