Servicestack.text deserializing an array for an object

I have a REST-Service build with ServicStack, and in one call the user can send different types of values. Therefore, I created a property in an object of type C #.

The sent JSON looks like this:

{"name":"ffff","params":[{"pId":1,"value":[624,625]},{"pId":2,"value":"xxx"}]} 

The value "value": [624,625] leads to a string object filled with "[624,625]". I was hoping to get an int-array or at least a string array, but this is a simple string. I set JsConfig.TryToParsePrimitiveTypeValues ​​= true, but this does not seem to have any effect.

I tried the latest sources from github.

Can this be done with any combination of switches, or should I make it out myself?

thanks

EDIT:

Here are some test codes:

 [TestMethod] public void JsonTest() { string json = "{\"name\":\"ffff\",\"params\":[{\"pId\":1,\"value\":[624,625]},{\"pId\":2,\"value\":\"xxx\"}]}"; var x = JsonSerializer.DeserializeFromString<xy>(json); Assert.AreEqual(x.Params[0].Value.GetType(), typeof(int[])); } public class xy { public string Name { get; set; } public List<Param> Params { get; set; } } public class Param { public int PId { get; set; } public object Value { get; set; } } 
+4
source share
1 answer

If you change the value type to an int array as follows, then the ServiceStack will be serialized to the int array.

  public class Param { public int PId { get; set; } public int[] Value { get; set; } } 

The following unit test passes:

  [TestMethod] public void JsonTest() { string json = "{\"name\":\"ffff\",\"params\":[{\"pId\":1,\"value\":[624,625]},{\"pId\":2,\"value\":\"xxx\"}]}"; var x = JsonSerializer.DeserializeFromString<xy>(json); Assert.AreEqual(x.Params[0].Value.GetType(), typeof(int[])); // Show that we have some integers Assert.IsTrue(x.Params[0].Value.Count()>0); } 

If you cannot change the value type for any reason, you can use ServiceStack.Text to serialize the string into an array as needed.

+1
source

Source: https://habr.com/ru/post/1445722/


All Articles