10
votes

I am exactly in the same case that this question: How do I make JSON.NET ignore object relationships?

I see the proposed solution and I know I must use a Contract Revolver, and I also see the code of the Contract Resolver, but I do not know how to use it.

  • Should I use it in the WebApiConfig.vb?
  • Should I modify my Entity Model anyway?
2
If you are in the same situation than me, here is my advice: forget it and change you controller to OData. It works perfectly, and there is not problem with JSON serializer. โ€“ Carlos
Carlos, my bellow solution works, may you mark it as the correct answer? โ€“ RAM
I have marked your answer, RAM, because it has some upvotes, so it must work. But I comment this because it can be useful (if was for me). In the constructor, Configuration.LazyLoadingEnabled did the trick: public ExampleController() { db.Configuration.LazyLoadingEnabled = false; } โ€“ Carlos
Thank you. Also my answer works in both True & False values of LazyLoadingEnabled property. โ€“ RAM

2 Answers

34
votes

It is a useful question๐Ÿ‘ and I hope this help:

A)

If you have created your models manually (without Entity Framework), mark the relation properties as virtual first.

If your models were created by EF, It has already done it for you and each Relation Property is marked as virtual, as seen below:

enter image description here

Sample class:

public class PC
{
    public int FileFolderId {get;set;}

    public virtual ICollection<string> Libs { get; set; }
    public virtual ICollection<string> Books { get; set; }
    public virtual ICollection<string> Files { get; set; }
}

B)

Those relation properties can now be ignored by the JSON serializer by using the following ContractResolver for JSON.NET:

CustomResolver:

class CustomResolver : DefaultContractResolver
{
    private readonly List<string> _namesOfVirtualPropsToKeep=new List<string>(new String[]{});

    public CustomResolver(){}

    public CustomResolver(IEnumerable<string> namesOfVirtualPropsToKeep)
    {
        this._namesOfVirtualPropsToKeep = namesOfVirtualPropsToKeep.Select(x=>x.ToLower()).ToList();
    }

    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        JsonProperty prop = base.CreateProperty(member, memberSerialization);
        var propInfo = member as PropertyInfo;
        if (propInfo != null)
        {
            if (propInfo.GetMethod.IsVirtual && !propInfo.GetMethod.IsFinal
                && !_namesOfVirtualPropsToKeep.Contains(propInfo.Name.ToLower()))
            {
                prop.ShouldSerialize = obj => false;
            }
        }
        return prop;
    }
}

C)

Finally, to serialize your model easily use the above ContractResolver. Set it up like this:

// -------------------------------------------------------------------
// Serializer settings
JsonSerializerSettings settings = new JsonSerializerSettings
{
    // ContractResolver = new CustomResolver();
    // OR:
    ContractResolver = new CustomResolver(new []
    {
        nameof(PC.Libs), // keep Libs property among virtual properties
        nameof(PC.Files) // keep Files property among virtual properties
    }),
    PreserveReferencesHandling = PreserveReferencesHandling.None,
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
    Formatting = Formatting.Indented
};

// -------------------------------------------------------------------
// Do the serialization and output to the console
var json = JsonConvert.SerializeObject(new PC(), settings);
Console.WriteLine(json);

// -------------------------------------------------------------------
// We can see that "Books" filed is ignored in the output:
// {
//  "FileFolderId": 0,
//  "Libs": null,
//  "Files": null
// }

Now, all the navigation (relation) properties [virtual properties] will be ignored automatically except you keep some of them by determine them in your code.๐Ÿ˜Ž

Live DEMO


Thanks from @BrianRogers for his answer here.

1
votes

If you are using Newtonsoft.Json

Mark field with

Newtonsoft.Json.JsonIgnore

Instead of

System.Text.Json.Serialization.JsonIgnore