Skip to content

Latest commit

 

History

History
238 lines (188 loc) · 8.83 KB

json-format.md

File metadata and controls

238 lines (188 loc) · 8.83 KB

JSON Event Format for CloudEvents - Version 0.1

Abstract

The JSON Format for CloudEvents defines how events are expressed in JavaScript Object Notation (JSON) Data Interchange Format (RFC8259).

Status of this document

This document is a working draft.

Table of Contents

  1. Introduction
  2. Attributes
  3. Envelope
  4. References

1. Introduction

CloudEvents is a standardized and transport-neutral definition of the structure and metadata description of events. This specification defines how the elements defined in the CloudEvents specification are to be represented in the JavaScript Object Notation (JSON) Data Interchange Format (RFC8259).

The Attributes section describes the naming conventions and data type mappings for CloudEvents attributes.

The Envelope section defines a JSON container for CloudEvents attributes and an associated media type.

1.1. Conformance

The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC2119.

2. Attributes

This section defines how CloudEvents attributes are mapped to JSON. This specification does not explicitly map each attribute, but provides a generic mapping model that applies to all current and future CloudEvents attributes, including extensions.

2.1. Base Type System

The core CloudEvents specification defines a minimal abstract type system, which this mapping leans on.

2.2. Type System Mapping

The CloudEvents type system MUST be mapped to JSON types as follows, with exceptions noted below.

CloudEvents JSON
String string
Integer number, only the int component is permitted
Binary string, Base64-encoded binary
URI-reference string
Timestamp string
Map JSON object
Any JSON value

Extension specifications MAY define diverging mapping rules for the values of attributes they define.

For instance, the attribute value might be a data structure defined in a standard outside of CloudEvents, with a formal JSON mapping, and there might be risk of translation errors or information loss when the original format is not preserved.

An extension specification that defines a diverging mapping rule for JSON, and any revision of such a specification, MUST also define explicit mapping rules for all other event formats that are part of the CloudEvents core at the time of the submission or revision.

2.3. Mapping Any-typed Attributes

The CloudEvents data attribute is Any-typed, meaning that it holds a value of any valid type. Map entry values are also Any typed.

If an implementation determines that the actual type of an Any is a String, the value MUST be represented as JSON string expression; for Binary, the value MUST represented as JSON string expression containing the Base64 encoded binary value; for Map, the value MUST be represented as a JSON object expression, whereby the index fields become member names and the associated values become the respective member's value.

2.4. Examples

The following table shows exemplary mappings:

CloudEvents Type Exemplary JSON Value
type String "com.example.someevent"
specversion String "0.1"
source URI-reference "/mycontext"
id String "1234-1234-1234"
time Timestamp "2018-04-05T17:31:00Z"
contenttype String "application/json"
data String "<much wow="xml"/>"
data Binary "Q2xvdWRFdmVudHM="
data Map { "objA" : "vA", "objB", "vB" }

2.5. JSONSchema Validation

The CloudEvents JSONSchema for the spec is located here and contains the definitions for validating events in JSON.

3. Envelope

Each CloudEvents event can be wholly represented as a JSON object.

Such a representation uses the media type application/cloudevents+json.

All REQUIRED and all not omitted OPTIONAL attributes in the given event MUST become members of the JSON object, with the respective JSON object member name matching the attribute name, and the member's type and value being mapped using the type system mapping.

3.1. Special Handling of the "data" Attribute

The mapping of the Any-typed data attribute follows the rules laid out in Section 2.3., with one additional rule:

If an implementation determines that the type of the data attribute is Binary or String, it MUST inspect the contenttype attribute to determine whether it is indicated that the data value contains JSON data.

If the contenttype value is either "application/json" or any media type with a structured +json suffix, the implementation MUST translate the data attribute value into a JSON value, and set the data attribute of the envelope JSON object to this JSON value.

If the contenttype value does not follow the structured +json suffix but is known to use JSON encoding, the implementation MUST translate the data attribute value into a JSON value, and set the data attribute of the envelope JSON object to this JSON value. Its typical examples are, but not limited to, text/json, application/json-seq and application/geo+json-seq.

Unlike all other attributes, for which value types are restricted to strings per the type-system mapping, the resulting data member JSON value is unrestricted, and MAY also contain numeric and logical JSON types.

3.2. Examples

Example event with String-valued data:

{
    "specversion" : "0.1",
    "type" : "com.example.someevent",
    "source" : "/mycontext",
    "id" : "A234-1234-1234",
    "time" : "2018-04-05T17:31:00Z",
    "comexampleextension1" : "value",
    "comexampleextension2" : {
        "otherValue": 5
    },
    "contenttype" : "text/xml",
    "data" : "<much wow=\"xml\"/>"
}

Example event with Binary-valued data

{
    "specversion" : "0.1",
    "type" : "com.example.someevent",
    "source" : "/mycontext",
    "id" : "B234-1234-1234",
    "time" : "2018-04-05T17:31:00Z",
    "comexampleextension1" : "value",
    "comexampleextension2" : {
        "otherValue": 5
    },
    "contenttype" : "application/vnd.apache.thrift.binary",
    "data" : "... base64 encoded string ..."
}

Example event with JSON data for the "data" member, either derived from a Map or JSON data data:

{
    "specversion" : "0.1",
    "type" : "com.example.someevent",
    "source" : "/mycontext",
    "id" : "C234-1234-1234",
    "time" : "2018-04-05T17:31:00Z",
    "comexampleextension1" : "value",
    "comexampleextension2" : {
        "otherValue": 5
    },
    "contenttype" : "application/json",
    "data" : {
        "appinfoA" : "abc",
        "appinfoB" : 123,
        "appinfoC" : true
    }
}

4. References

  • RFC2046 Multipurpose Internet Mail Extensions (MIME) Part Two: Media Types
  • RFC2119 Key words for use in RFCs to Indicate Requirement Levels
  • RFC4627 The application/json Media Type for JavaScript Object Notation (JSON)
  • RFC4648 The Base16, Base32, and Base64 Data Encodings
  • RFC6839 Additional Media Type Structured Syntax Suffixes
  • RFC8259 The JavaScript Object Notation (JSON) Data Interchange Format