C
C#14mo ago
Noriega

✅ Newtonsoft serializing returns nothing with custom converter

So I have a custom JsonConverter from newtonsoft that is supposed to replace object properties that are a specific type. For context, I'm looking for a type called IOptional that has properties HasValue and Value only if HasValue is true. If HasValue is true, the converter is supposed to replace the json property with Value. Otherwise, it replaces to null. I have this
file class EvalResultConverter : JsonConverter
{
public override bool CanRead => false;
public override bool CanWrite => true;

public override bool CanConvert(Type objectType) => true;

public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
=> throw new NotImplementedException();

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var token = JToken.FromObject(value!);

if (token.Type is JTokenType.Object)
{
var tokenObject = (JObject)token;
foreach (var property in tokenObject.Properties())
{
if (property.Value.Type is JTokenType.Object && typeof(IOptional).IsAssignableFrom(value!.GetType().GetProperty(property.Name)!.PropertyType))
{
var propertyObject = (JObject)property.Value;
tokenObject[property.Name] = propertyObject["HasValue"]!.Value<bool>() ? propertyObject["Value"] : null;
}
}

tokenObject.WriteTo(writer);
}
else token.WriteTo(writer);
}
}
file class EvalResultConverter : JsonConverter
{
public override bool CanRead => false;
public override bool CanWrite => true;

public override bool CanConvert(Type objectType) => true;

public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
=> throw new NotImplementedException();

public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
var token = JToken.FromObject(value!);

if (token.Type is JTokenType.Object)
{
var tokenObject = (JObject)token;
foreach (var property in tokenObject.Properties())
{
if (property.Value.Type is JTokenType.Object && typeof(IOptional).IsAssignableFrom(value!.GetType().GetProperty(property.Name)!.PropertyType))
{
var propertyObject = (JObject)property.Value;
tokenObject[property.Name] = propertyObject["HasValue"]!.Value<bool>() ? propertyObject["Value"] : null;
}
}

tokenObject.WriteTo(writer);
}
else token.WriteTo(writer);
}
}
So just to be clear, any other token types works fine along with any objects that don't have IOptional properties. But when I try this with types that do, serialization returns nothing. Not sure if im incorrectly writing to the jsonwriter, but its been annoying for a few days
6 Replies
Noriega
NoriegaOP14mo ago
Additionally, I can replicate this with a fiddle
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
TheBoxyBear
TheBoxyBear14mo ago
You look for properties assignable to IOptionalbut the Optional class doesn't implement that interface. The design pattern also seems curious and perhaps outdated with nullable reference types. I'd love to know more about the reasoning
Noriega
NoriegaOP14mo ago
The IOptional interface is from an entire different library and the one im replicating is just a quick example also in the fiddle, the type im checking for is the Optional class I have there
Noriega
NoriegaOP14mo ago
so in my project, I do have objects im testing that actually have properties with this interface
GitHub
Remora.Rest/Remora.Rest.Core/IOptional.cs at main · Remora/Remora.R...
Reusable tooling for interacting with JSON-driven REST APIs - Remora/Remora.Rest
Noriega
NoriegaOP14mo ago
Bump up
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server