deserialization

Deserialization of optional fields from BinaryFormatter

二次信任 提交于 2019-12-30 10:08:10
问题 I have an application that serializes data using BinaryFormatter . A member was added to the class that was serialized from one version to the next without changing the class name. Code was added to handle the possible absence of the added member in old serialized files: private void readData(FileStream fs, SymmetricAlgorithm dataKey) { CryptoStream cs = null; try { cs = new CryptoStream(fs, dataKey.CreateDecryptor(), CryptoStreamMode.Read); BinaryFormatter bf = new BinaryFormatter(); string

Copy object properties: reflection or serialization - which is faster?

主宰稳场 提交于 2019-12-30 04:34:25
问题 I have two objects of the same type and need to copy property values from one object to another. There are two options: Use reflection, navigate through the properties of the first object and copy the values. Serialize the first object and deserialize a copy. Both work for my requirement, the question is which do I better use in the terms of speed (cost)? Example class Person { public int ID { get; set; } public string Firsthand { get; set; } public string LastName { get; set; } public int

Deserializing complex object using Json.NET

落爺英雄遲暮 提交于 2019-12-30 04:21:32
问题 I need to deserialize the this json returned from grogle maps api: { "destination_addresses": [ "Via Medaglie D'Oro, 10, 47121 Forlì FC, Italia", "Via Torino, 20123 Milano, Italia", "Via Guglielmo Marconi, 71, 40121 Bologna, Italia", "Via Irnerio, 40126 Bologna, Italia" ], "origin_addresses": [ "Via Medaglie D'Oro, 10, 47121 Forlì FC, Italia", "Via Torino, 20123 Milano, Italia", "Via Guglielmo Marconi, 71, 40121 Bologna, Italia", "Via Irnerio, 40126 Bologna, Italia" ], "rows": [ { "elements":

Java serialization - Android deserialization

北慕城南 提交于 2019-12-30 01:27:06
问题 I have tried implementing cross platform serialization between Java and Android. I have used Serializable, and having my code in Android in the same package as in desktop Java. Source: java-desktop serializing Student student=new Student(); student.setName("John"); student.setSurname("Brown"); student.setNumber(776012345); try { FileOutputStream fout = new FileOutputStream("thestudent.dat"); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(student); oos.close(); } catch

Deserializing JSON to flattened class

喜你入骨 提交于 2019-12-29 09:20:11
问题 I found the same question here... Deserializing nested JSON structure to a flattened class with Json.NET using annotations ...but without a proper answer. One of the best suggestion is to wrap the nested object in a new class but this approach introduces another issue: lego name. In my example the most logic name for this class is the same name that parent class and of course is not possible. My example is simple, I just want to eliminate the "language" property in the parent class. Can

Why can I not deserialize this custom struct using Json.Net?

佐手、 提交于 2019-12-29 08:36:28
问题 I have a struct representing a DateTime which also has zone info as below: public struct DateTimeWithZone { private readonly DateTime _utcDateTime; private readonly TimeZoneInfo _timeZone; public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone, DateTimeKind kind = DateTimeKind.Utc) { dateTime = DateTime.SpecifyKind(dateTime, kind); _utcDateTime = dateTime.Kind != DateTimeKind.Utc ? TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone) : dateTime; _timeZone = timeZone; } public DateTime

XmlSerializer: How to Deserialize an enum value that no longer exists

随声附和 提交于 2019-12-29 08:32:36
问题 I am using the XMLSerializer to save this class to a file. The class has a string and an enum as shown below: public class IOPoint { string Name {get; set;} TypeEnum {get; set;} } public enum TypeEnum { Temperature, Pressure, Humidity, } When serialized it looks like this. <IOPoint> <Name>Relative Humidity</Name> <TypeEnum>Humidity</TypeEnum> </IOPoint> I've been serializing and deserializing this object with no problems for several versions. I no longer want to support Humidity, so I removed

Unserialize PHP data in python [duplicate]

跟風遠走 提交于 2019-12-29 06:44:06
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to read/make sense of a PHP serialised data string in python I'm using python to access a database which is managed by a Drupal install. The data I want to access in Drupal in saved in the database as a PHP serialized object. Are there any pre-built python modules which can deserialize a PHP serialized object into a python object? I've done some searching and come up with nothing. I realize I could write my

Selectively escape HTML in strings during deserialization

◇◆丶佛笑我妖孽 提交于 2019-12-29 01:41:13
问题 I'm looking to write a JsonConverter which escapes HTML in strings, unless the [AllowHtml] attribute has been applied; private class ObjectWithStrings { // will be HTML-escaped public string Name { get; set; } // won't be escaped [AllowHtml] public string Unsafe { get; set; } } So I'm trying to write a JsonConverter with a custom ReadJson property; public override bool CanConvert(Type objectType) { return objectType == typeof(string); } public override object ReadJson(JsonReader reader, Type

De-serializing objects from a file in Java

瘦欲@ 提交于 2019-12-28 16:11:32
问题 I have a file which contains multiple serialized objects of class XYZ. While serializing, the each XYZ object was appended to the file. Now I need to read each object from the file, and I am able to read only the first object. Any idea how I can read each object from the file and eventually store it into a List? 回答1: Try the following: List<Object> results = new ArrayList<Object>(); FileInputStream fis = new FileInputStream("cool_file.tmp"); ObjectInputStream ois = new ObjectInputStream(fis);