C
C#2y ago
Taylor

Unable to use properties of type "object" on model objects. Json serialization is seriously invalid.

The following code is used as a model.
public class JsonDocument
{
public Guid? Guid { get; set; }
public string AppName { get; set; } = "ApiCore";
public Object? Data { get; set; }
}
public class JsonDocument
{
public Guid? Guid { get; set; }
public string AppName { get; set; } = "ApiCore";
public Object? Data { get; set; }
}
When it is passed into the controller method:
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
public virtual async Task<ActionResult<Guid>> Create([FromBody] JsonDocument model)
{
// At this point, model.Data says something about ValueKind. What?? Why?
....
}
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
public virtual async Task<ActionResult<Guid>> Create([FromBody] JsonDocument model)
{
// At this point, model.Data says something about ValueKind. What?? Why?
....
}
14 Replies
Taylor
TaylorOP2y ago
I JUST want proper json to work. Arbitrary object goes in. Arbitrary object comes out. It shouldn't be so difficult! I'm frustrated on this. This has taken WAY more hours than it should have. Wtf is valuekind
ChucklesTheBeard
ValueKind is from System.Text.Json; there's something in the JsonDocument that acts as a raw string, as opposed to a number, or another Json object that requires further handling other things it could be: https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonvaluekind?view=net-8.0
Taylor
TaylorOP2y ago
Is there a way to make this not so difficult? Rather than save objects into the DB as json it is instead storing ValueKind 2. Which is not at all what ti should be storing I'm so sick of system.text.json right now.
ChucklesTheBeard
if you want to store an Object? ? ...not really If you have something more specific than "literally anything that can be serialized into json" you can (somewhat) avoid dealing with the serializer directly, but if you need it to be that generic... you get to deal with it
Taylor
TaylorOP2y ago
How extremely frustrating. I wasted 7 hours on this. So how are we supposed to handle "just store your own json blob" use cases then? This is a huge miss.
jcotton42
jcotton422y ago
@Tom (Taylor) can you show what the JSON being submitted to the endpoint looks like?
Taylor
TaylorOP2y ago
{
"guid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"appName": "string",
"data": ["please", "work"]
}
{
"guid": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"appName": "string",
"data": ["please", "work"]
}
@jcotton42
jcotton42
jcotton422y ago
is data always going to be that shape (that is, always a string list)? or can it be any shape?
Taylor
TaylorOP2y ago
Any shape. It's literally an endpoint for arbitrary json data storage. So many posts online say "why don't you just account for the docs you know will exist?" drives me bonkers...
jcotton42
jcotton422y ago
make Data a JsonElement instead of Object
jcotton42
jcotton422y ago
(which is what it's already using anyhow afaict)
jcotton42
jcotton422y ago
also, avoid naming your own type JsonDocument if reasonably possible how are you persisting this to the database?
Taylor
TaylorOP2y ago
{"Operation is not valid due to the current state of the object."} I;'ll join chat hang on MYSQL thoughg
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server