datacontractjsonserializer

How do DataContracts work? - Deserialize Json

老子叫甜甜 提交于 2019-12-04 05:43:34
问题 I grabbed an example off of this SO question, and built my own custom Google Maps object used for deserializing the json object. Now the code works like a champ, but I just need an explanation on why/how it works. Does the serializer "try" to match up names, or is something else going on. What exactly is this doing? Here's the working code. Imports System.Net Imports System.Web Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Json Imports System.Web.Script

How to serialize/deserialize a DateTime stored inside an object field using DataContractJsonSerializer?

一曲冷凌霜 提交于 2019-12-04 05:43:13
问题 I use the following class to exchange JSON data over two ASP.NET services : [DataContract] public class Filter { [DataMember] public string Name {get; set;} [DataMember] public FilterOperator Operator {get; set;} [DataMember] public object Value {get; set;} } Here is the problem : if I set a DateTime inside Value , it will be deserialized as string : Value = "/Date(1476174483233+0200)/" This is probably because deserializer has no clue to know what was the type of the value when serialized

DataContractJsonSerializer to skip nodes with null values

こ雲淡風輕ζ 提交于 2019-12-03 23:29:17
问题 I am using DataContractJsonSerializer to serialize my custom object to JSON. But i want to skip the data members whose values are null . If DataMember is null that node should not come in JSON string. How can I achieve this? Give me a simple code snippet to work with. 回答1: You can use the EmitDefaultValue = false property in the [DataMember] attribute. For members marked with that attribute, their values will not be output. [DataContract] public class MyType { [DataMember(EmitDefaultValue =

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

和自甴很熟 提交于 2019-12-03 17:19:56
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 server that if contains an ampersand will response with error, hence here I am. HttpUtility.HtmlEncode is

How do DataContracts work? - Deserialize Json

与世无争的帅哥 提交于 2019-12-02 11:39:52
I grabbed an example off of this SO question , and built my own custom Google Maps object used for deserializing the json object. Now the code works like a champ, but I just need an explanation on why/how it works. Does the serializer "try" to match up names, or is something else going on. What exactly is this doing? Here's the working code. Imports System.Net Imports System.Web Imports System.Runtime.Serialization Imports System.Runtime.Serialization.Json Imports System.Web.Script.Serialization Namespace Utilities.Apis Public NotInheritable Class GoogleGeolocate Private Const googleUrl As

How to serialize/deserialize a DateTime stored inside an object field using DataContractJsonSerializer?

允我心安 提交于 2019-12-02 09:05:33
I use the following class to exchange JSON data over two ASP.NET services : [DataContract] public class Filter { [DataMember] public string Name {get; set;} [DataMember] public FilterOperator Operator {get; set;} [DataMember] public object Value {get; set;} } Here is the problem : if I set a DateTime inside Value , it will be deserialized as string : Value = "/Date(1476174483233+0200)/" This is probably because deserializer has no clue to know what was the type of the value when serialized initially : JSON = {"Value":"\/Date(1476174483233+0200)\/"} As explained here ,

parse.com: SerializationException deserializing JSON objects with “__type” property

隐身守侯 提交于 2019-12-02 08:15:09
问题 I'm developing a Windows 10 UWP app and can't seem to get rid of this error: "An exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.ni.dll but was not handled in user code" I'm using the Rest API to retrieve values from a Data Store on Parse and instantiate objects. Here's what my Class looks like public class ImageTest { public class Image { public string __type { get; set; } public string name { get; set; } public string url { get; set; } } public

parse.com: SerializationException deserializing JSON objects with “__type” property

帅比萌擦擦* 提交于 2019-12-02 03:03:56
I'm developing a Windows 10 UWP app and can't seem to get rid of this error: "An exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.ni.dll but was not handled in user code" I'm using the Rest API to retrieve values from a Data Store on Parse and instantiate objects. Here's what my Class looks like public class ImageTest { public class Image { public string __type { get; set; } public string name { get; set; } public string url { get; set; } } public class Result { public string createdAt { get; set; } public Image image { get; set; } public string name

DataContractJsonSerializer to skip nodes with null values

久未见 提交于 2019-12-01 02:16:41
I am using DataContractJsonSerializer to serialize my custom object to JSON. But i want to skip the data members whose values are null . If DataMember is null that node should not come in JSON string. How can I achieve this? Give me a simple code snippet to work with. You can use the EmitDefaultValue = false property in the [DataMember] attribute. For members marked with that attribute, their values will not be output. [DataContract] public class MyType { [DataMember(EmitDefaultValue = false)] public string Prop1 { get; set; } [DataMember(EmitDefaultValue = false)] public string Prop2 { get;

HL7 FHIR serialisation to json in asp.net web api

吃可爱长大的小学妹 提交于 2019-11-30 21:08:29
I'm using the HL7.Fhir nuget package 0.9.3 created by Ewout Kramer. I am tying it up with ASP.NET Web API, but unfortunately the built in JSON serialization isn't generating the JSON correctly. It contains lots of this: "<IdentifierElement>k__BackingField" As suggested by the framework, this code works... public HttpResponseMessage GetConformance() { var conformance = new Conformance(); var json = FhirSerializer.SerializeResourceToJson(conformance); return new HttpResponseMessage{Content = new StringContent(json)}; } but this will become quite repetitive and isn't following the "by convention"