Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow NewtonsoftJsonSerializer to be parameterized with settings.
This allows custom settings such as turning off auto-magic date parsing. (We need to turn this off for the BigQuery wrapper library.)
- Loading branch information
Showing
3 changed files
with
103 additions
and
31 deletions.
There are no files selected for viewing
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
67 changes: 67 additions & 0 deletions
67
Src/Support/GoogleApis.Tests/Json/NewtonsoftJsonSerializer.cs
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,67 @@ | ||
/* | ||
Copyright 2017 Google Inc | ||
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 Google.Apis.Json; | ||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using System; | ||
|
||
namespace Google.Apis.Tests.Json | ||
{ | ||
class NewtonsoftJsonSerializerTest | ||
{ | ||
[Test] | ||
public void CreateDefaultSettings_Independent() | ||
{ | ||
var settings1 = NewtonsoftJsonSerializer.CreateDefaultSettings(); | ||
var settings2 = NewtonsoftJsonSerializer.CreateDefaultSettings(); | ||
|
||
settings1.DateParseHandling = DateParseHandling.DateTimeOffset; | ||
settings2.DateParseHandling = DateParseHandling.None; | ||
// Not affected by changing settings2 | ||
Assert.AreEqual(DateParseHandling.DateTimeOffset, settings1.DateParseHandling); | ||
} | ||
|
||
[Test] | ||
public void InstanceIsCached() | ||
{ | ||
var instance1 = NewtonsoftJsonSerializer.Instance; | ||
var instance2 = NewtonsoftJsonSerializer.Instance; | ||
Assert.AreSame(instance1, instance2); | ||
} | ||
|
||
[Test] | ||
public void DefaultInstanceParsesDates() | ||
{ | ||
string text = "\"2017-05-03T16:38:00Z\""; | ||
object value = NewtonsoftJsonSerializer.Instance.Deserialize<object>(text); | ||
Assert.IsInstanceOf<DateTime>(value); | ||
} | ||
|
||
[Test] | ||
public void CustomInstanceAvoidingDateParsin() | ||
{ | ||
string text = "\"2017-05-03T16:38:00Z\""; | ||
var settings = NewtonsoftJsonSerializer.CreateDefaultSettings(); | ||
settings.DateParseHandling = DateParseHandling.None; | ||
var instance = new NewtonsoftJsonSerializer(settings); | ||
object value = instance.Deserialize<object>(text); | ||
|
||
// No magic parsing to DateTime... | ||
Assert.IsInstanceOf<string>(value); | ||
} | ||
} | ||
} |