C
C#2y 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
ero2y 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
AllanOP2y ago
I don’t know what’s inside that JSON That’s the issue It’s an unpredictable object
Esa
Esa2y ago
What do you mean with unpredictable?
ero
ero2y ago
The JsonElement will still know
MODiX
MODiX2y 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
AllanOP2y 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
ero2y 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
AllanOP2y 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
ero2y 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
AllanOP2y ago
Nope
{
width,
height,
...,
data : { ??? }
}
{
width,
height,
...,
data : { ??? }
}
I can't ever know what's inside data
ero
ero2y ago
then how do you expect to use it?
Allan
AllanOP2y ago
I'm not using it
ero
ero2y ago
then why do you care about it?
Allan
AllanOP2y ago
I'm persisting it in a MSSQL database then giving it back whenever someone asks me to
ero
ero2y ago
right this seems at least somewhat like an xy problem
Allan
AllanOP2y 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
MODiX2y 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
ero2y ago
hmm hm?
Allan
AllanOP2y ago
mh Why do I have a different output 👀
ero
ero2y ago
iunno
ero
ero2y ago
share your code
Want results from more Discord servers?
Add your server