简体   繁体   English

有没有一种方法可以使ServiceStack.Redis使用JSON.NET而不是ServiceStack.Text?

[英]Is there a way to make ServiceStack.Redis use JSON.NET instead of ServiceStack.Text?

Is there a way to make ServiceStack.Redis use JSON.NET instead of ServiceStack.Text? 有没有一种方法可以使ServiceStack.Redis使用JSON.NET而不是ServiceStack.Text?

The reason I am asking is because of a specific case where integers are converted to strings in ServiceStack.Text but not in JSON.NET 我问的原因是因为在特定情况下,整数在ServiceStack.Text中转换为字符串,而在JSON.NET中却没有

This is a huge deal when sending data over the wire to the web. 通过有线将数据发送到Web时,这是一笔不小的交易。

In our specific case, JSON.NET stores data as 在我们的特定情况下,JSON.NET将数据存储为
'Offset': 0 '偏移':0
and ServiceStack.Text stores data as 和ServiceStack.Text将数据存储为
'Offset' : '0' '偏移':'0'

Why is this very bad? 为什么这很糟糕? In javascript, 29 + 0 = 29 but 29 + '0' = '290'. 在javascript中,29 + 0 = 29,但29 +'0'='290'。 This means array[29 + offset] can yield strange results with ServiceStack.Text 这意味着array [29 + offset]可以在ServiceStack.Text中产生奇怪的结果

I know this is a specific use case, but it'll be a lot easier to use JSON.NET (which behaves as expected) instead of ServiceStack.Text (which is 3 times faster but does not behave as expected). 我知道这是一个特定的用例,但是使用JSON.NET(行为符合预期)而不是ServiceStack.Text(速度快3倍,但行为却不符合预期)会容易得多。

It does not store numbers as text, Actual behavior in ServiceStack.Text: 它不将数字存储为文本,ServiceStack.Text中的实际行为:

public class HasOffset
{
    public int Offset { get; set; }
}

var dto = new HasOffset { Offset = 1 };
string json = dto.ToJson();
json.Print(); //prints {"Offset":1}

var fromJson = json.FromJson<HasOffset>();
Assert.That(fromJson.Offset, Is.EqualTo(1));

If you're trying to deserialize it using JsonObject it gets parsed into a Dictionary<string,string> Dictionary it gets coerced to a string. 如果要使用JsonObject对其进行反序列化,则将其解析为Dictionary<string,string>字典,并将其强制转换为字符串。 Likewise if you're trying to store it into a object the serializer doesn't know what type it should convert it to so it leaves it as a string. 同样,如果您尝试将其存储到object则序列化器不知道应将其转换为哪种类型,因此将其保留为字符串。

At the moment you can only switch between ServiceStack's JsonSerializer and the built-in JsonDataContractSerializer which you can do by adding this to the AppHost.Configure section: 目前,您只能在ServiceStack的JsonSerializer和内置的JsonDataContractSerializer之间切换,可以通过将其添加到AppHost.Configure部分中来实现:

SetConfig(new EndpointHostConfig {
    UseBclJsonSerializers = true
});

Please see this thread for the reference to the above information. 请参阅线程以获取对以上信息的引用。

It would appear that they simply don't support an out-of-box configurable option for different serializers. 看来他们根本不支持针对不同序列化器的开箱即用的可配置选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM