如何让 ServiceStack 序列化/反序列化具有正确类型的 expando 对象 [英] How to get ServiceStack to serialize / deserialize an expando object with correct types

查看:38
本文介绍了如何让 ServiceStack 序列化/反序列化具有正确类型的 expando 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想弄清楚 servicestack.text 支持将 expando 对象序列化为 json 和从 json 序列化的效果如何.我知道一个 expando 对象实现了一个 IDictionary.当我从 json 序列化和从 json 序列化时,我无法在反序列化的 IDictionary 中获取正确的类型.由于 Json 本身不支持类型,并且 servicestack 有一个名为 JsConfig.IncludeTypeInfo 的设置,我希望它在序列化的 json 中包含类型信息,以使服务堆栈能够反序列化为另一侧的正确类型(例如,没有小数位的小数反序列化为 uint64).

just trying to work out how well servicestack.text supports serializing expando objects to and from json. I know that an expando object implements an IDictionary. When I serialize to and from json I am having trouble getting the correct types in the deserialized IDictionary. As Json does not support types natively and servicestack has a setting called JsConfig.IncludeTypeInfo I expected it to include the type information in the serialized json to enable service stack to deserialize to the correct types on the other side, (eg a decimal without decimal places is deserialized to a uint64).

无论如何要强制 servicestack 使用 expando 对象正确反序列化与源相同的类型?

Is there anyway to force servicestack to correctly deserialize the same types as the source using an expando object ?

Ps:我不想使用 poco 对象来实现这一点,因为我直到运行时才知道对象的属性.

Ps: I do not want to use a poco object to achieve this as I do not know the properties of the object until runtime.

下面是一个快速测试,展示了我的意思.

Below is a quick test showing what I mean.

谢谢

/// <summary>
/// Test servicestack serialisation
/// I was expecting that IncludeTypeInfo=true would always add the type info
/// so when you deserialise into a IDictionary<string,object> servicerstack
/// would have enough information to convert to the expected type
/// </summary>
[Test]
public void TestDynamicSerialization()
{
    JsConfig.Reset();
    JsConfig.IncludeNullValues = true;
    JsConfig.IncludeTypeInfo = true;
    JsConfig.EmitCamelCaseNames = false;
    JsConfig.ConvertObjectTypesIntoStringDictionary = true;
    JsConfig.PropertyConvention = JsonPropertyConvention.Lenient;
    JsConfig.TryToParsePrimitiveTypeValues = true;

    // create an expando object
    dynamic obj = new ExpandoObject();

    // cast as a idictionary and set two decimals, one with decimnal places and one without
    var objDict = (IDictionary<string, object>)obj;
    objDict["decimal1"] = 12345.222M;
    objDict["decimal2"] = 12345M;

    Assert.AreEqual(typeof(decimal), objDict["decimal1"].GetType());
    Assert.AreEqual(typeof(decimal), objDict["decimal2"].GetType());

    // serialise to json
    var json = JsonSerializer.SerializeToString(obj);

    //deserialise to a a IDictionary<string,object>
    var deserialisedDict = JsonSerializer.DeserializeFromString<IDictionary<string, object>>(json);

    // make sure we got the expected types
    Assert.AreEqual(typeof(decimal), deserialisedDict["decimal1"].GetType());
    Assert.AreEqual(typeof(decimal), deserialisedDict["decimal2"].GetType(), "Fails because type is UInt64 expected decimal");


}

推荐答案

应从 https://github.com/ServiceStack/ServiceStack.Text/pull/347 默认情况下会返回小数,除非您指定 TryToParseNumericType,在这种情况下,您将获得最合适的数字类型.

Should be fixed as of https://github.com/ServiceStack/ServiceStack.Text/pull/347 decimals will come back by default unless you specify TryToParseNumericType in which case you'll get the best fitting number type.

这篇关于如何让 ServiceStack 序列化/反序列化具有正确类型的 expando 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆