/// <summary>
        /// Given JSON-encoded events, parses the event envelope and returns an array of EventGridEvents.
        /// If the content is not valid JSON, or events are missing required properties, an exception is thrown.
        /// </summary>
        /// <param name="json">An instance of <see cref="BinaryData"/> containing the JSON for one or more EventGridEvents.</param>
        /// <returns> An array of <see cref="EventGridEvent"/> instances.</returns>
        public static EventGridEvent[] ParseMany(BinaryData json)
        {
            Argument.AssertNotNull(json, nameof(json));

            EventGridEvent[] egEvents        = null;
            JsonDocument     requestDocument = JsonDocument.Parse(json);

            // Parse JsonElement into separate events, deserialize event envelope properties
            if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
            {
                egEvents    = new EventGridEvent[1];
                egEvents[0] = (new EventGridEvent(EventGridEventInternal.DeserializeEventGridEventInternal(requestDocument.RootElement)));
            }
            else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array)
            {
                egEvents = new EventGridEvent[requestDocument.RootElement.GetArrayLength()];
                int i = 0;
                foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
                {
                    egEvents[i++] = new EventGridEvent(EventGridEventInternal.DeserializeEventGridEventInternal(property));
                }
            }
            return(egEvents ?? Array.Empty <EventGridEvent>());
        }
Beispiel #2
0
        /// <summary>
        /// Given JSON-encoded events, parses the event envelope and returns an array of EventGridEvents.
        /// </summary>
        /// <param name="requestContent"> The JSON-encoded representation of either a single event or an array or events, encoded in the EventGridEvent schema. </param>
        /// <returns> A list of <see cref="EventGridEvent"/>. </returns>
        public static EventGridEvent[] Parse(string requestContent)
        {
            List <EventGridEventInternal> egEventsInternal = new List <EventGridEventInternal>();
            List <EventGridEvent>         egEvents         = new List <EventGridEvent>();
            JsonDocument requestDocument = JsonDocument.Parse(requestContent);

            // Parse JsonElement into separate events, deserialize event envelope properties
            if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
            {
                egEventsInternal.Add(EventGridEventInternal.DeserializeEventGridEventInternal(requestDocument.RootElement));
            }
            else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array)
            {
                foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
                {
                    egEventsInternal.Add(EventGridEventInternal.DeserializeEventGridEventInternal(property));
                }
            }

            foreach (EventGridEventInternal egEventInternal in egEventsInternal)
            {
                EventGridEvent egEvent = new EventGridEvent()
                {
                    Subject        = egEventInternal.Subject,
                    EventType      = egEventInternal.EventType,
                    DataVersion    = egEventInternal.DataVersion,
                    Id             = egEventInternal.Id,
                    EventTime      = egEventInternal.EventTime,
                    SerializedData = egEventInternal.Data
                };

                egEvents.Add(egEvent);
            }

            return(egEvents.ToArray());
        }