Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create wrapper around JsonTextReader to ensure consistent date parsing behaviour. #103

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/ArangoDB.Client.Test/Model/DateJson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace ArangoDB.Client.Test.Model
{
public class DateJson
{
public static string SingleDateModelResult => @"
{
'IsoDateAsString':'1983-10-20',
'IsoDateAsDateTime':'1983-10-20',
'IsoDateTimeAsString':'2017-11-16T00:00:00Z',
'IsoDateTimeAsDateTime':'2017-11-16T02:30:15Z',
'IsoDateTimeAsDateTimeOffset':'2017-11-16T01:00:00Z',
'IsoDateTimeWithOffsetAsDateTimeOffset':'2018-02-14T13:43:12+02:30',
'_id':'DateModel/KEY',
'_rev':'REV',
'_key':'KEY'
}
";
}
}

14 changes: 14 additions & 0 deletions src/ArangoDB.Client.Test/Model/DateModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace ArangoDB.Client.Test.Model
{
public class DateModel
{
public string IsoDateAsString { get; set; }
public DateTime IsoDateAsDateTime { get; set; }
public string IsoDateTimeAsString { get; set; }
public DateTime IsoDateTimeAsDateTime { get; set; }
public DateTimeOffset IsoDateTimeAsDateTimeOffset { get; set; }
public DateTimeOffset IsoDateTimeWithOffsetAsDateTimeOffset { get; set; }
}
}
90 changes: 79 additions & 11 deletions src/ArangoDB.Client.Test/Serialization/SimpleDocumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
using ArangoDB.Client.Serialization;
using ArangoDB.Client.Test.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace ArangoDB.Client.Test.Serialization
Expand All @@ -24,26 +20,98 @@ JsonTextReader GenerateReader(string json)
stream.Position = 0;

var streamReader = new StreamReader(stream);
return new JsonTextReader(streamReader);
return new ArangoJsonTextReader(streamReader);
}



[Fact]
public void ParseSingle()
public void ParseSingleResult<T>(string value, Action<JObject, T> assert)
{
using (var reader = GenerateReader(JsonSample.SingleResult))
using (var reader = GenerateReader(value))
{
var documentParser = new DocumentParser(new ArangoDatabase());
JObject jObject = null;
var person = documentParser.ParseSingleResult<Person>(reader, out jObject, true);
var result = documentParser.ParseSingleResult<T>(reader, out jObject, true);

assert(jObject, result);
}
}

[Fact]
public void ParseSingle()
{
ParseSingleResult<Person>(JsonSample.SingleResult, (jObject, person) =>
{
Assert.Equal(27, person.Age);
Assert.Equal("raoof hojat", person.Fullname);
Assert.Equal(172, person.Height);

Assert.True(JObject.DeepEquals(jObject, JObject.Parse(JsonSample.SingleResult)));
}
});
}


[Fact]
public void ParseSingle_IsoDate_ToString()
{
ParseSingleResult<DateModel>(DateJson.SingleDateModelResult, (jObject, result) =>
{
// ISO Date to String
Assert.Equal(JTokenType.String, jObject["IsoDateAsString"].Type);
Assert.Equal("1983-10-20", result.IsoDateAsString);

});
}

[Fact]
public void ParseSingle_IsoDate_ToDateTime()
{
ParseSingleResult<DateModel>(DateJson.SingleDateModelResult, (jObject, result) =>
{
Assert.Equal(JTokenType.String, jObject["IsoDateAsDateTime"].Type);
Assert.Equal(new DateTime(1983, 10, 20), result.IsoDateAsDateTime);

});
}

[Fact]
public void ParseSingle_IsoDateTime_ToString()
{
ParseSingleResult<DateModel>(DateJson.SingleDateModelResult, (jObject, result) =>
{
Assert.Equal(JTokenType.String, jObject["IsoDateTimeAsString"].Type);
Assert.Equal("2017-11-16T00:00:00Z", result.IsoDateTimeAsString);

});
}

[Fact]
public void ParseSingle_IsoDateTimeInUtc_ToDateTimeUtcKind()
{
ParseSingleResult<DateModel>(DateJson.SingleDateModelResult, (jObject, result) =>
{
Assert.Equal(new DateTime(2017, 11, 16, 02, 30, 15), result.IsoDateTimeAsDateTime);
Assert.Equal(DateTimeKind.Utc, result.IsoDateTimeAsDateTime.Kind);

});
}

[Fact]
public void ParseSingle_IsoDateTimeInUtc_ToDateTimeOffset()
{
ParseSingleResult<DateModel>(DateJson.SingleDateModelResult, (jObject, result) =>
{
Assert.Equal(new DateTimeOffset(2017, 11, 16, 01, 00, 00, TimeSpan.Zero), result.IsoDateTimeAsDateTimeOffset);
});
}

[Fact]
public void ParseSingle_IsoDateTimeWithOffset_ToDateTimeOffset()
{
ParseSingleResult<DateModel>(DateJson.SingleDateModelResult, (jObject, result) =>
{
// "2018-02-14T13:43:12+02:30"
Assert.Equal(new DateTimeOffset(2018, 2, 14, 13, 43, 12, new TimeSpan(2,30,0)), result.IsoDateTimeWithOffsetAsDateTimeOffset);
});
}

[Fact]
Expand Down
3 changes: 1 addition & 2 deletions src/ArangoDB.Client/Cursor/CursorAsyncEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ async Task MakeNextRequest()

stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
streamReader = new StreamReader(stream);
jsonTextReader = new JsonTextReader(streamReader);
jsonTextReader.DateParseHandling = DateParseHandling.None;
jsonTextReader = new ArangoJsonTextReader(streamReader);

CursorResult.RequestCount++;
}
Expand Down
13 changes: 13 additions & 0 deletions src/ArangoDB.Client/Serialization/ArangoJsonTextReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.IO;
using Newtonsoft.Json;

namespace ArangoDB.Client.Serialization
{
public class ArangoJsonTextReader : JsonTextReader
{
public ArangoJsonTextReader(TextReader reader) : base(reader)
{
this.DateParseHandling = DateParseHandling.None;
}
}
}
14 changes: 6 additions & 8 deletions src/ArangoDB.Client/Serialization/DocumentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace ArangoDB.Client.Serialization
public class DocumentSerializer
{
IArangoDatabase db;

public DocumentSerializer(IArangoDatabase db)
{
this.db = db;
Expand All @@ -23,11 +24,9 @@ public DocumentSerializer(IArangoDatabase db)
public T Deserialize<T>(Stream stream)
{
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
using (var jsonReader = new ArangoJsonTextReader(streamReader))
{
var serializer = CreateJsonSerializer();

return serializer.Deserialize<T>(jsonReader);
return Deserialize<T>(jsonReader);
}
}

Expand All @@ -41,7 +40,7 @@ public T Deserialize<T>(JsonTextReader reader)
public List<T> DeserializeBatchResult<T>(Stream stream, out BaseResult baseResult)
{
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
using (var jsonReader = new ArangoJsonTextReader(streamReader))
{
var serializer = CreateJsonSerializer();
return new DocumentParser(db).ParseBatchResult<T>(jsonReader, out baseResult);
Expand All @@ -51,7 +50,7 @@ public List<T> DeserializeBatchResult<T>(Stream stream, out BaseResult baseResul
public T DeserializeSingleResult<T>(Stream stream, out BaseResult baseResult)
{
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
using (var jsonReader = new ArangoJsonTextReader(streamReader))
{
var serializer = CreateJsonSerializer();
return new DocumentParser(db).ParseBatchResult<T>(jsonReader, out baseResult).FirstOrDefault();
Expand All @@ -61,9 +60,8 @@ public T DeserializeSingleResult<T>(Stream stream, out BaseResult baseResult)
public T DeserializeSingleResult<T>(Stream stream, out JObject jObject)
{
using (var streamReader = new StreamReader(stream))
using (var jsonReader = new JsonTextReader(streamReader))
using (var jsonReader = new ArangoJsonTextReader(streamReader))
{
var serializer = CreateJsonSerializer();
return new DocumentParser(db).ParseSingleResult<T>(jsonReader, out jObject, true);
}
}
Expand Down