Skip to content

Commit 8f92dbf

Browse files
author
Poscat
committed
Clarify what the TaggedFlatObject option does
1 parent 57f92e2 commit 8f92dbf

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

src/Data/Aeson/Types/Internal.hs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,38 @@ data SumEncoding =
683683
-- record the encoded constructor contents will be stored under
684684
-- the 'contentsFieldName' field.
685685
| TaggedFlatObject { tagFieldName :: String }
686-
-- ^ A constructor will be encoded to an object with a field
686+
-- ^ Conceptually, this option will allow data types to be encoded to an object
687+
-- with an additional field 'tagFieldName' which specifies the constructor tag.
688+
-- This option differs from 'TaggedObject' in that the fields are encoded
689+
-- in the same object as the tag, instead of in another object under the
690+
-- field @contentsFieldName@.
691+
--
692+
-- The detailed behavior is as follows:
693+
--
694+
-- 1. If the data type has only a single constructor and has field names
695+
-- (a record), it will be encoded as an object without any additional fields.
696+
-- For example, given @A@ defined as
697+
-- @data A = A {field1 :: Int, field2 :: Int}@,
698+
-- this option will encode @A 1 2@ as @{"field1": 1, "field2": 2}@
699+
-- 2. If the data type has only a single constructor but does not have any fields,
700+
-- it will be encoded as an array.
701+
-- For example, given @A@ defined as
702+
-- @data A = A Int Int@,
703+
-- this option will encode @A 1 2@ as @[1, 2]@
704+
-- 3. If the data type has multiple constructors and the constructor has field names,
705+
-- it will be encoded as an object with an additional field '$tagFieldName'.
706+
-- For example, given @A@ defined as
707+
-- @data A = A {field1 :: Int, field2 :: Int} | B@,
708+
-- this option will encode @A 1 2@ as @{"field1": 1, "field2": 2, "$tagFieldName": \"A"}@
709+
-- 4. If the data type has multiple constructors and the constructor does not have
710+
-- any feild names, it will be encoded as an object whose keys are the position of the value
711+
-- in that data type with an additional field '$tagFieldName'.
712+
-- For example, given @A@ defined as
713+
-- @data A = A Int Int | B@,
714+
-- this option will encode @A 1 2@ as @{"1": 1, "2": 2, "$tagFieldName": \"A"}@
715+
-- 5. The behavior is undefined when the '$tagFieldName' collides with another field name and should
716+
-- not be relied upon. It may or may not overwite the field.
717+
-- It may or may not throw an runtime exception. It may or may not raise an compile time error.
687718
| UntaggedValue
688719
-- ^ Constructor names won't be encoded. Instead only the contents of the
689720
-- constructor will be encoded as if the type had a single constructor. JSON

0 commit comments

Comments
 (0)