Permalink
Browse files

(fix) Fixes serialization in the presence of undefined

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 ffee127d155a56eaabd788fa13c2b55b76016636
Showing with 8 additions and 4 deletions.
  1. +5 −2 docs/source/en/data/result/to-json.md
  2. +3 −2 src/core/adt/derivations/serialization.js
@@ -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 } }
@@ -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 ]-------------------------------------------------

0 comments on commit ffee127

Please sign in to comment.