Beispiel #1
0
 public void WritePropertyNameThrowsArgumentExceptionWhenPropertyNameIsEmptyToPreventOurOwnErrors()
 {
     using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
     {
         var JsonSerializationWriter = new JsonSerializationWriter(stringWriter);
         AssertEx.Throws <ArgumentException>(() => JsonSerializationWriter.WritePropertyName(string.Empty));
     }
 }
Beispiel #2
0
 public void WritePropertyStringThrowsArgumentNullExceptionForNameInputAsNull()
 {
     using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
     {
         var writer = new JsonSerializationWriter(stringWriter);
         AssertEx.Throws <ArgumentNullException>(() => writer.WriteProperty(null, "value"));
     }
 }
Beispiel #3
0
 private static void SerializeHelper(ITelemetry telemetryItem, JsonSerializationWriter jsonSerializationWriter, string baseType, string telemetryName)
 {
     jsonSerializationWriter.WriteProperty("name", telemetryItem.WriteTelemetryName(telemetryName));
     telemetryItem.WriteEnvelopeProperties(jsonSerializationWriter);
     jsonSerializationWriter.WriteStartObject("data");
     jsonSerializationWriter.WriteProperty("baseType", baseType);
     jsonSerializationWriter.WriteStartObject("baseData");
     telemetryItem.SerializeData(jsonSerializationWriter);
     jsonSerializationWriter.WriteEndObject(); // baseData
     jsonSerializationWriter.WriteEndObject(); // data
 }
Beispiel #4
0
 public void WritePropertyIDictionaryStringStringWritesValuesWithoutDoubleQuotes()
 {
     using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
     {
         var writer = new JsonSerializationWriter(stringWriter);
         writer.WriteProperty("name", new Dictionary <string, string> {
             { "key1", "1" }
         });
         AssertEx.Contains("\"key1\":\"1\"", stringWriter.ToString(), StringComparison.OrdinalIgnoreCase);
     }
 }
Beispiel #5
0
 public void WritePropertyIDictionaryStringStringWritesPropertyNameFollowedByValuesInCurlyBraces()
 {
     using (var stringWriter = new StringWriter(CultureInfo.InvariantCulture))
     {
         var writer = new JsonSerializationWriter(stringWriter);
         writer.WriteProperty("name", new Dictionary <string, string> {
             { "key1", "1" }
         });
         AssertEx.StartsWith("\"name\":{", stringWriter.ToString(), StringComparison.OrdinalIgnoreCase);
         AssertEx.EndsWith("}", stringWriter.ToString(), StringComparison.OrdinalIgnoreCase);
     }
 }
Beispiel #6
0
        private static void SerializeTelemetryItem(ITelemetry telemetryItem, JsonSerializationWriter jsonSerializationWriter)
        {
            jsonSerializationWriter.WriteStartObject();

            if (telemetryItem is IAiSerializableTelemetry serializeableTelemetry)
            {
                telemetryItem.CopyGlobalPropertiesIfExist();
                telemetryItem.FlattenIExtensionIfExists();
                SerializeHelper(telemetryItem, jsonSerializationWriter, telemetryName: serializeableTelemetry.TelemetryName, baseType: serializeableTelemetry.BaseType);
            }
            else
            {
                SerializeUnknownTelemetryHelper(telemetryItem, jsonSerializationWriter);
            }

            jsonSerializationWriter.WriteEndObject();
        }
Beispiel #7
0
        /// <summary>
        /// Serializes <paramref name="telemetryItems"/> and write the response to <paramref name="streamWriter"/>.
        /// </summary>
        private static void SeializeToStream(IEnumerable <ITelemetry> telemetryItems, TextWriter streamWriter)
        {
            // JsonWriter jsonWriter = new JsonWriter(streamWriter);
            JsonSerializationWriter jsonSerializationWriter = new JsonSerializationWriter(streamWriter);

            int telemetryCount = 0;

            foreach (ITelemetry telemetryItem in telemetryItems)
            {
                if (telemetryCount++ > 0)
                {
                    streamWriter.Write(Environment.NewLine);
                }

                telemetryItem.Context.SanitizeGlobalProperties();
                telemetryItem.Sanitize();
                SerializeTelemetryItem(telemetryItem, jsonSerializationWriter);
            }
        }
Beispiel #8
0
        private static void SerializeTelemetryItem(ITelemetry telemetryItem, JsonSerializationWriter jsonSerializationWriter)
        {
            jsonSerializationWriter.WriteStartObject();

            if (telemetryItem is EventTelemetry)
            {
                EventTelemetry eventTelemetry = telemetryItem as EventTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, eventTelemetry.Data.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, eventTelemetry.BaseType, EventTelemetry.TelemetryName);
            }
            else if (telemetryItem is ExceptionTelemetry)
            {
                ExceptionTelemetry exTelemetry = telemetryItem as ExceptionTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, exTelemetry.Data.Data.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, exTelemetry.BaseType, ExceptionTelemetry.TelemetryName);
            }
            else if (telemetryItem is MetricTelemetry)
            {
                MetricTelemetry mTelemetry = telemetryItem as MetricTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, mTelemetry.Data.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, mTelemetry.BaseType, MetricTelemetry.TelemetryName);
            }
            else if (telemetryItem is PageViewTelemetry)
            {
                PageViewTelemetry pvTelemetry = telemetryItem as PageViewTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, pvTelemetry.Data.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, pvTelemetry.BaseType, PageViewTelemetry.TelemetryName);
            }
            else if (telemetryItem is PageViewPerformanceTelemetry)
            {
                PageViewPerformanceTelemetry pvptelemetry = telemetryItem as PageViewPerformanceTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, pvptelemetry.Data.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, PageViewPerformanceTelemetry.BaseType, PageViewPerformanceTelemetry.TelemetryName);
            }
            else if (telemetryItem is DependencyTelemetry)
            {
                DependencyTelemetry depTelemetry = telemetryItem as DependencyTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, depTelemetry.InternalData.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, depTelemetry.BaseType, DependencyTelemetry.TelemetryName);
            }
            else if (telemetryItem is RequestTelemetry)
            {
                RequestTelemetry reqTelemetry = telemetryItem as RequestTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, reqTelemetry.Data.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, reqTelemetry.BaseType, RequestTelemetry.TelemetryName);
            }
#pragma warning disable 618
            else if (telemetryItem is PerformanceCounterTelemetry)
            {
                PerformanceCounterTelemetry pcTelemetry = telemetryItem as PerformanceCounterTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, pcTelemetry.Properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, pcTelemetry.Data.BaseType, MetricTelemetry.TelemetryName);
            }
            else if (telemetryItem is SessionStateTelemetry)
            {
                SessionStateTelemetry ssTelemetry = telemetryItem as SessionStateTelemetry;
                SerializeHelper(telemetryItem, jsonSerializationWriter, ssTelemetry.Data.BaseType, EventTelemetry.TelemetryName);
            }
#pragma warning restore 618
            else if (telemetryItem is TraceTelemetry)
            {
                TraceTelemetry traceTelemetry = telemetryItem as TraceTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, traceTelemetry.Data.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, traceTelemetry.BaseType, TraceTelemetry.TelemetryName);
            }
            else if (telemetryItem is AvailabilityTelemetry)
            {
                AvailabilityTelemetry availabilityTelemetry = telemetryItem as AvailabilityTelemetry;
                Utils.CopyDictionary(telemetryItem.Context.GlobalProperties, availabilityTelemetry.Data.properties);

                SerializeHelper(telemetryItem, jsonSerializationWriter, availabilityTelemetry.BaseType, AvailabilityTelemetry.TelemetryName);
            }
            else
            {
                string msg = string.Format(CultureInfo.InvariantCulture, "Unknown telemetry type: {0}", telemetryItem.GetType());
                CoreEventSource.Log.LogVerbose(msg);
            }

            jsonSerializationWriter.WriteEndObject();
        }
Beispiel #9
0
        public void SerializeComplexObject()
        {
            var complexExtension   = new ComplexExtension();
            var mySubSubExtension1 = new MySubSubExtension()
            {
                Field3 = "Value1 for field3", Field4 = 100.00
            };
            var mySubSubExtension2 = new MySubSubExtension()
            {
                Field3 = "Value2 for field3", Field4 = 200.00
            };
            var mySubExtension1 = new MySubExtension()
            {
                Field1 = "Value1 for field1", Field2 = 100, MySubSubExtension = mySubSubExtension1
            };
            var mySubExtension2 = new MySubExtension()
            {
                Field1 = "Value2 for field1", Field2 = 200, MySubSubExtension = mySubSubExtension2
            };
            var listExtension = new List <MySubExtension>();

            listExtension.Add(mySubExtension1);
            listExtension.Add(mySubExtension2);

            var listString = new List <string>();

            listString.Add("Item1");
            listString.Add("Item2");
            listString.Add("Item3");

            complexExtension.MyBoolField           = true;
            complexExtension.MyDateTimeOffsetField = DateTimeOffset.Now;
            complexExtension.MyDoubleField         = 100.10;
            complexExtension.MyIntField            = 100;
            complexExtension.MyStringField         = "ValueStringField";
            complexExtension.MyTimeSpanField       = TimeSpan.FromSeconds(2);
            complexExtension.MySubExtensionField   = mySubExtension1;
            complexExtension.MyExtensionListField  = listExtension;
            complexExtension.MyStringListField     = listString;

            var dicString = new Dictionary <string, string>();

            dicString.Add("Key1", "Value1");
            dicString.Add("Key2", "Value2");
            complexExtension.MyStringDictionaryField = dicString;

            var dicDouble = new Dictionary <string, double>();

            dicDouble.Add("Key1", 1000.000);
            dicDouble.Add("Key2", 2000.000);
            complexExtension.MyDoubleDictionaryField = dicDouble;

            var stringBuilder = new StringBuilder();

            using (StringWriter stringWriter = new StringWriter(stringBuilder, CultureInfo.InvariantCulture))
            {
                var jsonSerializationWriter = new JsonSerializationWriter(stringWriter);
                jsonSerializationWriter.WriteStartObject();
                complexExtension.Serialize(jsonSerializationWriter);
                jsonSerializationWriter.WriteEndObject();
            }

            string actualJson = stringBuilder.ToString();

            Trace.WriteLine(actualJson);

            JObject obj = JsonConvert.DeserializeObject <JObject>(actualJson);

            Assert.IsNotNull(actualJson);
            Assert.AreEqual("ValueStringField", obj["MyStringField"].ToString());
            Assert.AreEqual(100, int.Parse(obj["MyIntField"].ToString()));
            Assert.AreEqual(100.10, double.Parse(obj["MyDoubleField"].ToString()));
            Assert.AreEqual(true, bool.Parse(obj["MyBoolField"].ToString()));
            Assert.AreEqual(TimeSpan.FromSeconds(2), TimeSpan.Parse(obj["MyTimeSpanField"].ToString()));
            //Assert.AreEqual(DateTimeOffset., double.Parse(obj["MyDateTimeOffsetField"].ToString()));

            Assert.AreEqual("Value1 for field1", obj["MySubExtensionField"]["Field1"].ToString());
            Assert.AreEqual(100, int.Parse(obj["MySubExtensionField"]["Field2"].ToString()));

            Assert.AreEqual("Value1 for field3", obj["MySubExtensionField"]["MySubSubExtension"]["Field3"].ToString());
            Assert.AreEqual(100, int.Parse(obj["MySubExtensionField"]["MySubSubExtension"]["Field4"].ToString()));

            Assert.AreEqual("Item1", obj["MyStringListField"][0].ToString());
            Assert.AreEqual("Item2", obj["MyStringListField"][1].ToString());
            Assert.AreEqual("Item3", obj["MyStringListField"][2].ToString());

            Assert.AreEqual("Value1 for field1", obj["MyExtensionListField"][0]["Field1"].ToString());
            Assert.AreEqual(100, int.Parse(obj["MyExtensionListField"][0]["Field2"].ToString()));
            Assert.AreEqual("Value1 for field3", obj["MyExtensionListField"][0]["MySubSubExtension"]["Field3"].ToString());
            Assert.AreEqual(100, int.Parse(obj["MyExtensionListField"][0]["MySubSubExtension"]["Field4"].ToString()));

            Assert.AreEqual("Value2 for field1", obj["MyExtensionListField"][1]["Field1"].ToString());
            Assert.AreEqual(200, int.Parse(obj["MyExtensionListField"][1]["Field2"].ToString()));
            Assert.AreEqual("Value2 for field3", obj["MyExtensionListField"][1]["MySubSubExtension"]["Field3"].ToString());
            Assert.AreEqual(200, int.Parse(obj["MyExtensionListField"][1]["MySubSubExtension"]["Field4"].ToString()));

            Assert.AreEqual("Value1", obj["MyStringDictionaryField"]["Key1"].ToString());
            Assert.AreEqual("Value2", obj["MyStringDictionaryField"]["Key2"].ToString());

            Assert.AreEqual(1000, double.Parse(obj["MyDoubleDictionaryField"]["Key1"].ToString()));
            Assert.AreEqual(2000, double.Parse(obj["MyDoubleDictionaryField"]["Key2"].ToString()));
        }
Beispiel #10
0
        private static void SerializeUnknownTelemetryHelper(ITelemetry telemetryItem, JsonSerializationWriter jsonSerializationWriter)
        {
            DictionarySerializationWriter dictionarySerializationWriter = new DictionarySerializationWriter();

            telemetryItem.SerializeData(dictionarySerializationWriter); // Properties and Measurements are covered as part of Data if present
            telemetryItem.CopyGlobalPropertiesIfExist(dictionarySerializationWriter.AccumulatedDictionary);

            if (telemetryItem.Extension != null)
            {
                DictionarySerializationWriter extensionSerializationWriter = new DictionarySerializationWriter();
                telemetryItem.Extension.Serialize(extensionSerializationWriter); // Extension is supposed to be flattened as well

                Utils.CopyDictionary(extensionSerializationWriter.AccumulatedDictionary, dictionarySerializationWriter.AccumulatedDictionary);
                Utils.CopyDictionary(extensionSerializationWriter.AccumulatedMeasurements, dictionarySerializationWriter.AccumulatedMeasurements);
            }

            jsonSerializationWriter.WriteProperty("name", telemetryItem.WriteTelemetryName(EventTelemetry.TelemetryName));
            telemetryItem.WriteEnvelopeProperties(jsonSerializationWriter); // No need to copy Context - it's serialized here from the original item

            jsonSerializationWriter.WriteStartObject("data");
            jsonSerializationWriter.WriteProperty("baseType", typeof(EventData).Name);
            jsonSerializationWriter.WriteStartObject("baseData");

            jsonSerializationWriter.WriteProperty("ver", 2);
            jsonSerializationWriter.WriteProperty("name", EventNameForUnknownTelemetry);

            jsonSerializationWriter.WriteProperty("properties", dictionarySerializationWriter.AccumulatedDictionary);
            jsonSerializationWriter.WriteProperty("measurements", dictionarySerializationWriter.AccumulatedMeasurements);

            jsonSerializationWriter.WriteEndObject(); // baseData
            jsonSerializationWriter.WriteEndObject(); // data
        }