on master help sign in
Merge branch 'master' of https://github.com/benweitzman/aeson
commit810a5a31019eccadba48700adc10f846bfe560cb authorBas van Dijk <v.dijk.bas@gmail.com> dateSat 10 Oct 2015, 15:49 (11 years ago) parent159de4f0b98185
3 files changed+22−10
Data/Aeson/Encode/Builder.hs+5 Data/Aeson/Types/Instances.hs+5−5 tests/Properties.hs+12−5
Data/Aeson/Encode/Builder.hs+5
@@ -23,6 +23,7 @@ module Data.Aeson.Encode.Builder
2323 , text
2424 , string
2525 , unquoted
26+ , quote
2627 , number
2728 , day
2829 , localTime
@@ -99,6 +100,10 @@ text t = B.char8 '"' <> unquoted t <> B.char8 '"'
99100unquoted :: T.Text -> Builder
100101unquoted t = TE.encodeUtf8BuilderEscaped escapeAscii t
101102
103+-- | Add quotes surrounding a builder
104+quote :: Builder -> Builder
105+quote b = B.char8 '"' <> b <> B.char8 '"'
106+
102107-- | Encode a JSON string.
103108string :: String -> Builder
104109string t = B.char8 '"' <> BP.primMapListBounded go t <> B.char8 '"'
Data/Aeson/Types/Instances.hs+5−5
@@ -713,14 +713,14 @@ instance FromJSON DotNetTime where
713713
714714instance ToJSON Day where
715715 toJSON = stringEncoding
716− toEncoding z = Encoding (E.day z)
716+ toEncoding z = Encoding (E.quote $ E.day z)
717717
718718instance FromJSON Day where
719719 parseJSON = withText "Day" (Time.run Time.day)
720720
721721instance ToJSON LocalTime where
722722 toJSON = stringEncoding
723− toEncoding z = Encoding (E.localTime z)
723+ toEncoding z = Encoding (E.quote $ E.localTime z)
724724
725725instance FromJSON LocalTime where
726726 parseJSON = withText "LocalTime" (Time.run Time.localTime)
@@ -728,7 +728,7 @@ instance FromJSON LocalTime where
728728instance ToJSON ZonedTime where
729729 toJSON = stringEncoding
730730
731− toEncoding z = Encoding (E.zonedTime z)
731+ toEncoding z = Encoding (E.quote $ E.zonedTime z)
732732
733733formatMillis :: (FormatTime t) => t -> String
734734formatMillis = take 3 . formatTime defaultTimeLocale "%q"
@@ -739,11 +739,11 @@ instance FromJSON ZonedTime where
739739instance ToJSON UTCTime where
740740 toJSON = stringEncoding
741741
742− toEncoding t = Encoding (E.utcTime t)
742+ toEncoding t = Encoding (E.quote $ E.utcTime t)
743743
744744-- | Encode something to a JSON string.
745745stringEncoding :: (ToJSON a) => a -> Value
746−stringEncoding = String . T.decodeLatin1 . L.toStrict . encode
746+stringEncoding = String . T.dropAround (== '"') . T.decodeLatin1 . L.toStrict . encode
747747{-# INLINE stringEncoding #-}
748748
749749instance FromJSON UTCTime where
tests/Properties.hs+12−5
@@ -14,7 +14,7 @@ import Encoders
1414import Instances ()
1515import Test.Framework (Test, testGroup)
1616import Test.Framework.Providers.QuickCheck2 (testProperty)
17−import Test.QuickCheck (Arbitrary(..), Property, (===))
17+import Test.QuickCheck (Arbitrary(..), Property, (===), (.&&.))
1818import Types
1919import qualified Data.Attoparsec.Lazy as L
2020import qualified Data.ByteString.Lazy.Char8 as L
@@ -39,16 +39,23 @@ toParseJSON parsejson tojson x =
3939 IError path msg -> failure "parse" (formatError path msg) x
4040 ISuccess x' -> x === x'
4141
42−roundTrip :: (FromJSON a, ToJSON a, Show a) =>
42+roundTripEnc :: (FromJSON a, ToJSON a, Show a) =>
4343 (a -> a -> Property) -> a -> a -> Property
44−roundTrip eq _ i =
45− case fmap ifromJSON . L.parse value . encode . toJSON $ i of
44+roundTripEnc eq _ i =
45+ case fmap ifromJSON . L.parse value . encode $ i of
4646 L.Done _ (ISuccess v) -> v `eq` i
4747 L.Done _ (IError path err) -> failure "fromJSON" (formatError path err) i
4848 L.Fail _ _ err -> failure "parse" err i
4949
50+roundTripNoEnc :: (FromJSON a, ToJSON a, Show a) =>
51+ (a -> a -> Property) -> a -> a -> Property
52+roundTripNoEnc eq _ i =
53+ case ifromJSON . toJSON $ i of
54+ (ISuccess v) -> v `eq` i
55+ (IError path err) -> failure "fromJSON" (formatError path err) i
56+
5057roundTripEq :: (Eq a, FromJSON a, ToJSON a, Show a) => a -> a -> Property
51−roundTripEq x y = roundTrip (===) x y
58+roundTripEq x y = roundTripEnc (===) x y .&&. roundTripNoEnc (===) x y
5259
5360toFromJSON :: (Arbitrary a, Eq a, FromJSON a, ToJSON a, Show a) => a -> Property
5461toFromJSON x = case ifromJSON (toJSON x) of