datacontractjsonserializer

Constructors Not Called On Deserialization

爱⌒轻易说出口 提交于 2019-12-24 05:35:30
问题 Various places I've been reading have pointed out that on deserialization, the .NET Framework makes a call to FormatterServices.GetUninitializedObject, in which constructors are not called and field initializers are not set. If this is true, why is my constructor being called? Are there instances where constructors and field initializers could be called? My Class: [DataContract] public class TestClass { [DataMember] public string Val1 { get; set; } [DataMember] public string Val2 { get; set;

Is it possible to use JsonReaderWriterFactory to convert XML to JSON without using DataContractJsonSerializer?

假装没事ソ 提交于 2019-12-24 02:18:41
问题 I need a generic routine that takes any valid XML and converts it to JSON without knowing the underlying data type. I know that this is easily done with Json.Net and I also know how to do it with the DataContractJsonSerializer but our organisation doesn't use Json.Net and the DataContractJsonSerializer needs a Data Contract enabled object type. My working code using Json.Net: XmlDocument document = new XmlDocument(); document.LoadXml(xml); string jsonText = JsonConvert.SerializeXmlNode

Deserializing a JSON array of mixed types

末鹿安然 提交于 2019-12-24 01:55:07
问题 I'm having trouble deserializing a JSON array of mixed types using the DataContractJsonSerializer class. I've spent a bunch of time looking for a solution to no avail, so I thought I'd go ahead and ask here. Basically, I am getting a JSON string like the one below. I'd like to get the array to deserialize into an List where position 0 has an Int32, position 1 has a String, and position 2 has an instance of my custom class. [ 2, "Mr. Smith", { "num":169, "name":"main street", "state":66 } ] If

parse an array as a Json string using DataContractJsonSerializer WP7

荒凉一梦 提交于 2019-12-22 10:53:58
问题 How can I parse the elements of an array in a Json string using DataContractJsonSerializer? The syntax is: { "array":[ { "elementsProperies":"SomeLiteral" } ] } 回答1: You wouldn't necessarily "parse" a json string using DataContractJsonSerializer, but you can deserialize it into an object or list of objects using this. Here is a simple way to deserialize it to a list of objects if this is what you're after. First you need to have an object type you plan on deserializing to: [DataContract]

HL7 FHIR serialisation to json in asp.net web api

允我心安 提交于 2019-12-19 03:25:21
问题 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 =

Serialize Dictionary<TKey, TValue> to JSON with DataContractJsonSerializer

廉价感情. 提交于 2019-12-18 18:55:43
问题 I have an object tree that I'm serializing to JSON with DataContractJsonSerializer . Dictionary<TKey, TValue> gets serialized but I don't like the markup - the items are not rendered like this: {key1:value, key2:value2} but rather like an array of serialized KeyValuePair<TKey, TValue> objects: [{ "__type":"KeyValuePairOfstringanyType:#System.Collections.Generic", "key":"key1", "value":"value1" }, { "__type":"KeyValuePairOfstringanyType:#System.Collections.Generic", "key":"key2", "value":

How do I serialize a C# anonymous type to a JSON string?

我与影子孤独终老i 提交于 2019-12-17 02:07:16
问题 I'm attempting to use the following code to serialize an anonymous type to JSON: var serializer = new DataContractJsonSerializer(thing.GetType()); var ms = new MemoryStream(); serializer.WriteObject(ms, thing); var json = Encoding.Default.GetString(ms.ToArray()); However, I get the following exception when this is executed: Type '<>f__AnonymousType1`3[System.Int32,System.Int32,System.Object[]]' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all

Silverlight json won't serialize

别说谁变了你拦得住时间么 提交于 2019-12-12 05:57:10
问题 I have been trying to serialize some json data in Silverlight. I am using the following code System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(stacks.GetType()); MemoryStream ms = new MemoryStream(); serializer.WriteObject(ms, stacks); StreamReader reader = new StreamReader(ms); string json = reader.ReadToEnd(); to attempt the serialization. It does not work. It was the only example I could find that did

C# DataMember Serializer Ordering Opposite of Expected

三世轮回 提交于 2019-12-12 04:58:56
问题 According to this article, I'd expect to see the fields in my base class at the top of the list of fields when serializing to JSON. However, I'm seeing the fields at the bottom of the list. The ordering is correct within the actual class itself, but not among the hierarchy. What's happening is it's ordering properly with the class, it it's doing the exact reverse of what I'd expect. I'd expect the base classes to have their fields serialized first. I don't want to use the Order=X attribute

Deserialization of array with DataContractJsonSerializer with Windows Store App

佐手、 提交于 2019-12-12 04:38:09
问题 Is it possible to deserialize a json array with native DataContractJsonSerializer in a Windows Store App? Example, from: [{"groups":[{"name":"tom","vip":false},{"name":"sam","vip":true}]},{"groups":[{"name":"pam","vip":false},{"name":"mom","vip":true}]}] To, anything roughly in the line of: public class Group { public string name { get; set; } public bool vip { get; set; } } [DataContract] public class RootObject { [DataMember] public List<Group> groups { get; set; } } So far, my attempts