0

Below is my class :

public class Categories
{
    public List<Categories> Subcategories { get; set; }         
    public List<Locations> Locations { get; set; }
}

public class Locations
{
    public List<Coordinates> Coordinates { get; set; }
}

public class Coordinates
{
    public int Id { get; set; }
    public int X { get; set; }
    public int Y { get; set; }
}

Now Coordinates contains records like below :

[0] : Id:0
      X:0
      Y:0

[1] : Id:1
      X:100
      Y:200

[2] : Id:2
      X:300
      Y:400

I am receiving 1 json string which I am deserializing like below :

var data = JsonConvert.DeserializeObject<List<Categories>>(json);

Now what I want to do is I want to ignore record from coordinates whose Id value is 0 while deserializing json in List of categories.

I know I can loop on to this data and then remove records from Coordinates where Id == 0 but I am trying to avoid this loop and would like to know that is there any way to do achieve this while deserialization of string in to class?

juanferrer
  • 1,192
  • 1
  • 16
  • 29
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • You probably can't do that with JSON.NET. You can either loop and remove (wich will make your code ugly) or use LINQ. I have no VS here but I GUESS something like that would do the job (and seems like what you are trying to serialize is on json var, so): json = json.Where(x => x.Locations.Where(y => y.Coordidates.Id != 0)).ToList(); – Alexandre Jul 31 '17 at 15:35
  • 1
    Why i am getting close votes and downvotes.I ask this question because i thought there might be some way to do this.So the purpose of asking this question is to learn new thing.I can easily do this with loop or linq.Please dont make it so hard to ask questions on SO :) – I Love Stackoverflow Jul 31 '17 at 15:39
  • 1
    I see one close vote with *Off topic > Questions seeking debugging...* reason, and that's invalid vote cast, I must say. There's nothing unclear in this post nor it is off-topic. I am with you @Learning – Nikhil Vartak Jul 31 '17 at 15:43
  • @niksofteng Thank you so much for understanding.You know sometimes it is hard for me to decide whether i should post some questions or not :) – I Love Stackoverflow Jul 31 '17 at 15:44

1 Answers1

2

If you are interested in knowing another way, then yes there is. You can inherit from JsonConvert class and provide own implementation for serialization and deserialization by overriding WriteJson and ReadJson methods. It is nothing difficult so I leave the implementation to you.

Sample: http://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm

However, if it's just about filtering record for specific ID then KISS. Go for filtering once you have deserialized JSON data. LINQ has a one liner solution for that. No looping, please :)

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32
  • Upvoted for your kind efforts towards helping me but where do i write ;logic(where Id==0)??(in WriteJson method) – I Love Stackoverflow Jul 31 '17 at 16:01
  • I suggest you look through Google, SO specifically for "custom+jsonconverter+newtonsoft" and you would find loads of links. I am sure. That will help you learn better. – Nikhil Vartak Jul 31 '17 at 16:21
  • @Learning - why are you writing a `WriteJson()` method? Wasn't your requirement to skip certain array entries during **reading**? Just override `CanWrite` and return false as shown [here](https://stackoverflow.com/a/29616648/3744182). – dbc Jul 31 '17 at 20:46
  • @dbc But how do i specify condition for Coordinates(where Id==0) in CanWrite method?? – I Love Stackoverflow Aug 01 '17 at 05:48
  • @dbc But i would like to skip certain records not all records from Coordinates class – I Love Stackoverflow Aug 01 '17 at 09:49
  • 1
    @Learning `WriteJson` is a self-descriptive method name that is used during serialization i.e. object > json. You are interested in Deserialization, json > object, and thus `ReadJson` is the one you need. – Nikhil Vartak Aug 01 '17 at 09:54