public override object ReadJson()

in src/Microsoft.TestPlatform.CommunicationUtilities/Serialization/TestResultConverter.cs [25:113]


    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        var data = JObject.Load(reader);

        var testCase = data["TestCase"].ToObject<TestCase>(serializer);
        var testResult = new TestResult(testCase);

        // Add attachments for the result
        var attachments = data["Attachments"];
        if (attachments != null && attachments.HasValues)
        {
            foreach (var attachment in attachments.Values<JToken>())
            {
                if (attachment.Type != JTokenType.Null)
                {
                    testResult.Attachments.Add(attachment.ToObject<AttachmentSet>(serializer));
                }
            }
        }

        // Add messages for the result
        var messages = data["Messages"];
        if (messages != null && messages.HasValues)
        {
            foreach (var message in messages.Values<JToken>())
            {
                if (message.Type != JTokenType.Null)
                {
                    testResult.Messages.Add(message.ToObject<TestResultMessage>(serializer));
                }
            }
        }

        JToken properties = data["Properties"];
        if (properties != null && properties.HasValues)
        {
            // Every class that inherits from TestObject uses a properties store for <Property, Object>
            // key value pairs.
            foreach (var property in properties.Values<JToken>())
            {
                var testProperty = property["Key"].ToObject<TestProperty>(serializer);

                // Let the null values be passed in as null data
                var token = property["Value"];
                string propertyData = null;
                if (token.Type != JTokenType.Null)
                {
                    // If the property is already a string. No need to convert again.
                    if (token.Type == JTokenType.String)
                    {
                        propertyData = token.ToObject<string>(serializer);
                    }
                    else
                    {
                        // On deserialization, the value for each TestProperty is always a string. It is up
                        // to the consumer to deserialize it further as appropriate.
                        propertyData = token.ToString(Formatting.None).Trim('"');
                    }
                }

                switch (testProperty.Id)
                {
                    case "TestResult.DisplayName":
                        testResult.DisplayName = propertyData; break;
                    case "TestResult.ComputerName":
                        testResult.ComputerName = propertyData ?? string.Empty; break;
                    case "TestResult.Outcome":
                        testResult.Outcome = (TestOutcome)Enum.Parse(typeof(TestOutcome), propertyData); break;
                    case "TestResult.Duration":
                        testResult.Duration = TimeSpan.Parse(propertyData); break;
                    case "TestResult.StartTime":
                        testResult.StartTime = DateTimeOffset.Parse(propertyData); break;
                    case "TestResult.EndTime":
                        testResult.EndTime = DateTimeOffset.Parse(propertyData); break;
                    case "TestResult.ErrorMessage":
                        testResult.ErrorMessage = propertyData; break;
                    case "TestResult.ErrorStackTrace":
                        testResult.ErrorStackTrace = propertyData; break;
                    default:
                        // No need to register member properties as they get registered as part of TestResultProperties class.
                        testProperty = TestProperty.Register(testProperty.Id, testProperty.Label, testProperty.GetValueType(), testProperty.Attributes, typeof(TestObject));
                        testResult.SetPropertyValue(testProperty, propertyData);
                        break;
                }
            }
        }

        return testResult;
    }