serialization

Newtonsoft.Json replacing spaces with underscores C# to JSON Serialize [duplicate]

情到浓时终转凉″ 提交于 2019-12-22 13:45:46
问题 This question already has answers here : How to apply a general rule for remapping all property names when serializing with Json.NET? (2 answers) Closed 2 years ago . INITIAL QUESTION: Here I have a function I use to convert from Entity Framework object to JSON: public class JSON { public static string ConvertEntityToJSON(object dataToSerialize) { return JsonConvert.SerializeObject(dataToSerialize, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); } } When

Is there an alternative for ShouldSerialize[PropertyName] in C#?

橙三吉。 提交于 2019-12-22 13:32:36
问题 I have been writing a lot of code lately which involves serialization using Json.NET and due to the nature of the data that I serialize, sometimes not all of their properties need to be serialized so, I do as follows... public int Foo { get; set; } public bool ShouldSerializeFoo() => Foo > -1; This's good and works but involves a lot of work if you have many properties ( in my case I have over 100 ). So, I wanted to know if there's an alternative to writing those methods. 回答1: One alternative

Is there an alternative for ShouldSerialize[PropertyName] in C#?

这一生的挚爱 提交于 2019-12-22 13:31:02
问题 I have been writing a lot of code lately which involves serialization using Json.NET and due to the nature of the data that I serialize, sometimes not all of their properties need to be serialized so, I do as follows... public int Foo { get; set; } public bool ShouldSerializeFoo() => Foo > -1; This's good and works but involves a lot of work if you have many properties ( in my case I have over 100 ). So, I wanted to know if there's an alternative to writing those methods. 回答1: One alternative

C# Serialize object to SOAP string array issue

徘徊边缘 提交于 2019-12-22 13:00:49
问题 I have an issue where trying to serialize an object containing a string array to soap causes an exception in my application. I am doing the following to create the soap formatter: XmlTypeMapping mapping = new SoapReflectionImporter().ImportTypeMapping(obj.GetType()); XmlSerializer serializer = new XmlSerializer(mapping); when I call Serialize on the serializer I get the following exception. "Token StartElement in state Epilog would result in an invalid XML document." However if I just want

C# Serialize object to SOAP string array issue

£可爱£侵袭症+ 提交于 2019-12-22 13:00:21
问题 I have an issue where trying to serialize an object containing a string array to soap causes an exception in my application. I am doing the following to create the soap formatter: XmlTypeMapping mapping = new SoapReflectionImporter().ImportTypeMapping(obj.GetType()); XmlSerializer serializer = new XmlSerializer(mapping); when I call Serialize on the serializer I get the following exception. "Token StartElement in state Epilog would result in an invalid XML document." However if I just want

What's the best way to serialize / deserialize a Dictionary<String,String> in .Net 2.0?

雨燕双飞 提交于 2019-12-22 12:52:18
问题 I need to be able to serialize / deserialize a Dictionary to a human-readable (and if it comes down to it, human-editable) string (XML or Json preferably). I only need to work with String dictionaries, not other object types of any kind. The strings, however, are presumed to be free text, so they can have characters like single/double quotes, etc, so they must be encoded/escaped correctly. This project is based on .Net 2.0 so I can't use JSON.Net unfortunately. Any ideas what's the best way

Android LinkedHashMap deserializing as HashMap, causes CCE error

妖精的绣舞 提交于 2019-12-22 12:27:14
问题 I'm attempting to pass a serialized LinkedHashMap between activities, and getting a confusing result/error when I deserialize the object. I serialize the object as follows: Bundle exDetails = new Bundle(); LinkedHashMap<String, Exercise> exMap = new LinkedHashMap<String, Exercise (workout.getExercises()); exDetails.putString(WORKOUTNAME, workout.getWorkoutName()); exDetails.putSerializable(workout.getWorkoutName(), exMap); iComplete.putExtra("wName", exDetails); startActivity(iComplete); That

Existing data serialized as hash produces error when upgrading to Rails 5

…衆ロ難τιáo~ 提交于 2019-12-22 11:35:25
问题 I am currently upgrading a Ruby on Rails app from 4.2 to 5.0 and am running into a roadblock concerning fields that store data as a serialized hash. For instance, I have class Club serialize :social_media, Hash end When creating new clubs and inputting the social media everything works fine, but for the existing social media data I'm getting: ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Hash, but was a ActionController::Parameters. How can I convert all of the

Existing data serialized as hash produces error when upgrading to Rails 5

非 Y 不嫁゛ 提交于 2019-12-22 11:35:17
问题 I am currently upgrading a Ruby on Rails app from 4.2 to 5.0 and am running into a roadblock concerning fields that store data as a serialized hash. For instance, I have class Club serialize :social_media, Hash end When creating new clubs and inputting the social media everything works fine, but for the existing social media data I'm getting: ActiveRecord::SerializationTypeMismatch: Attribute was supposed to be a Hash, but was a ActionController::Parameters. How can I convert all of the

Rails: Serialize value as comma-separated and not as YAML

久未见 提交于 2019-12-22 11:34:06
问题 I'm looking for a way to store a serialized value of eg. IDs in a column. In before claims that this is not an optimal design: the column is used for IDs of associated records, but will only be used when displaying the record - so no queries are made with selection on the column and no joins will be made on this column either. In Rails I can serialize the column by using: class Activity serialize :data end This encodes the column as YAML. For legacy sake and since I'm only storing one