简体   繁体   中英

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?

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

This is a huge deal when sending data over the wire to the web.

In our specific case, JSON.NET stores data as
'Offset': 0
and ServiceStack.Text stores data as
'Offset' : '0'

Why is this very bad? In javascript, 29 + 0 = 29 but 29 + '0' = '290'. This means array[29 + offset] can yield strange results with 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).

It does not store numbers as text, Actual behavior in 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. 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.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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