Skip to content

Commit a9c91b0

Browse files
committed
ensure dynamic objects write out type info and use it to deserialize.
1 parent 5e5e0bf commit a9c91b0

12 files changed

Lines changed: 299 additions & 234 deletions

File tree

NuGet/lib/net35/ServiceStack.Text.XML

Lines changed: 154 additions & 154 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 Bytes
Binary file not shown.

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,42 @@ private static object StringToType(Type type, string strType,
9595

9696
if (instance == null) instance = ctorFn();
9797

98+
if (propertyValueString != null
99+
&& propertyValueString.Length >= JsWriter.TypeAttrInObject.Length
100+
&& propertyValueString.Substring(0, JsWriter.TypeAttrInObject.Length) == JsWriter.TypeAttrInObject)
101+
{
102+
var propIndex = JsWriter.TypeAttrInObject.Length;
103+
var typeName = Serializer.EatValue(propertyValueString, ref propIndex);
104+
typeName = typeName.Substring(1, typeName.Length - 2); //remove quotes
105+
var propType = AssemblyUtils.FindType(typeName);
106+
if (propType == null)
107+
{
108+
Tracer.Instance.WriteWarning("Could not find type: " + typeName);
109+
}
110+
else
111+
{
112+
try
113+
{
114+
var parseFn = Serializer.GetParseFn(propType);
115+
var propertyValue = parseFn(propertyValueString);
116+
117+
setterMap.TryGetValue(propertyName, out setterFn);
118+
119+
if (setterFn != null)
120+
{
121+
setterFn(instance, propertyValue);
122+
}
123+
}
124+
catch (Exception)
125+
{
126+
Tracer.Instance.WriteWarning("WARN: failed to set dynamic property {0} with: {1}", propertyName, propertyValueString);
127+
}
128+
}
129+
130+
Serializer.EatItemSeperatorOrMapEndChar(strType, ref index);
131+
continue;
132+
}
133+
98134
parseStringFnMap.TryGetValue(propertyName, out parseStringFn);
99135

100136
if (parseStringFn != null)
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System;
2-
3-
namespace ServiceStack.Text.Common
4-
{
5-
internal static class JsState
6-
{
7-
//Exposing field for perf
8-
[ThreadStatic]
9-
public static int WritingKeyCount = 0;
10-
11-
[ThreadStatic]
12-
public static bool IsWritingValue = false;
13-
}
1+
using System;
2+
3+
namespace ServiceStack.Text.Common
4+
{
5+
internal static class JsState
6+
{
7+
//Exposing field for perf
8+
[ThreadStatic] internal static int WritingKeyCount = 0;
9+
10+
[ThreadStatic] internal static bool IsWritingValue = false;
11+
12+
[ThreadStatic] internal static bool IsWritingDynamic = false;
13+
}
1414
}

src/ServiceStack.Text/Common/JsWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace ServiceStack.Text.Common
1111
public static class JsWriter
1212
{
1313
public const string TypeAttr = "__type";
14+
public const string TypeAttrInObject = "{\"__type\":";
1415

1516
public const char MapStartChar = '{';
1617
public const char MapKeySeperator = ':';
@@ -275,7 +276,7 @@ public WriteObjectDelegate GetSpecialWriteFn(Type type)
275276

276277
public void WriteType(TextWriter writer, object value)
277278
{
278-
Serializer.WriteRawString(writer, AssemblyUtils.ToTypeString((Type)value));
279+
Serializer.WriteRawString(writer, ((Type)value).ToTypeString());
279280
}
280281

281282
}

src/ServiceStack.Text/Common/WriteType.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public static void WriteProperties(TextWriter writer, object value)
116116
{
117117
WriteTypeInfo(writer, value);
118118
}
119+
else if (JsState.IsWritingDynamic)
120+
{
121+
TypeInfoWriter(writer, value);
122+
}
119123

120124
if (PropertyWriters != null)
121125
{

src/ServiceStack.Text/Env.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static Env()
2323
+ (IsMonoTouch ? " MonoTouch" : "");
2424
}
2525

26-
public static decimal ServiceStackVersion = 2.27m;
26+
public static decimal ServiceStackVersion = 2.28m;
2727

2828
public static bool IsUnix { get; set; }
2929

src/ServiceStack.Text/Json/JsonWriter.Generic.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public static void WriteLateBoundObject(TextWriter writer, object value)
5757
{
5858
if (value == null) return;
5959
var writeFn = GetWriteFn(value.GetType());
60+
61+
var prevState = JsState.IsWritingDynamic;
62+
JsState.IsWritingDynamic = true;
6063
writeFn(writer, value);
64+
JsState.IsWritingDynamic = prevState;
6165
}
6266

6367
public static WriteObjectDelegate GetValueTypeToStringMethod(Type type)

src/ServiceStack.Text/Jsv/JsvWriter.Generic.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public static void WriteLateBoundObject(TextWriter writer, object value)
5757
{
5858
if (value == null) return;
5959
var writeFn = GetWriteFn(value.GetType());
60+
61+
var prevState = JsState.IsWritingDynamic;
62+
JsState.IsWritingDynamic = true;
6063
writeFn(writer, value);
64+
JsState.IsWritingDynamic = prevState;
6165
}
6266

6367
public static WriteObjectDelegate GetValueTypeToStringMethod(Type type)

src/ServiceStack.Text/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("2.2.6.*")]
35+
[assembly: AssemblyVersion("2.2.8.*")]
3636
//[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)