Reputation: 29836
I have a Post function which recieves a JObject(Newtonsoft Json) as a post varible.
Now I need this to be a JObject since I have the "real type" based on other information and I need extra flexibilty(I cant use generics and other options).
Now I'm using this code:
JObject data; // this is assigned with the data from the post.
Type type = getTypeFromSomeWhere(param1);
object obj = data.ToObject(type); // I need this since I reflect the object later on.
My Input data looks like this:
{
"Disclaimer": {},
"Name": {
"IsRelevant": true,
"Value": "Sample Name"
},
}
I'm trying to convert the object into this Type:
public class MyEntity
{
public string Disclaimer{ get; set; }
public Field<string> Name{ get; set; } // Field has Value\IsRelevant and other stuff.
}
I'm getting this exception:
{"Error reading string. Unexpected token: StartObject. Path 'Disclaimer'."}
I'm trying to understand why it happens. The object looks ok. I guess it's due to the empty object Disclaimer, but I need to support those situations.
Edit:
When I insert a string into the Disclaimer then everything works.
How can I tell him to insert "null" into empty objects?
Upvotes: 2
Views: 4403
Reputation: 101150
Your mapping is incorrect. Disclaimer is an object since it's defined as {}
.
{
"Disclaimer": {}, //this is an OBJECT
"Name": {
"IsRelevant": true,
"Value": "Sample Name"
},
}
That's why you get the error {"Error reading string. Unexpected token: StartObject. Path 'Disclaimer'."}
as JSON.NET finds the {
and you are trying to map it to a string.
As for Name
it's also an object. I don't know how your Field<T>
looks like, but as long as it have a Value
property it should work.
A correct model:
public class Disclaimer
{
}
public class MyEntity
{
public string Disclaimer{ get; set; }
public Field<string> Name { get; set; }
}
Upvotes: 3