C
C#17mo ago
Allan

Serialize a JsonDocument/JsonElement with STJ

I have a class with an object property. When deserializing something into this class, it deserializes it as a JsonElement. When trying to serialize it back to raw JSON, it will serialize the actual properties of JsonElement or JsonDocument... Any idea?
22 Replies
ero
ero17mo ago
JsonElement comes with an ElementType (or something of the sort) property You'll want to check that, and then use the GetX methods accordingly
Allan
Allan17mo ago
I don’t know what’s inside that JSON That’s the issue It’s an unpredictable object
Esa
Esa17mo ago
What do you mean with unpredictable?
ero
ero17mo ago
The JsonElement will still know
MODiX
MODiX17mo ago
Ero#1111
REPL Result: Success
using System.Text.Json;

var json1 = """
{
"Bar": 123
}
""";

var json2 = """
{
"Bar": "Qux"
}
""";

var foo1 = JsonSerializer.Deserialize<Foo>(json1);
var foo2 = JsonSerializer.Deserialize<Foo>(json2);

var je1 = (JsonElement)foo1.Bar;
var je2 = (JsonElement)foo2.Bar;

Console.WriteLine((je1.ValueKind, je2.ValueKind));

record Foo(
object Bar);
using System.Text.Json;

var json1 = """
{
"Bar": 123
}
""";

var json2 = """
{
"Bar": "Qux"
}
""";

var foo1 = JsonSerializer.Deserialize<Foo>(json1);
var foo2 = JsonSerializer.Deserialize<Foo>(json2);

var je1 = (JsonElement)foo1.Bar;
var je2 = (JsonElement)foo2.Bar;

Console.WriteLine((je1.ValueKind, je2.ValueKind));

record Foo(
object Bar);
Console Output
(Number, String)
(Number, String)
Compile: 728.328ms | Execution: 81.792ms | React with ❌ to remove this embed.
Allan
Allan17mo ago
That means I have to reinvent the wheel just because it’s not a concrete type? Cause right I have that cool JsonElement but using Serialize on foo1 or foo2 won’t serialize it back properly And so should I make a converter for this?
ero
ero17mo ago
you're forced to use a converter, yes if it truly is an object and not a concrete type, then you just got unlucky i guess get mad at the people who provide the json data
Allan
Allan17mo ago
Yeah... The data come from some JavaScript library, they do clown things there Thank you then, I'll roll a converter for this. I'm pretty sure a dynamic converter for this could be built in in the library though... I don't think I ever had that kind of issues with Json.Net
ero
ero17mo ago
do you ever know the type upfront? like if you for example get the data from this one endpoint, it'll be that type, if you get it from another endpoint, it'll be another type
Allan
Allan17mo ago
Nope
{
width,
height,
...,
data : { ??? }
}
{
width,
height,
...,
data : { ??? }
}
I can't ever know what's inside data
ero
ero17mo ago
then how do you expect to use it?
Allan
Allan17mo ago
I'm not using it
ero
ero17mo ago
then why do you care about it?
Allan
Allan17mo ago
I'm persisting it in a MSSQL database then giving it back whenever someone asks me to
ero
ero17mo ago
right this seems at least somewhat like an xy problem
Allan
Allan17mo ago
Could be yes 🙄 The converter is gonna be fine I guess anyway, I just wanted not to do one if there's a built-in solution to the issue
MODiX
MODiX17mo ago
Ero#1111
REPL Result: Success
using System.Text.Json;

var json = """
{
"Bar": {
"Qux": "Quo"
}
}
""";

var foo = JsonSerializer.Deserialize<Foo>(json);

Console.WriteLine(JsonSerializer.Serialize(foo));

record Foo(
object Bar);
using System.Text.Json;

var json = """
{
"Bar": {
"Qux": "Quo"
}
}
""";

var foo = JsonSerializer.Deserialize<Foo>(json);

Console.WriteLine(JsonSerializer.Serialize(foo));

record Foo(
object Bar);
Console Output
{"Bar":{"Qux":"Quo"}}
{"Bar":{"Qux":"Quo"}}
Compile: 772.990ms | Execution: 134.501ms | React with ❌ to remove this embed.
ero
ero17mo ago
hmm hm?
Allan
Allan17mo ago
mh Why do I have a different output 👀
ero
ero17mo ago
iunno
ero
ero17mo ago
share your code
Want results from more Discord servers?
Add your server
More Posts