on master help sign in
Add instance KeyValue Object

Although Object (a.k.a. HashMap Text Value) is typically not exposed for constructing objects in the aeson interface, this instance does help when third-party packages demand Object values.

commit63c0c99f200018fb11973197524cea4e09f84240 authorRobert Hensing <robert@roberthensing.nl> dateWed 26 Sep 2018, 12:25 (8 years ago) parent59bddd7
2 files changed+21−1
Data/Aeson/Types/ToJSON.hs+7 tests/UnitTests.hs+14−1
Data/Aeson/Types/ToJSON.hs+7
@@ -346,6 +346,13 @@ instance KeyValue Pair where
346346 name .= value = (name, toJSON value)
347347 {-# INLINE (.=) #-}
348348
349+-- | Constructs a singleton 'H.HashMap'. For calling functions that
350+-- demand an 'Object' for constructing objects. To be used in
351+-- conjunction with 'mconcat'. Prefer to use 'object' where possible.
352+instance KeyValue Object where
353+ name .= value = H.singleton name (toJSON value)
354+ {-# INLINE (.=) #-}
355+
349356-------------------------------------------------------------------------------
350357-- Classes and types for map keys
351358-------------------------------------------------------------------------------
tests/UnitTests.hs+14−1
@@ -27,7 +27,7 @@ import Data.Aeson ((.=), (.:), (.:?), (.:!), FromJSON(..), FromJSONKeyFunction(.
2727import Data.Aeson.Internal (JSONPathElement(..), formatError)
2828import Data.Aeson.TH (deriveJSON, deriveToJSON, deriveToJSON1)
2929import Data.Aeson.Text (encodeToTextBuilder)
30−import Data.Aeson.Types (Options(..), Result(Success), ToJSON(..), Value(Null), camelTo, camelTo2, defaultOptions, omitNothingFields, parse)
30+import Data.Aeson.Types (Options(..), Result(Success), ToJSON(..), Value(Null, Object), camelTo, camelTo2, defaultOptions, omitNothingFields, parse)
3131import Data.Char (toUpper)
3232import Data.Either.Compat (isLeft, isRight)
3333import Data.Hashable (hash)
@@ -93,6 +93,7 @@ tests = testGroup "unit" [
9393 , testGroup ".:, .:?, .:!" $ fmap (testCase "-") dotColonMark
9494 , testGroup "JSONPath" $ fmap (testCase "-") jsonPath
9595 , testGroup "Hashable laws" $ fmap (testCase "-") hashableLaws
96+ , testGroup "Object construction" $ fmap (testCase "-") objectConstruction
9697 , testGroup "Issue #351" $ fmap (testCase "-") issue351
9798 , testGroup "Nullary constructors" $ fmap (testCase "-") nullaryConstructors
9899 , testGroup "FromJSONKey" $ fmap (testCase "-") fromJSONKeyAssertions
@@ -302,6 +303,18 @@ hashableLaws = [
302303 a = object ["223" .= False, "807882556" .= True]
303304 b = object ["807882556" .= True, "223" .= False]
304305
306+------------------------------------------------------------------------------
307+-- Check that an alternative way to construct objects works
308+------------------------------------------------------------------------------
309+
310+objectConstruction :: [Assertion]
311+objectConstruction = [
312+ assertEqual "Equal objects constructed differently" recommended notRecommended
313+ ]
314+ where
315+ recommended = object ["foo" .= True, "bar" .= (-1 :: Int)]
316+ notRecommended = Object (mconcat ["foo" .= True, "bar" .= (-1 :: Int)])
317+
305318-------------------------------------------------------------------------------
306319-- ToJSONKey
307320-------------------------------------------------------------------------------