Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Culture issue #357

Merged
merged 2 commits into from
Jul 23, 2013
Merged
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
1 change: 1 addition & 0 deletions src/ServiceStack.Text/Common/ITypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal interface ITypeSerializer
void WriteObjectString(TextWriter writer, object value);
void WriteException(TextWriter writer, object value);
void WriteString(TextWriter writer, string value);
void WriteFormattableObjectString(TextWriter writer, object value);
void WriteDateTime(TextWriter writer, object oDateTime);
void WriteNullableDateTime(TextWriter writer, object dateTime);
void WriteDateTimeOffset(TextWriter writer, object oDateTimeOffset);
Expand Down
3 changes: 3 additions & 0 deletions src/ServiceStack.Text/Common/JsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ public WriteObjectDelegate GetValueTypeToStringMethod(Type type)
? (WriteObjectDelegate)Serializer.WriteEnumFlags
: Serializer.WriteEnum;

if (type.HasInterface(typeof (IFormattable)))
return Serializer.WriteFormattableObjectString;

return Serializer.WriteObjectString;
}

Expand Down
6 changes: 6 additions & 0 deletions src/ServiceStack.Text/Json/JsonTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ public void WriteObjectString(TextWriter writer, object value)
JsonUtils.WriteString(writer, value != null ? value.ToString() : null);
}

public void WriteFormattableObjectString(TextWriter writer, object value)
{
var formattable = value as IFormattable;
JsonUtils.WriteString(writer, formattable != null ? formattable.ToString(null, CultureInfo.InvariantCulture) : null);
}

public void WriteException(TextWriter writer, object value)
{
WriteString(writer, ((Exception)value).Message);
Expand Down
8 changes: 7 additions & 1 deletion src/ServiceStack.Text/Jsv/JsvTypeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ public void WriteString(TextWriter writer, string value)
writer.Write(value.EncodeJsv());
}

public void WriteDateTime(TextWriter writer, object oDateTime)
public void WriteFormattableObjectString(TextWriter writer, object value)
{
var f = (IFormattable)value;
writer.Write(f.ToString(null,CultureInfo.InvariantCulture).EncodeJsv());
}

public void WriteDateTime(TextWriter writer, object oDateTime)
{
writer.Write(DateTimeSerializer.ToShortestXsdDateTimeString((DateTime)oDateTime));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\ServiceStack.Text.Tests\AdhocModelTests.cs">
Expand Down
21 changes: 21 additions & 0 deletions tests/ServiceStack.Text.Tests/StructTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Windows;
using NUnit.Framework;
using ServiceStack.Text.Common;

Expand Down Expand Up @@ -253,6 +257,23 @@ public void StaticParseMethod_will_not_throw_on_unstandard_usage()
Assert.DoesNotThrow(() => ret = StaticParseMethod<DangerousText2>.Parse);
Assert.IsNull(ret);
}

[Test]
[TestCase("en")]
[TestCase("en-US")]
[TestCase("de-CH")]
[TestCase("de")]
public void test_rect_different_cultures(string culture)
{
var currentCulture = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentCulture = currentCulture;
Thread.CurrentThread.CurrentUICulture = currentCulture;
var s = new JsonSerializer<Rect>();
var r = new Rect(23, 34, 1024, 768);
var interim = s.SerializeToString(r);
var r2 = s.DeserializeFromString(interim);
Assert.AreEqual(r, r2);
}
}

public struct UserStruct
Expand Down