datacontractjsonserializer

Is it possible to use the .NET DataContractJSONSerializer to deserialize a JSON file formatted differently than it would usually serialize?

冷暖自知 提交于 2021-01-29 03:50:38
问题 I have a JSON file formatted like so, which is generated by a tool that I cannot edit: { "thing_name1": { "property1": 0, "property2": "sure" }, "thing_name2": { "property1": 34, "property2": "absolutely" } } The class I'm trying to deserialize to is as such: [DataContract] public class Thing { [DataMember] public String ThingName; [DataMember(Name="property1")] public int Property1; [DataMember(Name="property2")] public String Property2; } I need to put the values of "thing_name1" and "thing

Is it possible to use the .NET DataContractJSONSerializer to deserialize a JSON file formatted differently than it would usually serialize?

╄→尐↘猪︶ㄣ 提交于 2021-01-29 03:42:53
问题 I have a JSON file formatted like so, which is generated by a tool that I cannot edit: { "thing_name1": { "property1": 0, "property2": "sure" }, "thing_name2": { "property1": 34, "property2": "absolutely" } } The class I'm trying to deserialize to is as such: [DataContract] public class Thing { [DataMember] public String ThingName; [DataMember(Name="property1")] public int Property1; [DataMember(Name="property2")] public String Property2; } I need to put the values of "thing_name1" and "thing

Is it possible to use the .NET DataContractJSONSerializer to deserialize a JSON file formatted differently than it would usually serialize?

点点圈 提交于 2021-01-29 03:33:07
问题 I have a JSON file formatted like so, which is generated by a tool that I cannot edit: { "thing_name1": { "property1": 0, "property2": "sure" }, "thing_name2": { "property1": 34, "property2": "absolutely" } } The class I'm trying to deserialize to is as such: [DataContract] public class Thing { [DataMember] public String ThingName; [DataMember(Name="property1")] public int Property1; [DataMember(Name="property2")] public String Property2; } I need to put the values of "thing_name1" and "thing

DataContractJsonSerializer with object type member

陌路散爱 提交于 2020-01-25 07:09:22
问题 I have the following serialization method: private string Serialize(Message message) { byte[] json; using (var ms = new MemoryStream()) { var ser = new DataContractJsonSerializer(typeof(Message)); ser.WriteObject(ms, message); json = ms.ToArray(); } return Encoding.UTF8.GetString(json, 0, json.Length); } I'm trying to serialize the following object: [DataContract] public class Message { [DataMember(Name = "technical", Order = 1)] public Technical Technical; [DataMember(Name = "payload", Order

Deserializing JSON using System.Runtime.Serialization.Json

一个人想着一个人 提交于 2020-01-15 06:53:57
问题 I'm having issues with deserializing some json with C#. Suppose this is a snippet of the json I'm being sent (repeated many times, but nothing else other than id/name): [ { "id":0, "name":"N/A" }, { "id":1, "name":"Annie" }, { "id":2, "name":"Olaf" } ] If the top level was named, I'd do something like [DataContract] public class ChampList { [DataMember(Name = "SOMENAME")] public ElophantChamp[] ElophantChamps { get; set; } } [DataContract] public class ElophantChamp { [DataMember(Name = "id")

How to serialize class that derives from class decorated with DataContract(IsReference=true)?

筅森魡賤 提交于 2020-01-04 05:13:52
问题 I have class A that derives from System.Data.Objects.DataClasses.EntityObject . When I try to serialize using var a = new A(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(a.GetType()); serializer.WriteObject(Response.OutputStream, a); I get error 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

DataContractJsonSerializer generic list containing element type's subtypes

柔情痞子 提交于 2020-01-04 02:53:13
问题 I'm going to use DataContractJsonSerializer for JSON serialization/deserialization. I have two object types in the JSON array and want them both to be deserialized into corresponding object types. Having the following class defnitions [DataContract] public class Post { [DataMember(Name = "content")] public String Content { get; set; } } [DataContract] public class User { [DataMember(Name = "user_name")] public String UserName { get; set; } [DataMember(Name = "email")] public String Email {

Proper way to handle the ampersand character in JSON string send to REST web service

二次信任 提交于 2020-01-01 06:31:26
问题 OK, I am using the System.Runtime.Serialization and the DataContractJsonSerialization . The problem is that in the request I send a value of a property with the & character. Say, AT&T , and I get a response with error: Invalid JSON Data . I thought that the escaping would be done inside the library but now I see that the serialization is left untouched the ampersand & character. Yes, for a JSON format this is valid. But it will be a problem to my POST request since I need to send this to a

Xamarin Using DataContractJsonSerializer for Android Blank App

时间秒杀一切 提交于 2019-12-25 01:59:46
问题 I am trying to use DataContractJsonSerializer but I can't seem to get a reference/namespace for it. I created my app on a .NET Framework 4.5. When I click add reference I only get the version 2.0.5.0 for System.ServiceModel 回答1: If you want to get DataContractJsonSerializer working with Android you would need to create a PCL project and then it would be available under namespace System.Runtime.Serialization.Json. This would require a Xamarin business license or higher to work. Better option

JSON deserialization of variable named parameter using DataContract

北城余情 提交于 2019-12-24 16:19:50
问题 Assuming we have a JSON object similar to: { '12345': 'text string', 'rel': 'myResource' } Constructing a DataContract to map to this type seems fairly simple such as: [DataContract] MyResource { [DataMember(Name = "12345")] public string SpecialValue { get; set; } [DataMember(Name = "rel")] public string Rel { get; set; } } Now the problem arrives that the name of the property is variable so it not guaranteed to be '12345'. Since this variable cannot be properly mapped using attributes it