Source: stream-manager/util.js

  1. /* eslint-disable no-restricted-syntax */
  2. /*
  3. * Copyright (c) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. */
  5. const exceptions = require('./exceptions');
  6. const utilInternal = require('./utilInternal');
  7. /**
  8. * Validate And Serialize an instance of class to Json bytes
  9. * @param o a instance object
  10. * @return byte array
  11. * @exception throws ValidationException
  12. */
  13. const validateAndSerializeToJsonBytes = (o) => {
  14. const validation = utilInternal.isInvalid(o);
  15. if (validation) {
  16. throw new exceptions.ValidationException(validation);
  17. }
  18. return utilInternal.serializeToJsonWithEmptyArrayAsNull(o.asMap());
  19. };
  20. /**
  21. * Deserialize the json byte array to an instance of class
  22. * @param bytes a bytes array
  23. * @param type instance type
  24. * @return object
  25. */
  26. const deserializeJsonBytesToObj = (bytes, type) => type.fromMap(JSON.parse(bytes.toString('utf-8')));
  27. module.exports = {
  28. validateAndSerializeToJsonBytes,
  29. deserializeJsonBytesToObj,
  30. };