deserialization

JsonSerializer silently fails to deserialize my object

…衆ロ難τιáo~ 提交于 2020-08-10 22:46:28
问题 I am trying to store my relational object in a JSON file rather than a SqlLite database as I think it suits better for what I am trying to do. I am using Microsoft's System.Text.Json.Serialization but that does not seem to be working. My object is saved to a JSON text file with this: var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = true }; options.Converters.Add(new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); // Enums as

Deserialize inconsistent JSON property

╄→尐↘猪︶ㄣ 提交于 2020-08-09 08:02:11
问题 Hopefully someone can help me with the following inconsistency occurring in a large JSON file that I am attempting to deserialize using Newtonsoft.Json. One of the properties of the object occasionally appears as: "roles": [ { "field1" : "value", "field2" : "value" } ] While other times that same property appears as: "roles": { "roles": [ { "field1" : "value", "field2" : "value" } ] } For reference, this property is implemented in its class as: [JsonProperty("roles")] public List<Role> Roles

Enable Jackson Deserialization of Empty Objects to Null

非 Y 不嫁゛ 提交于 2020-08-03 02:20:12
问题 I was asked to change our jackson mapping configuration so that each empty object we deserialize (from JSON) is going to be deserialized as null. The problem is that I'm struggling to do it, but without any luck. Here is a sample of our ObjectMapper configuration (and example): ObjectMapper mapper = new ObjectMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(SerializationFeature.FAIL_ON

Enable Jackson Deserialization of Empty Objects to Null

Deadly 提交于 2020-08-03 02:19:09
问题 I was asked to change our jackson mapping configuration so that each empty object we deserialize (from JSON) is going to be deserialized as null. The problem is that I'm struggling to do it, but without any luck. Here is a sample of our ObjectMapper configuration (and example): ObjectMapper mapper = new ObjectMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(SerializationFeature.FAIL_ON

Deserializing cXML to C# Class

三世轮回 提交于 2020-08-02 13:08:12
问题 I'm trying to deserialize a cXML string into C# classes. However, when deserializing, I can't seem to get anything other than the root element to deserialize. How to Deserialize XML document seemed to provide the most help, and using that example I've managed to deserialize the root cXML tag, but this doesn't work on the nested object PunchOutSetupRequest which is the object I really need deserialized. My question, before showing you the wall of code, is how can I properly deserialize these

Default value for null fields using GSON

。_饼干妹妹 提交于 2020-07-22 09:19:41
问题 I would like to set default values for some fields on my model, so if during deserialization my json has missing fields, those default values are used. What I would like to understand which is the best practice to do this, and why. I should add the control of null in the setters of my model, I should use the try catch to find any cases, I should extend and modify typeAdapter (this solution seems very verbose to me, let me know if I'm wrong) or there is a even better solution? 回答1: If those

Default value for null fields using GSON

余生颓废 提交于 2020-07-22 09:19:35
问题 I would like to set default values for some fields on my model, so if during deserialization my json has missing fields, those default values are used. What I would like to understand which is the best practice to do this, and why. I should add the control of null in the setters of my model, I should use the try catch to find any cases, I should extend and modify typeAdapter (this solution seems very verbose to me, let me know if I'm wrong) or there is a even better solution? 回答1: If those