System.Text.Json JsonConverter<T> Throwing ArgumentExeception and TargetInvocationException

I am having a strange issue where my System.Text.Json.Serialization.JsonConverter<JsonEntries> is throwinng two exceptions: System.ArgumentException with message:
Value does not fall within the expected range.
and System.Reflection.TargetInvocationException with message:
Exception has been thrown by the target of an invocation.
here is the stack trace and example converter: https://gist.github.com/thakyZ/c29ad07ed0eaa57b1d832305a2f55904 using .net 9 version 9.0.6 using System.Text.Json version 9.0.6 it would appear it is being thrown when the JsonSeralizer is trying to create an instance of the JsonConverter via Activate.CreateInstance
7 Replies
Andrew
Andrew3mo ago
what is JsonEntries?
Neko Boi Nick
Neko Boi NickOP3mo ago
it's a class derived from ObservableCollection
Andrew
Andrew3mo ago
my guess is the ArgumentException is coming from that. The JsonConverter ctor tries to create a default value https://source.dot.net/#System.Text.Json/System/Text/Json/Serialization/JsonConverterOfT.cs,33 If you can attach a debugger you probably can catch exactly where the exception is thrown
Neko Boi Nick
Neko Boi NickOP3mo ago
doing that, it throws at the point i mentioned https://source.dot.net/#System.Text.Json/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Converters.cs,198 it doesn't allow for me to check the value of converterAttribute, converterType, converter, ctor and declaringType when debugging, so i turn off managed optimizations and restart the debugger, but upon landing at that point, memberInfo and converter(obviously) are the only two null values. but still is thowing the exception(s)...
Andrew
Andrew3mo ago
without more code I can't help beyond guessing. The exception indicates that some value is being validated to fall within a range (similar to numeric validation). Null could be a problem but that may not be the only problem
Neko Boi Nick
Neko Boi NickOP3mo ago
okay i will look into it a bit more, and then update this thread and the gist if i end up not being able to figure it out okay figured it out, still had the Krafs.Publicizer dependency, which i had used for some testing, but all the publicized members were commented out in the project file, and apparently it doesn't like that. thank for trying to help though!
Andrew
Andrew3mo ago
glad you figured it out!

Did you find this page helpful?