4

This just started happening recently on code that hasn't changed (it's an external library from a nuget package).

If I do this:

var client = HttpClient()
var response = await client.PostAsJsonAsync("some url"), someObject);

it sends the object serialized as Json, but with "k__BackingField" for each property.

It was all working 100% for months. Something must have changed in the solution that's using that library but I'm not sure what.

I've searched and seen plenty of examples on how to fix this in a WebAPI project, but this is a windows app. I haven't seen anyway to change the configuration of the JsonSerializer that it uses.

aircan
  • 291
  • 1
  • 3
  • 6
  • http://stackoverflow.com/questions/13022198/how-to-remove-k-backingfield-from-json-when-deserialize – stuartd Jan 23 '17 at 21:56
  • 3
    Possible duplicate of [How to remove k\_\_BackingField from json when Deserialize](http://stackoverflow.com/questions/13022198/how-to-remove-k-backingfield-from-json-when-deserialize) – Nico Jan 23 '17 at 21:56
  • 1
    None of the classes being serialized/deserialized have the [Serializable] attribute. Nor do they have [DataContract]. I'm not sure why I would need to add [DataContract] to all of them when they were working fine a week ago. – aircan Jan 23 '17 at 22:05
  • @aircan Did you find a solution to this? This exact thing happened to me recently and haven't been able to resolve it yet. Code that has been working for over a year suddenly started doing this. – dherrin79 Feb 10 '18 at 01:08

1 Answers1

6

I ran into this same problem a while ago. There was definitely something changed, since it worked perfectly for months.

My solution was to add the [JsonObject] attribute to my classes.

[JsonObject]
public class MyClass { ... }

This allowed me to keep using properties without having to add [DataMember] to all properties.

Alternatively, you can convert all your properties to fields instead, which should work just as well, but again requires more work.

William
  • 772
  • 7
  • 18
  • 2
    This worked for me. However I didn't have to do this before. Code that worked for over a year suddenly started adding the backing fields. Any thoughts on what changed? – dherrin79 Feb 12 '18 at 13:47