-Added Culture to JsonTextReader to use when converting values from JSON text

This commit is contained in:
JamesNK
2011-08-02 03:41:03 +00:00
parent 29b32aa2cb
commit 76fc1b3081
2 changed files with 114 additions and 1 deletions
@@ -25,6 +25,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Json;
@@ -799,6 +800,78 @@ bye", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
[Test]
public void ReadAsDateTimeOffsetIsoDate()
{
string json = @"{""Offset"":""2011-08-01T21:25Z""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
Assert.AreEqual(JsonToken.Date, reader.TokenType);
Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
Assert.AreEqual(new DateTimeOffset(new DateTime(2011, 8, 1, 21, 25, 0, DateTimeKind.Utc), TimeSpan.Zero), reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
[Test]
public void ReadAsDateTimeOffsetUnitedStatesDate()
{
string json = @"{""Offset"":""1/30/2011""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.Culture = new CultureInfo("en-US");
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
Assert.AreEqual(JsonToken.Date, reader.TokenType);
Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
DateTimeOffset dt = (DateTimeOffset)reader.Value;
Assert.AreEqual(new DateTime(2011, 1, 30, 0, 0, 0, DateTimeKind.Unspecified), dt.DateTime);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
[Test]
public void ReadAsDateTimeOffsetNewZealandDate()
{
string json = @"{""Offset"":""30/1/2011""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.Culture = new CultureInfo("en-NZ");
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
reader.ReadAsDateTimeOffset();
Assert.AreEqual(JsonToken.Date, reader.TokenType);
Assert.AreEqual(typeof(DateTimeOffset), reader.ValueType);
DateTimeOffset dt = (DateTimeOffset)reader.Value;
Assert.AreEqual(new DateTime(2011, 1, 30, 0, 0, 0, DateTimeKind.Unspecified), dt.DateTime);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
#endif
[Test]
@@ -822,5 +895,28 @@ bye", reader.Value);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
[Test]
public void ReadAsDecimalFrench()
{
string json = @"{""decimal"":""9,99""}";
JsonTextReader reader = new JsonTextReader(new StringReader(json));
reader.Culture = new CultureInfo("fr-FR");
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.StartObject, reader.TokenType);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.PropertyName, reader.TokenType);
decimal? d = reader.ReadAsDecimal();
Assert.AreEqual(JsonToken.Float, reader.TokenType);
Assert.AreEqual(typeof(decimal), reader.ValueType);
Assert.AreEqual(9.99m, d);
Assert.IsTrue(reader.Read());
Assert.AreEqual(JsonToken.EndObject, reader.TokenType);
}
}
}
+18 -1
View File
@@ -55,6 +55,16 @@ namespace Newtonsoft.Json
private int _currentLineNumber;
private bool _end;
private ReadType _readType;
private CultureInfo _culture;
/// <summary>
/// Gets or sets the culture used when reading JSON. Defaults to <see cref="CultureInfo.CurrentCulture"/>.
/// </summary>
public CultureInfo Culture
{
get { return _culture ?? CultureInfo.CurrentCulture; }
set { _culture = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="JsonReader"/> class with the specified <see cref="TextReader"/>.
@@ -357,7 +367,7 @@ namespace Newtonsoft.Json
return (decimal?)Value;
decimal d;
if (TokenType == JsonToken.String && decimal.TryParse((string)Value, NumberStyles.Number, CultureInfo.InvariantCulture, out d))
if (TokenType == JsonToken.String && decimal.TryParse((string)Value, NumberStyles.Number, Culture, out d))
{
SetToken(JsonToken.Float, d);
return d;
@@ -386,6 +396,13 @@ namespace Newtonsoft.Json
if (TokenType == JsonToken.Date)
return (DateTimeOffset)Value;
DateTimeOffset dt;
if (TokenType == JsonToken.String && DateTimeOffset.TryParse((string)Value, Culture, DateTimeStyles.None, out dt))
{
SetToken(JsonToken.Date, dt);
return dt;
}
throw CreateJsonReaderException("Unexpected token when reading date: {0}. Line {1}, position {2}.", TokenType, _currentLineNumber, _currentLinePosition);
}
#endif