Question on JSON serialize/Deserialization when property names contain dashes

852 views
Skip to first unread message

Jonathan Haase

unread,
Apr 9, 2018, 1:03:39 PM4/9/18
to RestSharp
I've got a rest API that I'm dealing with where the property names contain dashes. With this being the case I can't build a custom object and use it as the type for serialization/deserialization because c# won't allow those dashes in names... 

Right now I'm just using the dynamic type so that it returns it all as an array with the property names as the indexes.  However I'm getting ready to start a portion of the project which will involve me building a JSON body object for a PATCH command where I'll also have to deal with those.  

This has worked well for me with a different API that I'm talking to where I build the custom object and simply use the .AddJSonBody using my custom object, and it handles everything beautifully.  But I can't seem to get my head wrapped around doing it in this context where I can't build that custom object due to the proper names.

Suggestions?
Message has been deleted

Axa Axa

unread,
Oct 22, 2018, 7:28:07 PM10/22/18
to RestSharp
you probably found the answer already but...

Unsure of of PostSharp default serialized, but this is simply accomplished with json.net by adding an attribute to your custom object property.
So you can do something like this

[JsonProperty(PropertyName = "incompliant-name")]
public object IncompliantName;

Then the when serializing and deserializing it uses the PropertyName from the attribute.

You just use newtonsoft as the serializer rather than the default.


A less simple way but with finer control again with json.net, would be to create a custom deserializer, it must inherit from JsonConverter
And then do what ever you want with the object replacing one char with another or what not
plenty of examples out there on custom converters
Reply all
Reply to author
Forward
0 new messages