C# JsonConverter sometimes loses reference to original instance

79 Views Asked by At

I'm using custom JsonConverters with RavenDB in order to write/read references when saving/retrieving. In addition, the entity I'm working on it's a nested structure that starts populating itself using these references, calling the RavenBroker again, and executing the JsonConverter again and so on..

That is why when I create my JsonConverter instance I send the broker's instance as a parameter

public AbstractRuleBroker()
        {
            this.CustomConverters = new List<AbstractConverter<AbstractRule>> { new RuleSetConverter( this ) };
        }

JsonConverter construction

And here you can see how I use that broker to find those references when an object is being deserialized JsonConverter ReadJson method

Because of the business logic, I need a specific value from the original invocation that I'm storing in the broker's property.

The problem is that for some executions (same method, same parameters, I'm calling the API using postman) that property gets there as null, and sometimes it has the value is supposed to have.

Here's the place where I assign that Value FilteredVersion assignment

First invocation, everything is fine.. The value is there.. 1st invocation debug results

Second invocation, null.. 2nd invocation debug results

What I could find out is that when I'm debugging, in those cases where the value is null, executing GetHashCode() method from the Broker's perspective returns different memory addresses. This made me think if there's something I'm not setting correctly in the serializer when I add my custom converter. (maybe ContractResolver?)

this.Connection.Conventions.CustomizeJsonSerializer = serializer =>
                {
                    serializer.TypeNameHandling = TypeNameHandling.Auto;
                    if( !( converters is null ) )
                    {
                        GetCustomConverters().ForEach( c => serializer.Converters.Add( c ) );    
                    }
                };

Thanks!

0

There are 0 best solutions below