System.Text.Json – Deserialize nested object as string

Found a right way how to correctly read the nested JSON object inside the JsonConverter. The complete solution is the following: public class SomeModel { public int Id { get; set; } public string Name { get; set; } [JsonConverter(typeof(InfoToStringConverter))] public string Info { get; set; } } public class InfoToStringConverter : JsonConverter<string> { public … Read more

xsi:type attribute messing up C# XML deserialization

The short answer is that you need to manually add [XmlInclude(typeof(SequencePoint))] to your MethodPoint class: [System.CodeDom.Compiler.GeneratedCodeAttribute(“xsd”, “4.0.30319.33440”)] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute(“code”)] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] [XmlInclude(typeof(SequencePoint))] public partial class CoverageSessionModulesModuleClassesClassMethodsMethodMethodPoint { private string vcField; private string uspidField; private string ordinalField; private string offsetField; private string slField; private string scField; private string elField; private string ecField; private string becField; private … Read more

How to prevent a single object property from being converted to a DateTime when it is a string

What one can do is to add a custom JsonConverter to the InputModel type to temporarily toggle JsonReader.DateParseHandling to None: [JsonConverter(typeof(DateParseHandlingConverter), DateParseHandling.None)] class InputModel { public string Name { get; set; } public object Value { get; set; } } public class DateParseHandlingConverter : JsonConverter { readonly DateParseHandling dateParseHandling; public DateParseHandlingConverter(DateParseHandling dateParseHandling) { this.dateParseHandling = … Read more

Polymorphism in jackson annotations: @JsonTypeInfo usage

@JsonSubTypes.Type must have a value and a name like this, @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=As.WRAPPER_OBJECT, property=”type”) @JsonSubTypes({ @JsonSubTypes.Type(value=Dog.class, name=”dog”), @JsonSubTypes.Type(value=Cat.class, name=”cat”) }) In the subclass, use @JsonTypeName(“dog”) to say the name. The values dog and cat will be set in the property named type.

How do you deserialize XML with dynamic element names?

You can do this by having your Rates collection implement IXmlSerializable: public class Rates : List<Rate>, IXmlSerializable { public Rates() : base() { } public Rates(IEnumerable<Rate> collection) : base(collection) { } #region IXmlSerializable Members XmlSchema IXmlSerializable.GetSchema() { return null; } void IXmlSerializable.ReadXml(XmlReader reader) { // for the `decodeName` delegate, you could check that the node … Read more

VB.net JSON Deserialize

Here is the easiest way to deserialize JSON into an object (using .NET 4): Example JSON: { “dogs”:[], “chickens”:[ { “name”:”Macey”, “eggs”:7 }, { “name”:”Alfred”, “eggs”:2 } ] } VB.NET: Try Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(JSONString) Dim a = j(“dogs”) ‘ returns empty Object() array Dim b = j(“chickens”)(0) ‘ returns Dictionary(Of … Read more

How do I implement TypeAdapterFactory in Gson?

When you register a regular type adapter (GsonBuilder.registerTypeAdapter), it only generates a type adapter for THAT specific class. For example: public abstract class Animal { abstract void speak(); } public class Dog extends Animal { private final String speech = “woof”; public void speak() { System.out.println(speech); } } // in some gson related method gsonBuilder.registerTypeAdapter(Animal.class, … Read more

Jackson JSON library: how to instantiate a class that contains abstract fields

There are multiple ways; before version 1.8, simplest way is probably to do: @JsonDeserialize(as=Cat.class) public abstract class AbstractAnimal { … } as to deciding based on attribute, that is best done using @JsonTypeInfo, which does automatic embeddeding (when writing) and use of type information. There are multiple kinds of type info (class name, logical type … Read more