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

Commit fbdc355

Browse files
committed
More win in serialization scenarios wrt curr.cult.
If the object will be serialized via ToString, it will be checked whether the object implements IFormattable and then a CultureInvariant ToString overload is called.
1 parent e22abd6 commit fbdc355

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

src/ServiceStack.Text/Common/ITypeSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal interface ITypeSerializer
2020
void WriteObjectString(TextWriter writer, object value);
2121
void WriteException(TextWriter writer, object value);
2222
void WriteString(TextWriter writer, string value);
23+
void WriteFormattableObjectString(TextWriter writer, object value);
2324
void WriteDateTime(TextWriter writer, object oDateTime);
2425
void WriteNullableDateTime(TextWriter writer, object dateTime);
2526
void WriteDateTimeOffset(TextWriter writer, object oDateTimeOffset);

src/ServiceStack.Text/Common/JsWriter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ public WriteObjectDelegate GetValueTypeToStringMethod(Type type)
323323
? (WriteObjectDelegate)Serializer.WriteEnumFlags
324324
: Serializer.WriteEnum;
325325

326+
if (type.HasInterface(typeof (IFormattable)))
327+
return Serializer.WriteFormattableObjectString;
328+
326329
return Serializer.WriteObjectString;
327330
}
328331

src/ServiceStack.Text/Json/JsonTypeSerializer.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public void WriteObjectString(TextWriter writer, object value)
108108
JsonUtils.WriteString(writer, value != null ? value.ToString() : null);
109109
}
110110

111+
public void WriteFormattableObjectString(TextWriter writer, object value)
112+
{
113+
var formattable = value as IFormattable;
114+
JsonUtils.WriteString(writer, formattable != null ? formattable.ToString(null, CultureInfo.InvariantCulture) : null);
115+
}
116+
111117
public void WriteException(TextWriter writer, object value)
112118
{
113119
WriteString(writer, ((Exception)value).Message);

src/ServiceStack.Text/Jsv/JsvTypeSerializer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ public void WriteString(TextWriter writer, string value)
8888
writer.Write(value.EncodeJsv());
8989
}
9090

91-
public void WriteDateTime(TextWriter writer, object oDateTime)
91+
public void WriteFormattableObjectString(TextWriter writer, object value)
92+
{
93+
var f = (IFormattable)value;
94+
writer.Write(f.ToString(null,CultureInfo.InvariantCulture).EncodeJsv());
95+
}
96+
97+
public void WriteDateTime(TextWriter writer, object oDateTime)
9298
{
9399
writer.Write(DateTimeSerializer.ToShortestXsdDateTimeString((DateTime)oDateTime));
94100
}

0 commit comments

Comments
 (0)