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

Commit 1e2c2ad

Browse files
committed
Fix ConvertObjectTypesIntoStringDictionary when value starts with obj literal
1 parent aaebd60 commit 1e2c2ad

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public static object ObjectStringToType(ReadOnlySpan<char> strType)
7474

7575
if (config.ConvertObjectTypesIntoStringDictionary && !strType.IsNullOrEmpty())
7676
{
77-
if (strType[0] == JsWriter.MapStartChar)
77+
var firstChar = strType[0];
78+
var endChar = strType[strType.Length - 1];
79+
if (firstChar == JsWriter.MapStartChar && endChar == JsWriter.MapEndChar)
7880
{
7981
var dynamicMatch = DeserializeDictionary<TSerializer>.ParseDictionary<string, object>(strType, null, v => Serializer.UnescapeString(v).ToString(), v => Serializer.UnescapeString(v).ToString());
8082
if (dynamicMatch != null && dynamicMatch.Count > 0)
@@ -83,7 +85,7 @@ public static object ObjectStringToType(ReadOnlySpan<char> strType)
8385
}
8486
}
8587

86-
if (strType[0] == JsWriter.ListStartChar)
88+
if (firstChar == JsWriter.ListStartChar && endChar == JsWriter.ListEndChar)
8789
{
8890
return DeserializeList<List<object>, TSerializer>.ParseStringSpan(strType);
8991
}

tests/ServiceStack.Text.Tests/JsonTests/AnonymousDeserializationTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,18 @@ public void Deserialize_dynamic_json_with_inner_obj_and_array()
7777
var phone2 = dyn.obj.phones[1].number;
7878
Assert.AreEqual(phone2, "39967");
7979
}
80+
81+
[Test]
82+
public void Deserialize_dynamic_json_with_keys_starting_with_object_literal()
83+
{
84+
using (JsConfig.With(new Config {ConvertObjectTypesIntoStringDictionary = true}))
85+
{
86+
var json = @"{""prop1"": ""value1"", ""prop2"": ""{tag} value2"", ""prop3"": { ""A"" : 1 } }";
87+
var obj = json.FromJson<Dictionary<string, object>>();
88+
Assert.That(obj["prop1"], Is.EqualTo("value1"));
89+
Assert.That(obj["prop2"], Is.EqualTo("{tag} value2"));
90+
Assert.That(obj["prop3"], Is.EqualTo(new Dictionary<string,object> { ["A"] = "1" }));
91+
}
92+
}
8093
}
8194
}

0 commit comments

Comments
 (0)