/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
namespace QuantConnect.Util
{
///
/// Provides a json converter that allows defining the date time format used
///
public class DateTimeJsonConverter : JsonConverter
{
private readonly List _converters;
///
/// True, can read a json into a date time
///
public override bool CanRead => true;
///
/// True, can write a datetime to json
///
public override bool CanWrite => true;
///
/// Initializes a new instance of the class
///
/// >The date time format
public DateTimeJsonConverter(string format)
{
_converters = [new IsoDateTimeConverter() { DateTimeFormat = format }];
}
///
/// Initializes a new instance of the class
///
/// >The date time format
/// Other format for backwards compatibility
public DateTimeJsonConverter(string format, string format2) : this(format)
{
_converters.Add(new IsoDateTimeConverter() { DateTimeFormat = format2 });
}
///
/// Initializes a new instance of the class
///
/// >The date time format
/// Other format for backwards compatibility
/// Other format for backwards compatibility
public DateTimeJsonConverter(string format, string format2, string format3) : this(format, format2)
{
_converters.Add(new IsoDateTimeConverter() { DateTimeFormat = format3 });
}
///
/// True if can convert the given object type
///
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime) || objectType == typeof(string) || objectType == typeof(DateTime?);
}
///
/// Converts the given value
///
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
foreach (var converter in _converters)
{
try
{
return converter.ReadJson(reader, objectType, existingValue, serializer);
}
catch
{
}
}
throw new JsonSerializationException($"Unexpected value when converting date. Expected formats: {string.Join(",", _converters.Select(x => x.DateTimeFormat))}");
}
///
/// Writes the given value to json
///
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
_converters[0].WriteJson(writer, value, serializer);
}
}
}