Skip to content

Commit

Permalink
Merge pull request #5 from amphasis/master
Browse files Browse the repository at this point in the history
New release.
Implement DateTime fields support
  • Loading branch information
Xambey authored Jun 6, 2019
2 parents 11a5b58 + ab95e9d commit 51a60e7
Show file tree
Hide file tree
Showing 10 changed files with 556 additions and 23 deletions.
403 changes: 403 additions & 0 deletions Dadata.Test/DeserializationTest.cs

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions Dadata/Converters/TimestampToDateTimeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using DaData.Exceptions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Globalization;

namespace DaData.Converters
{
public class TimestampToDateTimeConverter : DateTimeConverterBase
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
TimeSpan offset;

switch (value)
{
case null:
writer.WriteNull();
return;

case DateTime dateTime:
offset = dateTime.ToUniversalTime().Subtract(TimestampBase);
break;

case DateTimeOffset dateTimeOffset:
offset = dateTimeOffset.ToUniversalTime().Subtract(TimestampOffsetBase);
break;

default:
throw new JsonSerializationException("Expected date object value.");
}

writer.WriteValue(offset.Ticks / TimeSpan.TicksPerMillisecond);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
try
{
long timestamp;

switch (reader.TokenType)
{
case JsonToken.Null:
if (IsNullableType(objectType)) return null;
throw new Exception($"Cannot convert null value to {objectType}.");

case JsonToken.String:
timestamp = long.Parse((string) reader.Value);
break;

case JsonToken.Integer:
timestamp = Convert.ToInt64(reader.Value);
break;

default:
throw new Exception($"Unsupported token type: {reader.TokenType}");
}

return IsDateTimeOffsetType(objectType)
? (object) TimestampOffsetBase.AddMilliseconds(timestamp)
: (object) TimestampBase.AddMilliseconds(timestamp);
}
catch (Exception ex)
{
var message = string.Format(CultureInfo.InvariantCulture, "Error converting value {0} to type '{1}'.", reader.Value, objectType);
throw new JsonConverterException(reader, message, ex);
}
}

private static readonly DateTime TimestampBase = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

private static readonly DateTimeOffset TimestampOffsetBase = new DateTimeOffset(TimestampBase);

private static bool IsNullableType(Type type)
{
if (type.IsByRef) return true;
if (!type.IsConstructedGenericType) return false;
return type.GetGenericTypeDefinition() == typeof(Nullable<>);
}

private static bool IsDateTimeOffsetType(Type type)
{
if (type == typeof(DateTimeOffset)) return true;
if (!type.IsConstructedGenericType) return false;
if (type.GetGenericTypeDefinition() != typeof(Nullable<>)) return false;
return Nullable.GetUnderlyingType(type) == typeof(DateTimeOffset);
}
}
}
31 changes: 31 additions & 0 deletions Dadata/Exceptions/JsonConverterException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Newtonsoft.Json;
using System;
using System.Globalization;

namespace DaData.Exceptions
{
public class JsonConverterException : JsonSerializationException
{
public JsonConverterException(JsonReader reader, string message, Exception exception)
: base(FormatMessage(reader as IJsonLineInfo, reader.Path, message), exception)
{
}

private static string FormatMessage(IJsonLineInfo lineInfo, string path, string message)
{
if (!message.EndsWith(Environment.NewLine, StringComparison.Ordinal))
{
message = message.Trim();
if (!message.EndsWith(".")) message += ".";
message += " ";
}

message += string.Format(CultureInfo.InvariantCulture, "Path '{0}'", path);
if (lineInfo != null && lineInfo.HasLineInfo())
message += string.Format(CultureInfo.InvariantCulture, ", line {0}, position {1}", lineInfo.LineNumber,
lineInfo.LinePosition);
message += ".";
return message;
}
}
}
10 changes: 5 additions & 5 deletions Dadata/Http/HttpExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using DaData.Converters;
using DaData.Exceptions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace DaData.Http
{
public static class HttpExtensions
{
private static JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings
private static readonly JsonSerializerSettings SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
Expand All @@ -23,12 +22,13 @@ public static class HttpExtensions
Formatting = Formatting.Indented
};

private static JsonSerializerSettings DeserializerSettings = new JsonSerializerSettings
private static readonly JsonSerializerSettings DeserializerSettings = new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver
{
NamingStrategy = new SnakeCaseNamingStrategy()
},
Converters = {new TimestampToDateTimeConverter()},
Formatting = Formatting.Indented
};

Expand Down
6 changes: 4 additions & 2 deletions Dadata/Models/Additional/Data/DataDocumentInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace DaData.Models.Additional.Data
using System;

namespace DaData.Models.Additional.Data
{
public class DataDocumentInfo
{
Expand All @@ -8,7 +10,7 @@ public class DataDocumentInfo

public string Number { get; set; }

public string IssueDate { get; set; }
public DateTime? IssueDate { get; set; }

public string IssueAuthority { get; set; }
}
Expand Down
11 changes: 6 additions & 5 deletions Dadata/Models/Additional/Data/DataLicense.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace DaData.Models.Additional.Data
{
Expand All @@ -8,17 +9,17 @@ public class DataLicense

public string Number { get; set; }

public string IssueDate { get; set; }
public DateTime? IssueDate { get; set; }

public string IssueAuthority { get; set; }

public string SuspendDate { get; set; }
public DateTime? SuspendDate { get; set; }

public string SuspendAuthority { get; set; }

public long? ValidFrom { get; set; }
public DateTime? ValidFrom { get; set; }

public long? ValidTo { get; set; }
public DateTime? ValidTo { get; set; }

public string Type { get; set; }

Expand Down
3 changes: 2 additions & 1 deletion Dadata/Models/Additional/Data/DataOrganization.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

using System;
using System.Collections.Generic;
using DaData.Models.Suggestions.Results;

Expand Down Expand Up @@ -56,7 +57,7 @@ public class DataOrganization

public List<string> Emails { get; set; }

public long? OgrnDate { get; set; }
public DateTime? OgrnDate { get; set; }

public string OkvedType { get; set; }
}
Expand Down
10 changes: 6 additions & 4 deletions Dadata/Models/Additional/Data/DataState.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace DaData.Models.Additional.Data
using System;

namespace DaData.Models.Additional.Data
{
public class DataState
{
public string Status { get; set; }

public long? ActualityDate { get; set; }
public DateTime? ActualityDate { get; set; }

public long? RegistrationDate { get; set; }
public DateTime? RegistrationDate { get; set; }

public long? LiquidationDate { get; set; }
public DateTime? LiquidationDate { get; set; }
}
}
10 changes: 6 additions & 4 deletions Dadata/Models/Suggestions/Data/DataState.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
namespace DaData.Models.Suggestions.Data
using System;

namespace DaData.Models.Suggestions.Data
{
public class DataState
{
public string Status { get; set; }

public string ActualityDate { get; set; }
public DateTime? ActualityDate { get; set; }

public string RegistrationDate { get; set; }
public DateTime? RegistrationDate { get; set; }

public string LiquidationDate { get; set; }
public DateTime? LiquidationDate { get; set; }
}
}
5 changes: 3 additions & 2 deletions Dadata/Models/Suggestions/Data/PartyData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DaData.Models.Suggestions.Results;
using System;
using DaData.Models.Suggestions.Results;

namespace DaData.Models.Suggestions.Data
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public class PartyData

public string Emails { get; set; }

public string OrgnDate { get; set; }
public DateTime? OrgnDate { get; set; }

public string OkvedType { get; set; }
}
Expand Down

0 comments on commit 51a60e7

Please sign in to comment.