rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
230 stars 29 forks source link

Models with loop reference #186

Closed rosostolato closed 6 years ago

rosostolato commented 6 years ago

I'm trying to serialize a model that has children with reference to their parents, so I guess that is why it's throwing exception. On Newtonsoft Json I use [JsonIgnore] on parents reference and all works great. Does NetJson have a way to igonre them too?

image

rpgmaker commented 6 years ago

It has indirect support for it. You can add it yourself using the provided extension methods. Example below

static void Main(String[] args){
    NetJSON.CanSerialize = CanSerialize;
}

private static bool CanSerialize(MemberInfo memberInfo)
{
    var attr = memberInfo.GetCustomAttribute<TestIgnoreAttribute>();
    if(attr != null)
    {
        return false;
    }
    return true;
}

public class TestClassWithIgnoreAttr
{
    [TestIgnore]
    public int ID { get; set; }
}

public class TestIgnoreAttribute : Attribute
{
}
rpgmaker commented 6 years ago

If the solution satisfy your question. Please help me close the issue.

rpgmaker commented 6 years ago

The documentation is here: https://github.com/rpgmaker/NetJSON/wiki/Quick-Guide

NetJSON.CanSerialize

rosostolato commented 6 years ago

Great, I used the same JsonIgnore as attribute and now it ignores on serialization. But, I guess that was not my problem at all, it still throws exception.

Take a look at my models. Maybe it's not working with Icollections: image

I had a exception when using Newtonsoft Json too, but it was resolved with JsonIgnore. And JsonIgnore is working here, I tested ignoring the Icollection in the first class and all worked. But with these collections it's not.

rpgmaker commented 6 years ago

ICollection should work. Are you using the nuget package? If so, you might want to use the latest from the repo. I am waiting for a change to push the latest changes to nuget.

Thanks,

rosostolato commented 6 years ago

yes, I'm using from nuget

rosostolato commented 6 years ago

I tested now using just the first 2 classes and it's still getting exception.

image

Ignoring all collection it works

rpgmaker commented 6 years ago

Are you testing using the latest code from the source code repo and not nuget? The latest code should work.

Thanks,

rosostolato commented 6 years ago

Ok, I'm downloading the last code from repo

rosostolato commented 6 years ago

Great! With the last repo it works!! Thank you a lot!

rpgmaker commented 6 years ago

The latest code is now available on nuget: https://www.nuget.org/packages/NetJSON/