JSON serialization without preserving data type information

Hej hoi!!

While serializing a VL object which is using an interface, or using a spread of object, the json serializer preserves the data type information. There is a $type in there which is a problem for a 3rd party service to parse it!

Is there a way to remove that $type during serialization?

merci merci!!

What I want:

Summary

{
“input”: [
{
“content”: [
{“text”: “writeyourtexthere”,
“type”: “input_text”
},
{
“image_url”: “data:image/png;base64,placeyourbase64imagehere”,
“type”: “input_image”
}
],
“role”: “user”
}
],
“model”: “model”
}

What I end up with:

{
“input”: [
{
“content”: [
{
“$type”: “cleanSerializasion.Main.TextContent_R, cleanSerializasion.vl.V2mzJi07XUkPIqNwCrateh, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”,
“text”: “writeyourtexthere”,
“type”: “input_text”
},
{
“$type”: “cleanSerializasion.Main.ImageContent_R, cleanSerializasion.vl.V2mzJi07XUkPIqNwCrateh, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null”,
“image_url”: “data:image/png;base64,placeyourbase64imagehere”,
“type”: “input_image”
}
],
“role”: “user”
}
],
“model”: “model”
}

JsonSerializasionTypeInfo.vl (49.6 KB)

There’s no such switch / option in message pack to change that to my knowledge. So here are your options:
A) Write your own serializer.
B) Get rid of the IContent/TextContent/ImageContent and use one Content record with the fields as you outlined.
C) Do some post-string processing or JSON processing and remove that line/JSON property

Thanks very much @Elias

Thinking of the option B; having one Content record would be the best, indeed.
However, although the types are all string, for the serialization, once I would need a pad called “text“ and once “image_url“.

I was wondering if there is a way to do something similar:

public class Content
{
[JsonPropertyName(“image_url”)]
public string Content { get; set; }
public string Type { get; set;}
}

Two pads, one named “image_url” one “text”. In the serialization you’ll probably see both with one of them empty.
Having an attribute to have a different name for the pads in json would be nice indeed. To my understanding the json serializer internally uses message pack, not sure that one supports that.

1 Like