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

Commit 5c0ac5f

Browse files
committed
Support white-space when parsing __type info
1 parent b82ac1e commit 5c0ac5f

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,17 @@ public static object ObjectStringToType(string strType)
7373
public static Type ExtractType(string strType)
7474
{
7575
var typeAttrInObject = Serializer.TypeAttrInObject;
76-
if (strType != null
77-
&& strType.Length > typeAttrInObject.Length
76+
if (strType == null || strType.Length <= 1) return null;
77+
78+
var hasWhitespace = JsonUtils.WhiteSpaceChars.Contains(strType[1]);
79+
if (hasWhitespace)
80+
{
81+
var pos = strType.IndexOf('"');
82+
if (pos >= 0)
83+
strType = "{" + strType.Substring(pos);
84+
}
85+
86+
if (strType.Length > typeAttrInObject.Length
7887
&& strType.Substring(0, typeAttrInObject.Length) == typeAttrInObject)
7988
{
8089
var propIndex = typeAttrInObject.Length;

src/ServiceStack.Text/Json/JsonTypeSerializer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ internal static string GetTypeAttrInObject(string typeAttr)
3333

3434
static JsonTypeSerializer()
3535
{
36-
WhiteSpaceFlags[' '] = true;
37-
WhiteSpaceFlags['\t'] = true;
38-
WhiteSpaceFlags['\r'] = true;
39-
WhiteSpaceFlags['\n'] = true;
36+
foreach (var c in JsonUtils.WhiteSpaceChars)
37+
{
38+
WhiteSpaceFlags[c] = true;
39+
}
4040
}
4141

4242
public WriteObjectDelegate GetWriteFn<T>()

src/ServiceStack.Text/Json/JsonUtils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public static class JsonUtils
1414
public const string True = "true";
1515
public const string False = "false";
1616

17+
public static char[] WhiteSpaceChars = new[] { ' ', '\t', '\r', '\n' };
18+
1719
static readonly char[] EscapeChars = new[]
1820
{
1921
QuoteChar, '\n', '\r', '\t', '"', '\\', '\f', '\b',
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
5+
namespace ServiceStack.Text.Tests.JsonTests
6+
{
7+
public class TypeInfoTests
8+
{
9+
class MyClass : IComparable
10+
{
11+
public int CompareTo(object obj)
12+
{
13+
return 0;
14+
}
15+
}
16+
17+
[Test]
18+
[TestCase("[{'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
19+
[TestCase("[{ '__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
20+
[TestCase("[{\n'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
21+
[TestCase("[{\t'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
22+
[TestCase("[ {'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
23+
[TestCase("[\n{'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
24+
[TestCase("[\t{'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
25+
[TestCase("[ { '__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
26+
[TestCase("[\n{\n'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
27+
[TestCase("[\t{\t'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
28+
[TestCase("[{'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'} ]")]
29+
[TestCase("[{ '__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}\t]")]
30+
[TestCase("[{\n'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}\n]")]
31+
[TestCase("[{\t'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass' }]")]
32+
[TestCase("[ {'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'\t}]")]
33+
[TestCase("[\n{'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'\n}]")]
34+
[TestCase("[\t{'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'\t}\n]")]
35+
[TestCase("[ { '__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass', }]")]
36+
[TestCase("[\n{\n'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
37+
[TestCase("[\t{\t'__type':'ServiceStack.Text.Tests.JsonTests.TypeInfoTests+MyClass'}]")]
38+
public void TypeAttrInObject(string json)
39+
{
40+
json = json.Replace('\'', '"');
41+
var deserDto = JsonSerializer.DeserializeFromString<List<IComparable>>(json);
42+
Console.WriteLine(json);
43+
Assert.IsNotNull(deserDto);
44+
Assert.AreEqual(1, deserDto.Count);
45+
Assert.IsNotNull(deserDto[0]);
46+
}
47+
48+
}
49+
}

tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
<ItemGroup>
179179
<Compile Include="AttributeTests.cs" />
180180
<Compile Include="JsonTests\InvalidJsonTests.cs" />
181+
<Compile Include="JsonTests\TypeInfoTests.cs" />
181182
<Compile Include="SerializationDelegatePerformanceTests.cs" />
182183
<Compile Include="SerializationHookTests.cs" />
183184
<Compile Include="StaticAccessorTests.cs" />

0 commit comments

Comments
 (0)