Archive

Archive for the ‘JSON’ Category

Working with JSON Values that are C# Reserve Words.

February 17, 2011 Leave a comment

February 17th 2010 | Jimmy Bosse

Working with JSON Values that are C# Reserve Words

While playing around with GitHub’s API for Gists, I discovered I couldn’t simply deserialize into an object because the API used the C# reserve word “public” as a property name. Luckily, the DataMember attribute has a “Name” property that allows to you explicitly map a JSON property to your object’s property of a different name:

   1:  namespace MyGistClient
   2:  {
   3:      [DataContract]
   4:      public class GistsResult
   5:      {
   6:          ...
   7:   
   8:          [DataMember(Name = "public")]
   9:          public bool IsPublic { get; set; }
  10:   
  11:          ...
  12:      }
  13:  }

While C# will let me make a property named “Public”, I decided to use “IsPublic” instead so I don’t have any issues if my object gets consumed by another .NET language that isn’t case sensitive.

Jimmy Bosse is a Senior .NET developer and Team Lead at Thycotic Software, an agile software services and product development company based in Washington DC. Secret Server is our flagship password management software product.

Categories: C#, JSON Tags: , ,