C
C#2y ago
Hugh

✅ Extracting an object from a Utf8JsonReader

I'm currently writing my own JsonConverter, and I've got the writer bit working fine. (simplified example below)
public override void Write(Utf8JsonWriter writer, MyClass value, JsonSerializerOptions options)
{
writer.WriteString("name", value.Name);
writer.WriteString("value", value.Value);
}
public override void Write(Utf8JsonWriter writer, MyClass value, JsonSerializerOptions options)
{
writer.WriteString("name", value.Name);
writer.WriteString("value", value.Value);
}
However, I'm struggling a bit to do the opposite of this in Read() I have the Utf8JsonReader object in Read(), but the only thing that I can figure out is how to get a direct value from the reader, and can't get values by key from it. What am I missing here? Thanks
4 Replies
Hugh
Hugh2y ago
I think I've figured this one out. It's a little less straight-forward than I'd hoped... I ended up with this:
public override MyClass? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if(reader.TokenType != JsonTokenType.StartObject)
{
return null;
}

reader.Read();

string propertyName?;
string propertyValue?;

while(reader.TokenType != JsonTokenType.EndObject)
{
string jsonPropertyName = reader.GetString() ?? "";
reader.Read();
string jsonPropertyValue = reader.GetString() ?? "";
reader.Read();

switch(jsonPropertyName)
{
case "name":
propertyName = jsonPropertyValue;
break;
case "value":
propertyValue = jsonPropertyValue;
break;
}
}

if(propertyName is not null && propertyValue is not null)
{
return new MyClass(propertyName, propertyValue);
}

return null;
}
public override MyClass? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if(reader.TokenType != JsonTokenType.StartObject)
{
return null;
}

reader.Read();

string propertyName?;
string propertyValue?;

while(reader.TokenType != JsonTokenType.EndObject)
{
string jsonPropertyName = reader.GetString() ?? "";
reader.Read();
string jsonPropertyValue = reader.GetString() ?? "";
reader.Read();

switch(jsonPropertyName)
{
case "name":
propertyName = jsonPropertyValue;
break;
case "value":
propertyValue = jsonPropertyValue;
break;
}
}

if(propertyName is not null && propertyValue is not null)
{
return new MyClass(propertyName, propertyValue);
}

return null;
}
If I'm over-complicating this, I'd appreciate any pointers here
canton7
canton72y ago
Looks roughly equivalent to the example here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/converters-how-to?pivots=dotnet-7-0#sample-factory-pattern-converter (you're not using a factory of course, but I don't think you need to)
How to write custom converters for JSON serialization - .NET
Learn how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace.
canton7
canton72y ago
Although your serializer doesn't write a { but your deserializer expects one...
Hugh
Hugh2y ago
Thanks - I'll have a look at that page
Want results from more Discord servers?
Add your server
More Posts