deserialization

How can I mass implement Deserialize for all types that implement a specific trait?

折月煮酒 提交于 2019-12-10 22:24:25
问题 I am deserializing a YAML config file with Serde. For most structs I deserialize into, things are quite simple — there's a one-to-one relationship between the fields of the structs and the properties in my YAML file. In a few cases, things are a bit more complicated. For these, the properties in the YAML file are better viewed as parameters to the constructor. The actual struct will have different fields, calculated from those. For these cases, I have written separate config structs that I

Deserializing an ArrayList. no valid constructor

不问归期 提交于 2019-12-10 21:24:14
问题 This how I deserialize my arrayList which contains objects of identification public void deserializeArrayList(){ String path = "./qbank/IdentificationHARD.quiz"; try{ FileInputStream fileIn = new FileInputStream(path); ObjectInputStream in = new ObjectInputStream(fileIn); ArrayList<Identification> list = (ArrayList<Identification>) in.readObject(); System.out.println(list); }catch(Exception e){ e.printStackTrace(); } } This is how I serialize it public void saveItemIdentification(ArrayList

Deserialization of JSON object with sub field as string using DataContractJsonSerializer in C#

你。 提交于 2019-12-10 20:56:09
问题 Here is my JSON: { "Name": "Eli", "Age": 4, "Children": { "Moshe": { "Age": 6, "Characteristics": "Nice;Good;" }, "Yossi": { "Age": 3, "Characteristics": "Hero;Brave" } } } Here is my JSON deserialization function: public static object FromJSON<T>(string json) { using (MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(json))) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); return serializer.ReadObject(stream); } } I'm trying to serialize it

How would I read into a 'nested' Json file with 'DataContractJsonSerializer' in C# .NET (win7 phone)?

给你一囗甜甜゛ 提交于 2019-12-10 20:02:52
问题 I have an issue where if my json file looks like this { "Numbers": "45387", "Words": "space buckets"} I can read it just fine, however if it looks like this: { "Main" :{ "Numbers": "45387", "Words": "space buckets"}, "Something" :{"Numbers": "12345", "Words": "Kransky"} } I get no information back. I have no idea how to switch between Main and Something! Loading a JSON with this 'nested' information using this code, var ser = new DataContractJsonSerializer(typeof(myInfo)); var info = (myInfo

Deserialize Xml Using XmlReader and Class from xsd.exe

不问归期 提交于 2019-12-10 19:46:15
问题 OK I have hit a snag trying to learn XmlSerializer while going through some tutorials. I have followed all the recommended steps but my program is not returning anything, or is returning null. I created an XML file as follows: <?xml version='1.0' encoding='UTF-8' ?> <WeeklyJobs> <DailyJobs Date = "02/03/2012"/> <DailyJobs Date = "02/04/2012" TotalJobs = "2"> <Jobs> <Job JobName = "Job Name" Description = "Description"/> <Job JobName = "Job Name" Description = "Description"/> </Jobs> <

Deserialize a type containing a Dictionary property using ServiceStack JsonSerializer

孤者浪人 提交于 2019-12-10 19:13:18
问题 The code snippet below shows two ways I could achieve this. The first is using MsgPack and the second test is using ServiceStack's JSONSerializer. The second is more favourable because the ServiceStack.Text JSONSerializer is used throughout a project I'm working in. Why is the second test below failing when using a Dictionary<Street,HashSet<int>>? [TestFixture] public class NeighbourhoodTests { private Neighbourhood _myNeighbourhood; private Street _street; [SetUp] public void SetupOnEachTest

What is the best practice to deserialize a JSON object with field that might be either string or int?

强颜欢笑 提交于 2019-12-10 18:24:05
问题 I am trying to use Newton Json Deserializer to deserialize json object which looks like this: { "grades": 97 } Or this: { "grades": "" } I wonder how should I properly define a DTO class in C# to represent the this object? Thanks! 回答1: Please try with dynamic keyword: public class Rootobject { public dynamic grades { get; set; } } It will work with both string and int . For more info refer to using dynamic type. UPDATE: Then to convert into int or string I will use Convert.ToString() because

Json.net no longer throws in case of duplicate

送分小仙女□ 提交于 2019-12-10 18:09:51
问题 I'm trying to upgrade my C# application from Newtonsoft.JSON 6 to the latest version (9.0.1). I noticed a change of behavior when deserializing object containing duplicate elements, such as : { "name": "test", "data": { "myElem": 1, "myElem": 2 } } When deserializing such object, Json.net was previously throwing an ArgumentException. Now the deserialization succeed and it seems that it uses the value of the last duplicated key (hence "2" in above example). From what I read, there is some

Deserialization order over the inheritance tree

蓝咒 提交于 2019-12-10 17:52:49
问题 I have a base class marked Serializable , and derived classes marked Serializable too. I want to do something in the base class during deserialization, and therefore declared a method marked OnDeserializing , but it's important that this method will execute before any derived class's OnDeserializing methods. Derived classes might be written by others too. Can i rely that the base class's method will be called prior to any serialization method in derived classes? I gonna use SoapFormatter .

Jackson deserialization with anonymous classes

爱⌒轻易说出口 提交于 2019-12-10 17:39:57
问题 I have been searching all day for something that answers this, but I have not had a lot of luck thus far. My question is straightforward: how do I deserialize an anonymous object correctly using Jackson. private interface Interface1 { int getValue(); } public static void testAnonymousObject() throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); mapper