✅ Can the NewtonSoft JSON package conditionally create classes when parse?

I have a situation where a field in a class will sometimes have a string, but other times have a class instance. Is there a way to have the JSON serializer wrap the string in something so the field doesn't have to be just object? It might work out that the string is just a shorthand way of saying "default all other fields in the class instance."
14 Replies
TheRanger
TheRanger9mo ago
something like what?
Will Pittenger
Will Pittenger9mo ago
Well, examine this:
[
{
"t": "ss"
},
{
"t":
{
"wrapped": "value",
"some other value": "value2"
}
}
]
[
{
"t": "ss"
},
{
"t":
{
"wrapped": "value",
"some other value": "value2"
}
}
]
In the first entry, some other value is defaulted to some value.
TheRanger
TheRanger9mo ago
yeah so you want it to create an instance of a class when its not a string, right? you can do that
Will Pittenger
Will Pittenger9mo ago
I want to create the class instance even when it is a string.
TheRanger
TheRanger9mo ago
when is what a string, the field? or the value in the json?
Will Pittenger
Will Pittenger9mo ago
If in the JSON t is a string, wrap the string in the class.
TheRanger
TheRanger9mo ago
ok yeah u can do that look up JsonConverter Attribute
Will Pittenger
Will Pittenger9mo ago
So that array would be as though the following was in the JSON.
[
{
"t":
{
"wrapped": "ss",
"some other value": "default"
},
{
"t":
{
"wrapped": "value",
"some other value": "value2"
}
}
]
[
{
"t":
{
"wrapped": "ss",
"some other value": "default"
},
{
"t":
{
"wrapped": "value",
"some other value": "value2"
}
}
]
TheRanger
TheRanger9mo ago
https://www.newtonsoft.com/json/help/html/CustomJsonConverter.htm or the generic version is probably better https://www.newtonsoft.com/json/help/html/CustomJsonConverterGeneric.htm alternatively u can define an explicit operator Bar that takes a string newton can detect it and convert the string into bar
Will Pittenger
Will Pittenger9mo ago
I didn't know if the parser would observe that.
TheRanger
TheRanger9mo ago
the explicit operator choice looks easier
Will Pittenger
Will Pittenger9mo ago
Well, the operator won't help. The instance in the array needs a parent back to the owner. It also be more like this:
[
"ss",
{
"wrapped": "value",
"some other value": "value2"
}
]
[
"ss",
{
"wrapped": "value",
"some other value": "value2"
}
]
That'd produce the same result as:
[
{
"wrapped": "ss",
"some other value": "default"
},
{
"wrapped": "value",
"some other value": "value2"
}
]
[
{
"wrapped": "ss",
"some other value": "default"
},
{
"wrapped": "value",
"some other value": "value2"
}
]
Will Pittenger
Will Pittenger9mo ago
Discord
Discord - A New Way to Chat with Friends & Communities
Discord is the easiest way to communicate over voice, video, and text. Chat, hang out, and stay close with your friends and communities.
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.