Serializing types to and from JSON

The SDK provides two helper functions to serialize and deserialize the data to and from JSON while retaining the type information.

  • types.reviver: Function to be passed to JSON.parse
  • types.replacer: Function to be passed to JSON.stringify

Serializing the data to JSON may be necessary if you are saving the data to LocalStorage or if you have a server that fetches the data and you want to pass the preloaded state to your Redux store.

Example:

const { reviver, replacer, UUID } = require('sharetribe-flex-sdk').types;

const testData = {
  id: new UUID('f989541d-7e96-4c8a-b550-dff1eef25815')
};

const roundtrip = JSON.parse(JSON.stringify(testData, replacer), reviver);

assert(roundtrip.id.constructor.name === 'UUID');

Please note: Your own types are not serialized.

Please note: When an API call fails, the error response is wrapped in Error object. Stringifying an Error object may result in unexpected results. Because of this, it's recommended that you do not stringify whole SDK responses as is, but instead pick the status, statusText and data fields from the response, and store and stringify those.