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
JsonElement
comes with an ElementType
(or something of the sort) property
You'll want to check that, and then use the GetX
methods accordinglyI don’t know what’s inside that JSON
That’s the issue
It’s an unpredictable object
What do you mean with unpredictable?
The JsonElement will still know
Ero#1111
REPL Result: Success
Console Output
Compile: 728.328ms | Execution: 81.792ms | React with ❌ to remove this embed.
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?
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
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
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
Nope
I can't ever know what's inside data
then how do you expect to use it?
I'm not using it
then why do you care about it?
I'm persisting it in a MSSQL database then giving it back whenever someone asks me to
right
this seems at least somewhat like an xy problem
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
Ero#1111
REPL Result: Success
Console Output
Compile: 772.990ms | Execution: 134.501ms | React with ❌ to remove this embed.
hmm
hm?
mh
Why do I have a different output 👀
iunno
share your code