ServiceStack.Text.JsonSerializer.DeserializeFromSt

2019-08-05 23:52发布

问题:

Trying: T obj = JsonSerializer.DeserializeFromString<T>(jsonData);

on a string that has several \n's throughout it. JayRock's library successfully deserializes this like: T obj = (T)JsonConvert.Import(typeof(T), jsonData);

Is this a bug, or do I need to manually strip out newlines?

回答1:

The problem I ran into wasn't the \n's, but instead was the lack of public properties on my DTO's.

RE: Can ServiceStack.Text deserialize JSON to a custom generic type?



回答2:

The debugger preview popup shows actual linebreaks as \n so that the preview remains single line. The text visualizer shows linebreaks correctly.

This implies to me that the JSON itself is broken, because newlines should be encoded with \n.

Linebreaks in strings are illegal in Javascript, and thus also in JSON.

If this doesn't happen to the the issue: the nuget version was published 1st Oct, but there's a commit in github dated 3rd Oct with comment "fix whitespace issues hopefully once and for all". Worth trying.