ServiceStack.Text.JsonSerializer.DeserializeFromString<T>() fails to deserialize if string contains \n's

混江龙づ霸主 提交于 2019-12-11 10:55:56

问题


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.



来源:https://stackoverflow.com/questions/12765072/servicestack-text-jsonserializer-deserializefromstringt-fails-to-deserialize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!