0

I'm trying to deserialise a JSON string received from a REST call into a clean C# object called User. However, the JSON that returns has a lot of 'fluff' that I wish to ignore.

The JSON string is as follows:

{
  "Items": [
    {
      "LoginID": "A",
      "EmployeeID": "1",
      "FirstName": "A",
      "LastName": "A",
      "MiddleName": "",
      "PrimaryEmail": "A@1",
      "Active": true,
      "CellPhoneNumber": null,
      "OrganizationUnit": null,
      "ID": null,
      "URI": null
    },
    {
      "LoginID": "B",
      "EmployeeID": "2",
      "FirstName": "B",
      "LastName": "B",
      "MiddleName": "",
      "PrimaryEmail": "B@2",
      "Active": true,
      "CellPhoneNumber": null,
      "OrganizationUnit": null,
      "ID": null,
      "URI": null
    }
  ],
  "NextPage": null
}

I wish to convert this into an array of User objects, which are defined as:

[DataContract]
    public class User
    {
        [DataMember]
        public string LoginID { get; set; }
        [DataMember]
        public string EmployeeID { get; set; }
        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string MiddleName { get; set; }
        [DataMember]
        public string PrimaryEmail { get; set; }
        [DataMember]
        public bool Active { get; set; }
        [DataMember]
        public object CellPhoneNumber { get; set; }
        [DataMember]
        public object OrganizationUnit { get; set; }
        [DataMember]
        public object ID { get; set; }
        [DataMember]
        public object URI { get; set; }
    }

I'm trying to avoid using Newtonsoft.Json, as I want to create as few dependencies as possible.

Using the standard DataContractJsonSerializer will not work as it uses the data stream returned. I'd like to avoid creating a class just to get the JSON to fit my class structure and never use it, especially if those classes are exposed to the users of the code.

In short, can I only deserialise a specific part of the JSON string without Newtonsoft.Json? If not, what is the best practise and cleanest way of deserialising the JSON?

Thanks

plusheen
  • 1,128
  • 1
  • 8
  • 23
  • 1
    I hate to break it to you, but you are going to have to do some programming to accomplish your goal. It is very common to use to a mapper from external data to your internal representations so you will probably need to do that. And it is trivial to turn a string into a MemoryStream so don't let that deter you from `DataContractSerializer`. – Crowcoder Apr 18 '16 at 15:34
  • As it's going to be a library, i'm thinking of just sucking it up and stuffing all the root objects into an internal class so the user won't see these classes. It's a little dirty though so I was hoping to come up with a cleaner solution. If it's not possible without fiddling with the JSON string, these root classes might be a better, safer solution. – plusheen Apr 18 '16 at 15:38

1 Answers1

0

I'm not sure if you can do it without using NewtonSoft.Json.

I usually use one of this options for the case that you are showing us.

When the value can be null:

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

or allways ignore for the deserialization:

[JsonIgnore]

This two options will ignore for deserialization.

Jack Morton
  • 154
  • 6
  • 18