I have a use case to store dynamic properties of a class to a document in MongoDB.
Here is my Metadata class:
public class Metadata
{
[JsonProperty("name")]
[BsonElement("name")]
public string Name { get; set; }
/// <summary>
/// Properties of element
/// </summary>
[JsonProperty("properties")]
[BsonElement("properties")]
public Dictionary<string, object> Properties { get; set; }
}
I want to read and format JSON correctly when the object for Properties is string/object/array. It is currently working with all types except for collections. Sample input:
"properties": {
"description": "How many developers are in your team?",
"questionType": "singleChoice",
"choices": [
{
"label": "<50",
"value": 0
},
{
"label": ">=50",
"value": 1
}
]
}
While deserializing the choices
array, JSONConvert is considering it as an object as shown in the below image:
[Note the braces highlighted by the yellow lines]
This is causing a problem when storing the object in Mongo collection and retrieving.
How do I accept any type of value as value in my 'Properties' dictionary?
I am letting the controller do the de-serialization normally from the request body. I am guessing it is equivalent to:
JsonConvert.DeserializeObject<Dictionary<string, object>>(requestBody);