Ejemplo n.º 1
0
        /// <summary>
        /// Given a single JSON-encoded event, parses the event envelope and returns a <see cref="CloudEvent"/>.
        /// If the specified event is not valid JSON an exception is thrown.
        /// By default, if the event is missing required properties, an exception is thrown though this can be relaxed
        /// by setting the <paramref name="skipValidation"/> parameter.
        /// </summary>
        /// <param name="json">An instance of <see cref="BinaryData"/> containing the JSON for the CloudEvent.</param>
        /// <param name="skipValidation">Set to <see langword="true"/> to allow missing or invalid properties to still parse into a CloudEvent.
        /// In particular, by setting strict to <see langword="true"/>, the source, id, specversion and type properties are no longer required
        /// to be present in the JSON. Additionally, the casing requirements of the extension attribute names are relaxed.
        /// </param>
        /// <returns> A <see cref="CloudEvent"/>. </returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="json"/> contained multiple events. <see cref="ParseMany"/> should be used instead.
        /// </exception>
        public static CloudEvent?Parse(BinaryData json, bool skipValidation = false)
        {
            Argument.AssertNotNull(json, nameof(json));

            using JsonDocument requestDocument = JsonDocument.Parse(json);
            CloudEvent?cloudEvent = null;

            if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
            {
                cloudEvent = CloudEventConverter.DeserializeCloudEvent(requestDocument.RootElement, skipValidation);
            }
            else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array)
            {
                if (requestDocument.RootElement.GetArrayLength() > 1)
                {
                    throw new ArgumentException(
                              "The BinaryData instance contains JSON from multiple cloud events. This method " +
                              "should only be used with BinaryData containing a single cloud event. " +
                              Environment.NewLine +
                              $"To parse multiple events, use the {nameof(ParseMany)} overload.");
                }
                foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
                {
                    cloudEvent = CloudEventConverter.DeserializeCloudEvent(property, skipValidation);
                    break;
                }
            }
            return(cloudEvent);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Given JSON-encoded events, parses the event envelope and returns an array of CloudEvents.
        /// If the specified event is not valid JSON an exception is thrown.
        /// By default, if the event is missing required properties, an exception is thrown though this can be relaxed
        /// by setting the <paramref name="skipValidation"/> parameter.
        /// </summary>
        /// <param name="json">An instance of <see cref="BinaryData"/> containing the JSON for one or more CloudEvents.</param>
        /// <param name="skipValidation">Set to <see langword="true"/> to allow missing or invalid properties to still parse into a CloudEvent.
        /// In particular, by setting strict to <see langword="true"/>, the source, id, specversion and type properties are no longer required
        /// to be present in the JSON. Additionally, the casing requirements of the extension attribute names are relaxed.
        /// </param>
        /// <returns> An array of <see cref="CloudEvent"/> instances.</returns>
        public static CloudEvent[] ParseMany(BinaryData json, bool skipValidation = false)
        {
            Argument.AssertNotNull(json, nameof(json));

            CloudEvent[]? cloudEvents = null;
            JsonDocument requestDocument = JsonDocument.Parse(json);

            // Parse JsonElement into separate events, deserialize event envelope properties
            if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
            {
                cloudEvents    = new CloudEvent[1];
                cloudEvents[0] = CloudEventConverter.DeserializeCloudEvent(requestDocument.RootElement, skipValidation);
            }
            else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array)
            {
                cloudEvents = new CloudEvent[requestDocument.RootElement.GetArrayLength()];
                int i = 0;
                foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
                {
                    cloudEvents[i++] = CloudEventConverter.DeserializeCloudEvent(property, skipValidation);
                }
            }
            return(cloudEvents ?? Array.Empty <CloudEvent>());
        }