1

On the most recent version of JSON.NET for Unity3D I am receiving the following compiler warning message:

[CS0618] `Newtonsoft.Json.Serialization.DefaultContractResolver.DefaultMembersSearchFlags' is obsolete:
`DefaultMembersSearchFlags is obsolete. To modify the members serialized inherit from DefaultContractResolver and override the GetSerializableMembers method instead.'

The signature of the GetSerializableMembers method is:

protected virtual List<MemberInfo> GetSerializableMembers(Type objectType)

I'm very confused how to override this method to accomplish the same logical equivalent we used to have when simply setting the DefaultMembersSearchFlags. Our previous usage we need to move into an override of GetSerializableMembers was:

contractResolver = new DefaultContractResolver() {
  DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic
};

Does an example exist of a working override for this method? Google is failing me.

Here's what I've tried so far by reverse-engineering the current usage of DefaultMembersSearchFlags in DefaultContractResolver.cs:

private class ContractResolver : DefaultContractResolver {
    override protected List<MemberInfo> GetSerializableMembers(Type objectType) {
      BindingFlags DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
      return ReflectionUtils.GetFieldsAndProperties(objectType, DefaultMembersSearchFlags)
                            .Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList();
    }
}

Problem is... ReflectionUtils doesn't seem to be importable from the current JSON.NET package and rolling my own version of all that sounds like a rabbit trail when this should be 'easy'... right?

slumtrimpet
  • 3,159
  • 2
  • 31
  • 44
  • 1
    Why are you doing this? You don't need to use that. There is a [JsonUtility](http://docs.unity3d.com/ScriptReference/JsonUtility.html) which is built into Unity in 5.3....Example can be found [here](http://stackoverflow.com/a/36244111/3785314) – Programmer Aug 15 '16 at 21:16
  • Mainly because, as your link shows, JsonUtility is as light-weight a JSON parsing functionality as can be imagined and has none of the power of the full JSON.NET library. As soon as you need to do anything complex with JSON (ie.. like in my question), the limitations of Unity's JsonUtility become obvious. – slumtrimpet Aug 15 '16 at 21:24
  • Yes, yes there is a limitation for `JsonUtility` right now. I also want to let you know that I've seen instances where `JSON.NET` crash on iOS devices. `JsonUtility` has never crashed on anyone as far as I am concerned. What is it that you are doing that can only be done with `JSON.NET`? – Programmer Aug 15 '16 at 21:30
  • 1
    Without being too short but also not wanting to leave your comment unanswered, JSON.NET is consistently one of the top paid assets on the Unity Asset Store with hundreds of 5 star reviews. We've been using it for years on iOS with no issues at all. I'm really just looking for an answer to my question above as we are way beyond the point where debating usage of the library itself is a worthwhile discussion to have. – slumtrimpet Aug 15 '16 at 21:46

0 Answers0