3

I have a BaseDTO c# class which just has one property of type boolean called Success

I then have a derived class which has some extra properties.

When this derived class gets outputted in JSON in web api the Success property appears @ the bottom.

I would like it to appear first.
Is there any attribute i can set on the property to allow this?

Thanks

AdrianSean
  • 397
  • 1
  • 5
  • 21

1 Answers1

0

It doesn't matter what order they are in:

An object is an unordered set of name/value pairs.
- http://json.org/

If you're requiring an order to the properties, then you are no longer dealing with true JSON and can't expect existing libraries to accommodate this requirement out the box.

Having said that, JSON.NET allows you to implement a IContractResolver which you could create to search for a custom attribute and favour that. More details can be found in this Stack Overflow answer.


Actually this answer on the same question shows an existing attribute you can use instead of the above.

Community
  • 1
  • 1
dav_i
  • 27,509
  • 17
  • 104
  • 136
  • yes that looks much simpler and what i was aiming for - i'm not using JSON.net currently so may look @ importing it. However i'm also now kind of against doing this following the json.org statement above!! – AdrianSean Feb 27 '15 at 12:54
  • @AdrianSean Actually WebApi uses JSON.net under the covers so this should be available. – dav_i Feb 27 '15 at 12:55
  • 2
    nice - setting attribute as [JsonProperty(Order = -2)] does the trick in my base class :) – AdrianSean Feb 27 '15 at 13:10