src/plugin.js
/**
   * Serialize the given CloudEvent in JSON format.
   *
   * @param {!object} event the CloudEvent to serialize
   * @param {object} [options={}] optional serialization attributes:
   *        encoder (function, no default) a function that takes data and returns encoded data,
   *        encodedData (string, no default) already encoded data (but consistency with the datacontenttype is not checked),
   *        onlyValid (boolean, default false) to serialize only if it's a valid instance,
   * @return {string} the serialized event, as a string
   * @throws {Error} if event is undefined or null, or an option is undefined/null/wrong
   * @throws {Error} if onlyValid is true, and the given event is not a valid CloudEvent instance
   */
Serialize the given CloudEvent in JSON format.


Serialize the given CloudEvent in JSON format .
function serialize (event, { encoder, encodedData, onlyValid = false } = {}) {
    ensureIsObjectPlain(event, 'event')
    if (event.datacontenttype === CloudEvent.datacontenttypeDefault()) {
      if ((onlyValid === false) || (onlyValid === true && CloudEvent.isValidEvent(event) === true)) {
        return stringify(event)
      } else {
        throw new Error('Unable to serialize a not valid CloudEvent.')
      }
    }
    if (encoder !== undefined && encoder !== null) {
      if (typeof encoder !== 'function') {
        throw new Error(`Missing or wrong encoder function: '${encoder}' for the given content type: '${event.datacontenttype}'.`)
      }
      encodedData = encoder(event.payload)
    } else {
      if (!isValue(event.data) && !isDefinedAndNotNull(encodedData)) {
        throw new Error(`Missing encoder function: use encoder function or already encoded data with the given data content type: '${event.datacontenttype}'.`)
      }
      if (isValue(event.data) && !isDefinedAndNotNull(encodedData)) {
        encodedData = `${event.data}`
      }
    }
    if (typeof encodedData !== 'string') {
      throw new Error(`Missing or wrong encoded data: '${encodedData}' for the given content type: '${event.datacontenttype}'.`)
    }
    const newEvent = CloudEventTransformer.mergeObjects(event, { data: encodedData })
    if ((onlyValid === false) || (onlyValid === true && CloudEvent.isValidEvent(newEvent) === true)) {
      return stringify(newEvent)
    } else {
      throw new Error('Unable to serialize a not valid CloudEvent.')
    }
  }
function serialize ( event , { encoder , encodedData , onlyValid = false } = { } ) { ensureIsObjectPlain ( event , 'event' ) if ( event . datacontenttype === CloudEvent . datacontenttypeDefault ( ) ) { if ( ( onlyValid === false ) || ( onlyValid === true && CloudEvent . isValidEvent ( event ) === true ) ) { return stringify ( event ) } else { throw new Error ( 'Unable to serialize a not valid CloudEvent.' ) } } if ( encoder !== undefined && encoder !== null ) { if ( typeof encoder !== 'function' ) { throw new Error ( ` ${ encoder } ${ event . datacontenttype } ` ) } encodedData = encoder ( event . payload ) } else { if ( ! isValue ( event . data ) && ! isDefinedAndNotNull ( encodedData ) ) { throw new Error ( ` ${ event . datacontenttype } ` ) } if ( isValue ( event . data ) && ! isDefinedAndNotNull ( encodedData ) ) { encodedData = ` ${ event . data } ` } } if ( typeof encodedData !== 'string' ) { throw new Error ( ` ${ encodedData } ${ event . datacontenttype } ` ) } const newEvent = CloudEventTransformer . mergeObjects ( event , { data : encodedData } ) if ( ( onlyValid === false ) || ( onlyValid === true && CloudEvent . isValidEvent ( newEvent ) === true ) ) { return stringify ( newEvent ) } else { throw new Error ( 'Unable to serialize a not valid CloudEvent.' ) } }
Serialize the given CloudEvent in JSON format.
Serialize the given CloudEvent in JSON format .
// else (non defaut datacontenttype)
 // encoder not defined, check encodedData
 // but mandatory only for non-value data
 // console.log(`DEBUG - new event details: ${CloudEventTransformer.dumpObject(newEvent, 'newEvent')}`)
