Skip to content

Commit

Permalink
(fix) Fixes serialization in the presence of undefined
Browse files Browse the repository at this point in the history
JSON does not support `undefined` as a value, but we do need to put
some value there as otherwise we can't properly reify the structure
(we iterate over keys and copy those descriptors to the reified structure).
For this reason, undefined is now serialised as `null`.
  • Loading branch information
robotlolita committed Mar 18, 2017
1 parent f4026e0 commit ffee127
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 5 additions & 2 deletions docs/source/en/data/result/to-json.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ See the docs for `core/adt/derivations/serialization` for more details.
Result.Ok(1).toJSON();
// ==> { '@@type': 'folktale:Data.Result', '@@tag': 'Ok', '@@value': { value: 1 } }

Result.Error().toJSON();
// ==> { '@@type': 'folktale:Data.Result', '@@tag': 'Error', '@@value': { value: 1 } }
Result.Error(1).toJSON();
// ==> { '@@type': 'folktale:Data.Result', '@@tag': 'Error', '@@value': { value: 1 } }

Result.Error(undefined).toJSON();
// ==> { '@@type': 'folktale:Data.Result', '@@tag': 'Error', '@@value': { value: null } }
5 changes: 3 additions & 2 deletions src/core/adt/derivations/serialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ const parseValue = (parsers) => (value) => {
* type: ('a) => JSON
*/
const serializeValue = (value) =>
value !== null && typeof value.toJSON === 'function' ? value.toJSON()
: /* otherwise */ value;
value === undefined ? null
: value !== null && typeof value.toJSON === 'function' ? value.toJSON()
: /* otherwise */ value;


// --[ Implementation ]-------------------------------------------------
Expand Down

0 comments on commit ffee127

Please sign in to comment.