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

Commit c667dfc

Browse files
committed
Parse objects into primitive types when JsConfig.TryToParsePrimitiveTypeValues=true
1 parent 0cc91a7 commit c667dfc

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public static object ObjectStringToType(string strType)
6767
}
6868
}
6969

70-
return Serializer.UnescapeString(strType);
70+
return (JsConfig.TryToParsePrimitiveTypeValues
71+
? ParsePrimitive(strType)
72+
: null) ?? Serializer.UnescapeString(strType);
7173
}
7274

7375
public static Type ExtractType(string strType)

tests/ServiceStack.Text.Tests/DynamicObjectTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,5 +283,40 @@ public void Can_deserialize_object_array_with_line_breaks_around_number_element(
283283
Assert.That(arrayValues.Count, Is.EqualTo(1));
284284
Assert.That(arrayValues[0], Is.EqualTo(5));
285285
}
286+
287+
class TypeWithObjects
288+
{
289+
public object Value { get; set; }
290+
public Dictionary<string, object> Map { get; set; }
291+
public List<object> List { get; set; }
292+
}
293+
294+
[Test]
295+
public void Does_deserialize_int_objects()
296+
{
297+
JsConfig.TryToParsePrimitiveTypeValues = true;
298+
299+
var dto = new TypeWithObjects
300+
{
301+
Value = 1,
302+
Map = new Dictionary<string, object>
303+
{
304+
{"string", "foo"},
305+
{"int", 1},
306+
},
307+
List = new List<object> { "foo", 1 }
308+
};
309+
310+
var json = dto.ToJson();
311+
Assert.That(json, Is.EqualTo("{\"Value\":1,\"Map\":{\"string\":\"foo\",\"int\":1},\"List\":[\"foo\",1]}"));
312+
313+
314+
var fromJson = json.FromJson<TypeWithObjects>();
315+
Assert.That(fromJson.Value, Is.EqualTo(1));
316+
Assert.That(fromJson.Map["int"], Is.EqualTo(1));
317+
Assert.That(fromJson.List[1], Is.EqualTo(1));
318+
319+
JsConfig.Reset();
320+
}
286321
}
287322
}

0 commit comments

Comments
 (0)