【问题标题】:Alter output of ServiceStack.Text JSON Deserializer更改 ServiceStack.Text JSON 反序列化器的输出
【发布时间】:2016-12-19 01:32:25
【问题描述】:

我目前正在使用Newtonsoft.json nuget 包,但我想找到一个更快的替代方案。 ServiceStack.Text 似乎可以解析它,但它以我期望的不同格式返回 JSON。无论如何要更改返回对象的格式以匹配我的期望吗?

问题是在反序列化后,response.fullName 会返回 Newtonsoft.json 预期的“joey cool”,但 ServiceStack.Text 将返回 null,因为格式不同。

我可以更改ServiceStack.Text 的输出格式以使其符合我的预期吗?我想打电话给response.fullName 并得到“乔伊酷”。

这是我使用 ServiceStack.Text 组件的代码:

T response = a_response.Content.FromJson<T>();

这是我使用 Newtonsoft.json 的代码:

T response = JsonConvert.DeserializeObject<T>(a_response.Content);

以下是文本中的 JSON 作为输出的样子:

{ "userId": "fc7b4c4e0b6660c7daf711b1d17e0039", "emailAddress": "joey+100@stringify.com", "fullName": "乔伊酷", "accountType": "个人", “创建日期”:1440104822411, "电话号码": "15555555555", "timezone": "America/Los_Angeles", "照片": "https://stringify.s3.amazonaws.com/users/fc7b4c4e0b6660c7daf711b1d17e0039-profile.jpg", “名称”:“默认”, “类型”:“API” }

下面是调试器如何显示Newtonsoft.json 对象:

下面是您的ServiceStack.Text JSON Deserializer 如何显示响应对象:

----编辑---- 从 NuGet 尝试了 4.0.62,它给了我一个例外。

消息:“ServiceStack.StringExtensions”的类型初始化程序引发了异常。对象引用未设置为对象实例,位于 ServiceStack.StringExtensions..cctor () [0x00017] in :0

-----编辑-----

URL to a file containing the JSON class

Here's a video demonstrating the usage differences and the strange output

【问题讨论】:

    标签: c# .net json xamarin servicestack-text


    【解决方案1】:

    您可以尝试使用最新v4的ServiceStack.Text which is now free from v4.0.62+available in all Xamarin platforms

    自 v3 以来,它已经添加了很多改进,因此如果此行为是 v3 中的错误导致的,现在很可能已修复。

    编辑:

    您尝试序列化的此类无效,ServiceStack 使用公共属性序列化字典或 POCO 类,它不会同时执行这两种操作,例如:

    [JsonObject (MemberSerialization.OptIn)]
    public class UserDataDict : Dictionary<string, object>
    {
        [JsonProperty]
        public string userID { get; set; }
        [JsonProperty]
        public string emailAddress { get; set; }
        [JsonProperty]
        public string fullName { get; set; }
        [JsonProperty]
        public string accountType { get; set; }
        [JsonProperty]
        public string units { get; set; }
        [JsonProperty]
        public string unitsDistance { get; set; }
        [JsonProperty]
        public string newsletterSub { get; set; }
    
        [JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
        public string location { get; set; }
        [JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
        public string phoneNumber { get; set; }
        [JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
        public string address { get; set; }
        [JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
        public string photo { get; set; }
        [JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
        public string createdDate { get; set; }
        [JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
        public string verifyURL { get; set; }
        [JsonProperty (NullValueHandling = NullValueHandling.Ignore)]
        public string timezone { get; set; }
        [JsonProperty (NullValueHandling = NullValueHandling.Include)]
        public APIManifestDict apiManifest { get; set; }
    }
    

    我会删除继承字典,因为让您的类包含一个字典然后从一个字典继承更具互操作性。此外,您的 [JsonProperty] 属性是 JSON.NET 特定的,在其他序列化程序中无效,因此我将您的类重写为:

    public class UserData
    {
        public string userID { get; set; }
        public string emailAddress { get; set; }
        public string fullName { get; set; }
        public string accountType { get; set; }
        public string units { get; set; }
        public string unitsDistance { get; set; }
        public string newsletterSub { get; set; }
    
        public string location { get; set; }
        public string phoneNumber { get; set; }
        public string address { get; set; }
        public string photo { get; set; }
        public string createdDate { get; set; }
        public string verifyURL { get; set; }
        public string timezone { get; set; }
        public APIManifestDict apiManifest { get; set; }
    
        public Dictionary<string,string> Metadata { get; set; }
    }
    

    如果你想包含空值,你可以指定它:

    JsConfig.IncludeNullValues = true;
    

    但我建议您的应用不要依赖空值的存在,因为如果它们不包含在 JSON 有效负载中,您的属性自然为空。包含空值更脆弱,必须满足空的多种定义,抑制版本控制并且只会使有效负载膨胀。

    【讨论】:

    • 当我尝试从 NuGet 使用 4.0.62 时,我得到一个异常。是 Xamarin 的问题吗?我附上了上述异常的更多细节。消息:“ServiceStack.StringExtensions”的类型初始化程序引发了异常。对象引用未设置为 :0 中 ServiceStack.StringExtensions..cctor () [0x00017] 处的对象实例
    • 如果你还没有,你需要initialize the client
    • 所以我得到了那个工作,不得不使用 ServiceStack.Client 而不是默认的。但我仍然有同样的问题。 JSON 显示不正确......它以与 Xamarin 组件相同的损坏格式输出。
    • @LampShade 您能否发布您要反序列化的 C# 类定义以及您用于反序列化的 C# 代码?
    • 我发布了一个文件,其中包含您需要的所有内容。还有一个视频演示它。我相信他们的关键在于 Newtonsoft 如何使用 [JsonObject (MemberSerialization.OptIn)] 来告诉它如何解析 JSON...还没有得到与 ServiceStack 一起工作的等价物
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 2012-04-26
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多