Ejemplo n.º 1
0
        public async Task CanCreateBinaryDataFromNonSeekableStream()
        {
            byte[] buffer = Encoding.UTF8.GetBytes("some data");
            using MemoryStream stream = new NonSeekableStream(buffer);
            BinaryData data = BinaryData.FromStream(stream);

            Assert.Equal(buffer, data.ToArray());

            byte[] output       = new byte[buffer.Length];
            var    outputStream = data.ToStream();

            outputStream.Read(output, 0, (int)outputStream.Length);
            Assert.Equal(buffer, output);

            stream.Position = 0;
            data            = await BinaryData.FromStreamAsync(stream);

            Assert.Equal(buffer, data.ToArray());

            outputStream = data.ToStream();
            outputStream.Read(output, 0, (int)outputStream.Length);
            Assert.Equal(buffer, output);
        }
Ejemplo n.º 2
0
        public async Task CanCreateBinaryDataFromLongStream()
        {
            byte[] buffer = "some data" u8.ToArray();
            using MemoryStream stream = new OverFlowStream(offset: int.MaxValue - 10000, buffer);
            BinaryData data = BinaryData.FromStream(stream);

            Assert.Equal(buffer, data.ToArray());

            byte[] output       = new byte[buffer.Length];
            var    outputStream = data.ToStream();

            outputStream.Read(output, 0, (int)outputStream.Length);
            Assert.Equal(buffer, output);

            stream.Position = 0;
            data            = await BinaryData.FromStreamAsync(stream);

            Assert.Equal(buffer, data.ToArray());

            outputStream = data.ToStream();
            outputStream.Read(output, 0, (int)outputStream.Length);
            Assert.Equal(buffer, output);
        }
        public async Task CanDecodePreamble()
        {
            var client    = CreateClient();
            var groupName = TestEnvironment.SchemaRegistryGroup;

            var encoder = new SchemaRegistryAvroEncoder(client, groupName, new SchemaRegistryAvroObjectEncoderOptions {
                AutoRegisterSchemas = true
            });

            var employee = new Employee {
                Age = 42, Name = "Caketown"
            };
            EventData eventData = await encoder.EncodeMessageDataAsync <EventData>(employee);

            string schemaId = eventData.ContentType.Split('+')[1];

            eventData.ContentType = "avro/binary";

            using var stream = new MemoryStream();
            stream.Write(new byte[] { 0, 0, 0, 0 }, 0, 4);
            var encoding = new UTF8Encoding(false);

            stream.Write(encoding.GetBytes(schemaId), 0, 32);
            stream.Write(eventData.Body.ToArray(), 0, eventData.Body.Length);
            stream.Position     = 0;
            eventData.EventBody = BinaryData.FromStream(stream);

            Employee deserialized = await encoder.DecodeMessageDataAsync <Employee>(eventData);

            // decoding should not alter the message
            Assert.AreEqual("avro/binary", eventData.ContentType);

            // verify the payload was decoded correctly
            Assert.IsNotNull(deserialized);
            Assert.AreEqual("Caketown", deserialized.Name);
            Assert.AreEqual(42, deserialized.Age);
        }
Ejemplo n.º 4
0
 public SendReceiveTest(SizeOptions options) : base(options)
 {
     _payload = BinaryData.FromStream(RandomStream.Create(options.Size));
 }
 public DefaultServiceBusMessage(Stream data)
 {
     Data = BinaryData.FromStream(data);
 }
        private async Task <EventGridEvent[]> DeserializeEventGridEventsInternal(string requestContent, bool async, CancellationToken cancellationToken = default)
        {
            List <EventGridEventInternal> egInternalEvents = new List <EventGridEventInternal>();
            List <EventGridEvent>         egEvents         = new List <EventGridEvent>();

            // Deserialize raw JSON string into separate events, deserialize event envelope properties
            JsonDocument requestDocument = await ParseJsonToDocument(requestContent, async, cancellationToken).ConfigureAwait(false);

            foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
            {
                egInternalEvents.Add(EventGridEventInternal.DeserializeEventGridEventInternal(property));
            }

            // Deserialize 'Data' property from JsonElement for each event
            foreach (EventGridEventInternal egEventInternal in egInternalEvents)
            {
                JsonElement dataElement = egEventInternal.Data;
                object      egEventData = null;

                // Reserialize JsonElement to stream
                MemoryStream dataStream = SerializePayloadToStream(dataElement, cancellationToken);

                // First, let's attempt to find the mapping for the event type in the custom event mapping.
                if (_customEventTypeMappings.TryGetValue(egEventInternal.EventType, out Type typeOfEventData))
                {
                    if (!TryGetPrimitiveFromJsonElement(dataElement, out egEventData))
                    {
                        if (async)
                        {
                            egEventData = await _dataSerializer.DeserializeAsync(dataStream, typeOfEventData, cancellationToken).ConfigureAwait(false);
                        }
                        else
                        {
                            egEventData = _dataSerializer.Deserialize(dataStream, typeOfEventData, cancellationToken);
                        }
                    }
                }
                // If a custom mapping doesn't exist, let's attempt to find the mapping for the deserialization function in the system event type mapping.
                else if (SystemEventTypeMappings.SystemEventDeserializers.TryGetValue(egEventInternal.EventType, out Func <JsonElement, object> systemDeserializationFunction))
                {
                    egEventData = systemDeserializationFunction(dataElement);
                }
                else
                {
                    // If event data is not a primitive/string, return as BinaryData
                    if (!TryGetPrimitiveFromJsonElement(dataElement, out egEventData))
                    {
                        egEventData = BinaryData.FromStream(dataStream);
                    }
                }

                egEvents.Add(new EventGridEvent(
                                 egEventInternal.Subject,
                                 egEventData,
                                 egEventInternal.EventType,
                                 egEventInternal.DataVersion)
                {
                    Id        = egEventInternal.Id,
                    EventTime = egEventInternal.EventTime
                });
            }

            return(egEvents.ToArray());
        }
        private async Task <CloudEvent[]> DeserializeCloudEventsInternal(string requestContent, bool async, CancellationToken cancellationToken = default)
        {
            List <CloudEventInternal> cloudEventsInternal = new List <CloudEventInternal>();
            List <CloudEvent>         cloudEvents         = new List <CloudEvent>();

            // Deserialize raw JSON string into separate events, deserialize event envelope properties
            JsonDocument requestDocument = await ParseJsonToDocument(requestContent, async, cancellationToken).ConfigureAwait(false);

            foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
            {
                cloudEventsInternal.Add(CloudEventInternal.DeserializeCloudEventInternal(property));
            }

            // Deserialize 'Data' property from JsonElement for each event
            foreach (CloudEventInternal cloudEventInternal in cloudEventsInternal)
            {
                object cloudEventData = null;
                if (cloudEventInternal.DataBase64 != null)
                {
                    cloudEventData = Convert.FromBase64String(cloudEventInternal.DataBase64);
                }
                else
                {
                    JsonElement?dataElement = cloudEventInternal.Data;

                    if (dataElement.HasValue && dataElement.Value.ValueKind != JsonValueKind.Null)
                    {
                        // Reserialize JsonElement to stream
                        MemoryStream dataStream = SerializePayloadToStream(dataElement, cancellationToken);

                        // First, let's attempt to find the mapping for the event type in the custom event mapping.
                        if (_customEventTypeMappings.TryGetValue(cloudEventInternal.Type, out Type typeOfEventData))
                        {
                            if (!TryGetPrimitiveFromJsonElement(dataElement.Value, out cloudEventData))
                            {
                                if (async)
                                {
                                    cloudEventData = await _dataSerializer.DeserializeAsync(dataStream, typeOfEventData, cancellationToken).ConfigureAwait(false);
                                }
                                else
                                {
                                    cloudEventData = _dataSerializer.Deserialize(dataStream, typeOfEventData, cancellationToken);
                                }
                            }
                        }
                        // If a custom mapping doesn't exist, let's attempt to find the mapping for the deserialization function in the system event type mapping.
                        else if (SystemEventTypeMappings.SystemEventDeserializers.TryGetValue(cloudEventInternal.Type, out Func <JsonElement, object> systemDeserializationFunction))
                        {
                            cloudEventData = systemDeserializationFunction(dataElement.Value);
                        }
                        // If no custom mapping was added, either return a primitive/string, or an object wrapped as BinaryData
                        else
                        {
                            // If event data is not a primitive/string, return as BinaryData
                            if (!TryGetPrimitiveFromJsonElement(dataElement.Value, out cloudEventData))
                            {
                                cloudEventData = BinaryData.FromStream(dataStream);
                            }
                        }
                    }
                    else // Event has null data
                    {
                        cloudEventData          = null;
                        cloudEventInternal.Type = "";
                    }
                }

                cloudEvents.Add(new CloudEvent(
                                    cloudEventInternal.Source,
                                    cloudEventInternal.Type)
                {
                    Id              = cloudEventInternal.Id,
                    Data            = cloudEventData,
                    Time            = cloudEventInternal.Time,
                    DataSchema      = cloudEventInternal.Dataschema,
                    DataContentType = cloudEventInternal.Datacontenttype,
                    Subject         = cloudEventInternal.Subject
                });
            }

            return(cloudEvents.ToArray());
        }