0

I have a generic class which clones an object to a view model that is passed.

    public static TR Map<T, TR>(T model)
        where T : class
        where TR : class
    {
        if (model == null)
            return default(TR);

        var data = JsonConvert.SerializeObject(model);
        return JsonConvert.DeserializeObject<TR>(data);
    }

Now my problem is that the class Category has a virtual list of Sub Category and the Sub Category class has a virtual reference to Category. When serializing either Category or Sub Category it is entering an infinite loop. I`ve tried solutions but all just ignore the virtual references. I need it to be serialized with virtual properties included. Any ideas?

RajeshKdev
  • 6,365
  • 6
  • 58
  • 80
mcas
  • 3
  • 4
  • possible duplicate of [JSON.NET Error Self referencing loop detected for type](http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type) – Panagiotis Kanavos Nov 24 '14 at 10:44
  • no it is not a duplicate as I need an answer to not just ignore the virtual references as answered in the solution – mcas Nov 24 '14 at 10:46
  • That's a Json.Net message and the ways to deal with this are well documented and answered multiple times in SO. Have you tried any of them? Serializing to Json is the slowest possible way to map objects, plus you need to deal with unnecessary serialization issues (as you found). Better try a library like AutoMapper – Panagiotis Kanavos Nov 24 '14 at 10:46
  • Is Category/SubCategory a self-reference or are they two distinct classes ? – Alex Nov 24 '14 at 10:50
  • It is a duplicate as the linked answers cover the available options. The options are available in the Json.NET documentation anyway. You can use one of the options to overcome the exception then fill in any missing information. The real problem is that you are using serialization to solve a mapping problem. Serializing circular references can only be done if you break the circle. – Panagiotis Kanavos Nov 24 '14 at 10:51
  • @Alex they are two distinct classes – mcas Nov 24 '14 at 10:52
  • @PanagiotisKanavos I can get by if i m not using JSON. What do you think will be the fastest and can get me what I need? – mcas Nov 24 '14 at 10:53
  • Don't go through serialization. Use a library like [AutoMapper](https://github.com/AutoMapper/AutoMapper/wiki/Getting-started) to map one object to the other. AutoMapper uses conventions so you don't have to write a lot of configuration code. It's also much faster because it doesn't have to serialize data to string and back – Panagiotis Kanavos Nov 24 '14 at 10:56
  • You might have a look at my answer on **[“Self Referencing Loop Detected” exception with JSON.Net](https://stackoverflow.com/questions/40472419/self-referencing-loop-detected-exception-with-json-net/51235783#51235783)** page. – Murat Yıldız Jul 08 '18 at 20:39

1 Answers1

0

How about saving only the category ID on subcategory? you don't have to save the reference of category on subcategory.

Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
  • but then how am i going to have the subcategories in categories and category in subcategory? – mcas Nov 24 '14 at 10:55
  • @mcas you would query the subcategories getting all where the parent category id = category id to get subcategories in a category. – DGibbs Nov 24 '14 at 11:09
  • for subcategories, you have list of subcategory on category. if you need the category, you could search it by category id on subcategory. – muhamad ridwan Nov 24 '14 at 11:12