-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from amphasis/master
New release. Implement DateTime fields support
- Loading branch information
Showing
10 changed files
with
556 additions
and
23 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters