问题标签 [datacontractjsonserializer]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
307 浏览

json - Windows Phone - 后台任务 - 在 DataContractJsonSerializer.WriteObject 处中断

我在 Windows Phone Mango 中使用后台任务。我需要使用 JSON 格式将数据发送到服务器。但是当执行 DataContractJsonSerializer.WriteObject 函数时,此后什么也没有发生。

有没有人对 Windows Phone Mango 中的后台任务有同样的经历?

0 投票
3 回答
3331 浏览

.net - 序列化字典使用 DataContractJsonSerializer 转换为 JSON

我有一个对象树,我用DataContractJsonSerializer. Dictionary<TKey, TValue>被序列化,但我不喜欢标记 - 项目不会像这样呈现:

而是像一个序列化KeyValuePair<TKey, TValue>对象数组:

丑陋,不是吗?

因此,我通过将通用 Dictionary 包装在实现的自定义对象中来避免这种情况ISerializable,并在方法中实现我的自定义序列化GetObjectData(它只需要 3 行)。

现在的问题 - 我不能让我的类派生自Dictionary<TKey, TValue>,所以我在我的自定义类中实现所有逻辑(AddClear等),并应用于私有Dictionary<TKey, TValue>字段。继承会更可取,因为在使用自定义对象时,我将拥有所有通用的 Dictionary 功能。

继承的问题在于它自己Dictionary<TKey, TValue>实现ISerializable,并且DataContractJsonSerializer似乎更喜欢这种实现,即使我ISerializable从我的自定义类中显式实现,如下所示:

实际上,我很惊讶这是可能的,因为它允许我两次实现相同的接口,而显然无法使用显式接口实现 - 所以我在一篇关于多接口实现的博客文章中更详细地分析了这种情况

所以,根据我在那里做的实验,序列化程序应该调用我的 ISerializable 实现,无论内部使用什么类型的转换 -

或者:

但它显然没有发生,因为我在生成的 JSON 中看到KeyValuePair<TKey, TValue>仍然调用序列化程序。我错过了什么可能会发生什么?

更新:到目前为止,我得到的答案和评论几乎只是建议解决方法。但是,我注意到我有一个非常有效的解决方法,所以在提出这个问题时我有两个目标:

  1. 最终让它与原始设计一起工作——我不会仅仅为此改变序列化逻辑,有很多代码和逻辑依赖于它

  2. 为了解开为什么不DataContractJsonSerializer使用我的序列化代码的奥秘 - 正如我提到的博客文章中所见,我已经对接口实现和继承进行了各种实验,并且我确信我掌握了所有内容和过程之外,所以我对无法理解在这种情况下发生的事情感到困扰

0 投票
1 回答
2529 浏览

c# - XmlSerializer 保存临时文件的最安全位置

我注意到 XmlSerializer 需要使用磁盘空间来进行投标。如果没有可写的 %temp% 文件夹,则会失败并显示如下错误:

Source : System.Xml Message : Unable to generate a temporary class (result=1). error CS2001: Source file 'C:\Windows\TEMP\c1ls4elp.0.cs' could not be found error CS2008: No inputs specified StackTrace : at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence) at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies) at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace) at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace) at StreamLib.Tuna.SerializationHelper.Deserialize[T](String presetsString) ...

作为参考,实现StreamLib.Tuna.SerializationHelper.Deserialize[T]看起来如下:

更改文件夹的权限是我认为最好留给用户的事情,而不是为狡猾的序列化程序提供补丁,所以我想通过在其他地方给序列​​化程序写它的废话来解决这个问题。这可以通过添加以下内容来实现app.config/web.config

我的问题是,此设置是否有防弹位置使用,在某些客户端计算机上不会失败?如果没有,我的替代方案是什么?是否DataContractJsonSerializer也以相同的方式需要磁盘空间?

0 投票
2 回答
11074 浏览

c# - JSON 样本到 [DataContract]

是否有任何工具允许基于 JSON 样本生成 DataContract 实体定义?我真的很怀念带有代码生成工具的旧的良好 WSDL 元数据。

0 投票
1 回答
1247 浏览

json - 使用未知字段反序列化 JSON 对象

这个问题类似于用未知字段反序列化 JSON,但我想改用内置的 DataContractJsonSerializer。

所以我有这样的JSON数据:

我以为我可以像这样执行数据合同:

而 TwoStringMembersClass 就是这样:

但是似乎在 JSON.Net 中有效的方法似乎不适用于本机 JSON 解析器。在 ReadObject() 中,我得到一个 ArgumentException,可能是因为字典。

知道如何使这项工作的最佳解决方案是什么?

提前致谢。

0 投票
1 回答
560 浏览

json - Silverlight 4 DataContractJsonSerializer,派生类的私有字段

我使用 DataContractJsonSerializer 在 Silverlight 4 中反序列化 json 数据。Json 数据键名与我的类属性名不匹配;所以我想我必须使用 DataMemberAttribute。所以我做了以下事情:

现在反序列化失败了,因为我没有将 DataContractAttribute 应用于 Person 的基类 Model。要求很严格吗?此外,在我将 DataContractAttribute 应用于 Model 之后,反序列化再次失败,因为我将 DataMember 属性应用于私有字段,而不是公共属性。为什么我不能将它们应用于私人成员(文档似乎另有说明)。

注意:服务器端代码不是 ASP.NET;所以不使用WCF。

0 投票
2 回答
1423 浏览

c#-4.0 - 如何序列化派生自用 DataContract(IsReference=true) 装饰的类的类?

我有class ASystem.Data.Objects.DataClasses.EntityObject. 当我尝试使用序列化

我收到错误

TestController+A._Id' is not marked with OptionalFieldAttribute, thus indicating that it must be serialized. However, 'TestController+A' derives from a class marked with DataContractAttribute and an IsReference setting of 'True'. It is not possible to have required data members on IsReference classes. Either decorate 'TestController+A._Id' with OptionalFieldAttribute, or disable the IsReference setting on the appropriate parent class.

即使我用OptionalFieldAttribute我来装饰场地

The type 'TestController+A' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.

我无法修改EntityObject课程。我想A_Bag完全按照Aclass 创建 class 并填充它并序列化它而不是A,但我认为有更优雅的方法来做到这一点。

你能建议我怎么做吗?

0 投票
3 回答
6946 浏览

c# - 在 C# 中解析 JSON 键值对

参考Parse JSON in C#

我正在尝试在 C# 中解析以下JSON 提要,但在访问“rates”中的数据时遇到问题。我试图将其反序列化为 List> 或 Dictionary 以及其他各种类型,但我总是得到 0 个结果。我设法让它工作的唯一方法是创建一个自定义类型并将我需要的所有货币作为属性——这非常好。

这是我目前的数据合同:

我希望能够用某种集合替换 RatesObj。

有任何想法吗?

0 投票
2 回答
2584 浏览

arrays - 使用 DataContractJsonSerializer WP7 将数组解析为 Json 字符串

如何使用 DataContractJsonSerializer 解析 Json 字符串中的数组元素?语法是:

0 投票
4 回答
25818 浏览

c# - DataContractJsonSerializer - 反序列化列表中的 DateTime

I'm having trouble using the System.Runtime

I'm having trouble using the System.Runtime.Serialization.Json.DataContractJsonSerializer class to deserialize DateTime instances contained within a List<object>. I cannot seem to get DateTime to deserialize back into the original type. The DataContractJsonSerializer always deserializes it into a string type with the format "/Date(1329159196126-0500)/". It'll serialize and deserialize fine if I run it through using a strongly typed List<DateTime>, however I am looking for way to get the serializer to identify and properly deserialize DateTimes when encountered within a simple list or array of object.

Note that DateTimes are the only type besides primitives and strings that this list will ever contain. Here is the code snippet I'm using to test this.


If DataContractJsonSerializer isn't a must, here is a solution using Json.Net.

This is the Json string

and returned types are long,string,double,bool and DateTime