0

I need to deserialize an object that has a different name according to Get or Post.

I am working with visual studio (VB) with the class HttpClient and Newtonsoft's Json.NET. When I make the call get the object is presented like this:

  • id (int)
  • name (str)
  • type (int)

When I make the call Post the requested JSON is:

  • id (int)
  • name (str)
  • type_sii (int)

The problem is that my object is declared like this:

<JsonProperty ("id")>
 Public Property Id As Integer
<JsonProperty ("name")>
 Public Property Name As String
<JsonProperty ("type")>
 Public Property Type As Integer

Then the object its good me when I make the GET call, how can I change the property (Type) when I want to make a post call? (change to "type_sii")

dbc
  • 104,963
  • 20
  • 228
  • 340
  • You could add both properties and decorate with ``, ``, setting the unused property to null (`nothing`). Non-reference types can be declared as nullable e.g., `Public Property type As Integer?` (Btw, `Type` is bad name). – Jimi Jun 20 '19 at 03:44
  • Right, what I mean is that a Property decorated with a `JsonProperty` that specifies `NullValueHandling = NullValueHandling.Ignore`, when set to null is not serialized. Otherwise, implement a custom serializer. – Jimi Jun 20 '19 at 03:51
  • The simple solution is to create two properties but my question was if I could declare only one. The "NullValueHandling" i use it in "JsonSerializerSettings" not in properties. –  Jun 20 '19 at 04:07
  • So basically you want `Type` to have a different name serializing vs deserializing. If so, are these what you want? [How to ignore JsonProperty(PropertyName = “someName”) when serializing json?](https://stackoverflow.com/q/20622492/3744182), [JsonProperty - Use different name for deserialization, but use original name for serialization?](https://stackoverflow.com/q/22993576) and/or [json deserialize from legacy property names](https://stackoverflow.com/q/33155458/3744182)? – dbc Jun 20 '19 at 05:54
  • Possible duplicate of [JsonProperty - Use different name for deserialization, but use original name for serialization?](https://stackoverflow.com/questions/22993576/jsonproperty-use-different-name-for-deserialization-but-use-original-name-for) – Craig Jun 20 '19 at 13:11
  • @dbc No. no type. The difference is in the name. –  Jul 12 '19 at 02:17

0 Answers0