-1

I need to apply a custom converter conditionally based on the depth of the reader. The root of the json object is a Def class that should deserialize like normal, however any Defs within the object should be resolved to a reference to that deserialized Def. My plan is to check the depth of the reader, and if we're not at the root, then create a skeleton Def and add it to a list to be resolved later once we've deserialized all the Defs.

public class DefConverter : JsonConverter {
    public override bool CanConvert(Type objectType) {
        return objectType == typeof(Def);
    }

    public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer) {
        if (reader.Depth == 0) {
            // Use the default serializer to read the Def
            return serializer.Deserialize(reader, objectType); // ?
        }
        
        // Create skeleton Def with id
        // Add to list of defs to be resolved later
    }
    
    public override bool CanWrite => false;
    public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) {
        throw new NotImplementedException();
    }
}

The issue I'm running into is that there doesn't seem to be a way to call the Json.NET default converter, using serializer.Deserialize(reader, objectType) will just cause an infinite loop as it just calls the custom converter.

Joe
  • 1
  • 2
    Show an example JSON please. – Good Night Nerd Pride Feb 16 '23 at 09:25
  • Have you seen [this](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/preserve-references) yet? – Good Night Nerd Pride Feb 16 '23 at 09:27
  • Instead of using the `serializer` passed to `ReadJson()` (which is configured to use your `DefConerter`), create a new `JsonSerializer` and use that one for depth `0`. – Good Night Nerd Pride Feb 16 '23 at 09:34
  • *there doesn't seem to be a way to call the Json.NET default converter, using `serializer.Deserialize(reader, objectType)` will just cause an infinite loop as it just calls the custom converter.* -- then does [JSON.Net throws StackOverflowException when using `[JsonConvert()]`](https://stackoverflow.com/q/29719509/3744182) answer your question? – dbc Feb 16 '23 at 14:20
  • Also possibly related: [Json.NET serialize by depth and attribute](https://stackoverflow.com/q/36159424/3744182). – dbc Feb 16 '23 at 21:13
  • Does this answer your question? [Json.NET Recursive Serialisation: Custom converter attribute seems to be being ignored](https://stackoverflow.com/questions/51426834/json-net-recursive-serialisation-custom-converter-attribute-seems-to-be-being-i) – Dour High Arch Feb 21 '23 at 15:12

1 Answers1

0

I managed to get it working using the solution here:

Json.NET Recursive Serialisation: Custom converter attribute seems to be being ignored

Toggling the converter off and on using the CanRead getter

Joe
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '23 at 18:26