RJson 0.3.4 → 0.3.5
raw patch · 3 files changed
+51/−26 lines, 3 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Text.RJson: instance [overlap ok] (Data ctx a[a28s], Data ctx b[a28t], Sat (ctx (Union a[a28s] b[a28t]))) => Data ctx (Union a[a28s] b[a28t])
+ Text.RJson: JDNull :: JsonData
+ Text.RJson: instance [overlap ok] (Data ctx a[a28u], Data ctx b[a28v], Sat (ctx (Union a[a28u] b[a28v]))) => Data ctx (Union a[a28u] b[a28v])
+ Text.RJson: instance [overlap ok] (FromJson a, TranslateField a, Typeable a) => FromJson (Maybe a)
+ Text.RJson: instance [overlap ok] (Typeable a, ToJson a) => ToJson (Maybe a)
Files
- README +31/−22
- RJson.cabal +3/−3
- Text/RJson.hs +17/−1
README view
@@ -1,8 +1,14 @@-Author: Alex Drummond.-Thanks to Dustin DeWeese for a patch fixing a bug in the parser.+************************************************************************+* RJSon 0.3.5 *+* Author: Alex Drummond *+* *+* Thanks to Dustin DeWeese for a patch fixing a bug in the parser. *+* Thanks to Adam Langley for a patch fixing the lack of support for *+* null JSON values. *+************************************************************************ -This is some documentation on how to use the library. I wrote a blog-post about an older version of the library which may contain some+This is some documentation on how to use the RJson library. I wrote a+blog post about an older version of the library which may contain some useful information on using syb-with-class: http://lingnerd.blogspot.com/2007/12/pushing-haskells-type-system-to-limits.html@@ -10,11 +16,11 @@ ------------------------------------------------------------------------ Not all features of the library are covered in this document yet.-Unfortunately, the haddock documentation will not build at the moment-because of the Template Haskell code in RJson.hs. You might want to-look at Text/RJson.hs to check out the Haddock comments for the-methods of ToJson and FromJson. +IMPORTANT: Be sure to check the end of this README, which details a+couple of issues with the library you should be aware of before using+it.+ ------------------------------------------------------------------------ Suppose we have the following datatypes:@@ -78,7 +84,8 @@ following expression will evaluate to Just the same TestRecord1 structure that we passed to 'toJson' earlier: - fromJsonString (undefined :: TestRecord1) "{\"_a\":\"foo\",\"_b\":{\"_c\":5,\"_d\":\"bar\"}}"+ fromJsonString (undefined :: TestRecord1)+ "{\"_a\":\"foo\",\"_b\":{\"_c\":5,\"_d\":\"bar\"}}" --> Right (TestRecord1 { _a="foo", _b=TestRecord2 { _c=5, _d="bar"}}) The first parameter of 'fromJsonString' (and 'fromJson') is a dummy@@ -142,15 +149,16 @@ constructor names to JSON strings and vice versa. The functions 'firstCharToUpper' and 'firstCharToLower' are provided by RJson. -Default serialization behavior is as follows:+Default serialization/deserialization behavior is as follows: - Haskell primitive types <--> Corresponding JSON type- Haskell records <--> JSON objects- Haskell tupels <--> Heterogenous JSON arrays- Haskell algebraic types <--> JSON array of arguments given to- constructor. First constructor- always used when deserializing.- (Not a very useful default.)+ Haskell primitive types <--> Corresponding JSON type+ Haskell records <--> JSON objects+ Haskell tupels <--> Heterogenous JSON arrays+ Haskell algebraic types &+ newtypes <--> JSON array of arguments given to+ constructor. First constructor+ always used when deserializing.+ (Not a very useful default.) Both ToJson and FromJson have some other methods which can be used to customize serialization behavior (check the Haddock@@ -171,14 +179,15 @@ constructor. Type synonyms are defined for complex unions of this kind (Union3 a b c, Union4 a b c d, etc. etc.) +------------------------------------------------------------------------+ WARNING: Record types with strict constructors will lead to runtime errors when using 'fromJson' ('toJson' will still work fine). This is because it seems to be necessary to temporarily create records with dummy field values. If the fields are strict, these dummy values get evaluated, leading to an exception being raised. -OTHER WEIRD BUG: You cannot have a field of type X and a field whose type is a-synonym of X in the same record. This leads to weird compile-time errors if you-try to serialize the record. I have no idea why (but this is normally easy to-work around).-+OTHER WEIRD BUG: You cannot have a field of type X and a field whose+type is a synonym of X in the same record. This leads to weird+compile-time errors if you try to serialize the record. I have no idea+why (but this is normally easy to work around).
RJson.cabal view
@@ -1,5 +1,5 @@ Name: RJson-Version: 0.3.4+Version: 0.3.5 Cabal-Version: >= 1.2 License: BSD3 License-File: LICENSE@@ -14,8 +14,8 @@ Description: See included README for some examples. This package uses the Scrap Your Boilerplate With Class approach- to generics to implement a reflective Json serializer and deserializer.- Nested record types can be automatically converted to corresponding+ to generics to implement a reflective JSON serializer and deserializer.+ Nested record types are automatically converted to corresponding JSON objects and vice versa. In both cases, various aspects of serializing and deserializing can be customized by implementing instances of type classes. Note that only Haskell 98 types can be
Text/RJson.hs view
@@ -64,6 +64,7 @@ JDNumber Double | JDArray [JsonData] | JDBool Bool |+ JDNull | JDObject (M.Map String JsonData) listJoin :: a -> [a] -> [a]@@ -120,6 +121,7 @@ show (JDBool True) = "true" show (JDBool False) = "false" show (JDArray l) = "[" ++ concatJoin "," (map show l) ++ "]"+ show JDNull = "null" show (JDObject o) = alistToJsonDict (map (\(k,v) -> (k, show v)) (M.toList o)) @@ -225,6 +227,10 @@ lToJson s = JDString s toJson c = JDString [c] +instance (Typeable a, ToJson a) => ToJson (Maybe a) where+ toJson (Just c) = toJson c+ toJson Nothing = JDNull+ instance (ToJson a, TranslateField a, Data TranslateFieldD (M.Map String a)) => ToJson (M.Map String a) where toJson x = JDObject (M.map toJson x)@@ -356,6 +362,13 @@ lFromJson _ (JDString s) = Right s lFromJson _ _ = Left "Bad fromJson conversion: Non-string to 'String'" +instance (FromJson a, TranslateField a, Typeable a) => FromJson (Maybe a) where+ fromJson _ JDNull = Right Nothing+ fromJson _ y =+ case fromJson undefined y of+ Left err -> Left err+ Right v -> Right $ Just v+ instance (FromJson a, TranslateField a, Typeable a) => FromJson [a] where fromJson _ x = lFromJson undefined x @@ -704,7 +717,10 @@ boolean = (P.try (P.string "true") >> return (JDBool True)) <|> (P.string "false" >> return (JDBool False)) -jsonValue = number <|> jsonString <|> jsonArray <|> object <|> boolean+jsonNull :: P.Parser JsonData+jsonNull = P.string "null" >> return JDNull++jsonValue = number <|> jsonString <|> jsonArray <|> object <|> boolean <|> jsonNull --