Skip to content

JSON

JSON is the default in both directions, but not in the same sense:

  • Decode: an event without datacontenttype is read as application/json.
  • Encode: there is no default. The caller always passes the content type.
.Add<Candle>("application/json",
    ce => CandleJson.Decode(JsonNode.Parse((string)ce.Data!)!),
    candle => new CloudEvent
    {
        Data = CandleJson.Encode(candle).ToJsonString(),
        DataContentType = "application/json",
    })

The encoder sets only data, content type and (optionally) schema. Routing and the envelope are applied by the binding.

JSON and binary for the same value

var state  = codec.Encode(value, "application/json");
var change = codec.Encode(value, OrderBookImage.ContentType);

That is the intended pattern for an order book: the saved state is JSON, the published event is FlatBuffers.