deserialization

“Relaxed” fields names for Jackson

左心房为你撑大大i 提交于 2019-12-24 07:15:08
问题 I'm working on Jackson configuration and I wonder if there is any option to deserialise different kinds of field patterns. For example, I have an object: class DeserializeIt { String fieldOne; String fieldOneAndHalf; String fieldTwo; String fieldThree; String fieldFour; //getters setters etc. } And I have below JSON payload: { "fieldOne" : "value1", "field_ONE-and_Half": "value15", "FIELD_TWO": "value2", "FIELD_THREE" : "value3", "field_four": "value4" } I would like to deserialize all these

Json.Net Is converting on its own before using my JsonConverter

三世轮回 提交于 2019-12-24 05:59:59
问题 In my WPF code, I'm using Newtonsoft.Json to deserialize json into my models. First, I receive a Json string ('json') which I then parse into 'message'. (The object I want to deserialize is wrapped in a "data" field in the json string). Activity message = JObject.Parse(json)["data"].ToObject<Activity>(); My Activity class uses several [JsonProperty] attributes to generate its fields. One of them is an enum called 'ActivityType'. [JsonProperty("type")] [JsonConverter(typeof

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;

Creating an Entity from XML

只谈情不闲聊 提交于 2019-12-24 05:17:06
问题 I have the following XML structure <T> <F> <H> <H1>some value</H1> <H2>some value</H2> <H3>some value</H3> </H> <O> <P>some value</P> <TI>some value</TI> <TI>some value</TI> </O> <R> <PTY>some value</PTY> <PTY>some value</PTY> <PTY>some value</PTY> </R> </F> <T> I need to parse this xml in C# and get the values out of them to be further exported to a CSV file. My query is how do you go about creating an entity for this XML 回答1: You can play with XmlSerializer and its related attributes. As

Deserialize JSON into a generic map, forcing all JSON floats into Decimal or String, in Jackson

本小妞迷上赌 提交于 2019-12-24 04:55:16
问题 I am using Jackson in the jsonrpc4j to access a remote service. In my Java application there are no defined classes for the return values, so deserialization produces generic LinkedHashMaps. So I cannot put any annotations anywhere. jsonrpc4j can take in a Jackson ObjectMapper object. The remote service responds with structured json objects, some fields of which are very big decimal numbers, and Jackson treats them as Doubles. An example object can look like this {"s1":"zxcvb","f1":20.00234,

serialize object for unit testing

烈酒焚心 提交于 2019-12-24 03:43:14
问题 Assuming in a unit test I need to have an object all of 50 fields of which are set up with some values. I don't want to manually set up all this fields, as it takes time and annoying... Somehow I need to get an instance where all the fields are initialized by some not-null values. and I had an idea - what if I will debug some code, at some point i will get a working instance of this object with some data set up - and I just will serialize it to the disk. Then I will put this file to test

serialize object for unit testing

喜欢而已 提交于 2019-12-24 03:43:13
问题 Assuming in a unit test I need to have an object all of 50 fields of which are set up with some values. I don't want to manually set up all this fields, as it takes time and annoying... Somehow I need to get an instance where all the fields are initialized by some not-null values. and I had an idea - what if I will debug some code, at some point i will get a working instance of this object with some data set up - and I just will serialize it to the disk. Then I will put this file to test

DjangoRestFramework - correct way to add “required = false” to a ModelSerializer field?

丶灬走出姿态 提交于 2019-12-24 03:43:00
问题 This is my serializer: class PostSerializer(serializers.ModelSerializer): class Meta: model = Post fields = ('user', 'post', 'reblog',) def create(self, validated_data): post = Post( user = validated_data['user'], post = validated_data['post'], ) post.save() # manually create a list of "reblog" users post.reblog.add(validated_data['user']) return post and this is my model: class Post(models.Model): user = models.ForeignKey(User) post = models.CharField(max_length=400) reblog = models

Prevent XmlSerializer from auto instantiating List's on Deserialize

落爺英雄遲暮 提交于 2019-12-24 02:38:06
问题 Assuming I have this sample class: public class MyClass { public List<int> ListTest { get; set; } public string StringTest { get; set; } public int IntTest { get; set; } } And this code: string xmlStr = "<MyClass><StringTest>String</StringTest></MyClass>"; XElement xml = XElement.Parse(xmlStr); XmlSerializer ser = new XmlSerializer(typeof(MyClass)); using (XmlReader reader = xml.CreateReader()) { var res = ser.Deserialize(reader); } After the Deserialize is completed the value of res is:

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