如何让 ServiceStack 序列化/反序列化具有正确类型的 expando 对象

编程入门 行业动态 更新时间:2024-10-10 21:32:01
本文介绍了如何让 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"); }

推荐答案

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

Should be fixed as of github/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 对象

本文发布于:2023-11-12 10:05:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   对象   正确   类型   ServiceStack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!