﻿using System;
using Newtonsoft.Json;

namespace Glimpse.Internal
{
    public class TimeSpanConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var result = 0.0;
            
            var convertedNullable = value as TimeSpan?; 
            if (convertedNullable.HasValue)
            {
                result = Math.Round(convertedNullable.Value.TotalMilliseconds, 2);
            }
            
            writer.WriteRawValue(result.ToString());
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return reader.Value;
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(TimeSpan) || objectType == typeof(TimeSpan?);
        }
    }
}

﻿using System;
using System.Globalization;
using Newtonsoft.Json;

namespace Glimpse.Internal
{
    public class TimeSpanConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var result = 0.0;
            
            var convertedNullable = value as TimeSpan?; 
            if (convertedNullable.HasValue)
            {
                result = Math.Round(convertedNullable.Value.TotalMilliseconds, 2);
            }
            
            writer.WriteRawValue(result.ToString(CultureInfo.InvariantCulture));
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return reader.Value;
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(TimeSpan) || objectType == typeof(TimeSpan?);
        }
    }
}

Fix invalid JSON caused by localized decimal mark
Fix invalid JSON caused by localized decimal mark

Using InvariantCulture to convert the double to string to generate a dot instead of a comma.
