Parses a JavaScript object previously serialised as aResult.toJSON()
into a proper Result structure.
This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.
type JSONSerialisation = {
"@@type": String,
"@@tag": String,
"@@value": Object Any
}
type JSONParser = {
fromJSON: (JSONSerialisation, Array JSONParser) => Variant
}
(JSONSerialisation, Array JSONParser) => Variant
Parses a JavaScript object previously serialised as aResult.toJSON()
into a proper Result structure.
See the docs for adt/union/derivations/serialization
for more details.
const Result = require('folktale/result');
Result.fromJSON(Result.Ok(1).toJSON());
// ==> Result.Ok(1)
Result.fromJSON(Result.Error(1).toJSON());
// ==> Result.Error(1)
function(value, parsers = { [typeName]: adt }, keysIndicateType = false) {
const valueTypeName = value[typeJsonKey];
const valueTagName = value[tagJsonKey];
const valueContents = value[valueJsonKey];
assertType(typeName, valueTypeName);
const parsersByType = keysIndicateType ? parsers
: /*otherwise*/ indexByType(values(parsers));
const parsedValue = mapValues(valueContents, parseValue(parsersByType));
return extend(Object.create(adt[valueTagName].prototype), parsedValue);
}