Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:22:25+00:00 2026-06-14T23:22:25+00:00

The following class shall be received by an API as Json and stored in

  • 0

The following class shall be received by an API as Json and stored in MongoDB, using the C# Driver and Web API. The data property is unstructured, but I can restrict it to key-value pairs with possibly nested arrays in these values.

public class Something
{
    [BsonId, JsonIgnore]
    public ObjectId _id { get; set; }

    public IDictionary<string, object> data { get; set; }
}

When the json is posted from the client, Json.NET deserializes properly.

Saving the class to MongoDB, I get something like this in the database with the c# specific type:

{
    property1: 'one',
    property2: {
        _t: 'System.Collections.Generic.List`1[System.Object]'
        _v:
        [
            {}, {}, {}, ...
        ]
     }
}

Based on these sources, I have pulled together a CustomCreationConverter for Json.NET that nests a List into the value of the Dictionary:

Apply JsonDictionaryAttributes to properties

Json.NET: Deserializing nested dictionaries

CustomCreationConverter Source Code

public class Something
{
    ...
    [JsonProperty(ItemConverterType = typeof(CustomConverter))]
    public IDictionary<string, object> data { get; set; }
}

with this override:

public class CustomConverter : CustomCreationConverter<IList<object>>
{
    public override IList<object> Create(Type objectType)
    {
        return new List<object>();
    }

    public override bool CanConvert(Type objectType)
    {
        return true; // Just to keep it simple
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.StartArray)
            return base.ReadJson(reader, objectType, existingValue, serializer);

        return serializer.Deserialize(reader);
    }
}

This works actually fine, but I still get the construction with the c# specific types in MongoDB. How can I get this data into MongoDB without the type-value properties when nested?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:22:26+00:00Added an answer on June 14, 2026 at 11:22 pm

    So I have this working with Dictionary<> rather than IDictionary<> without an custom converters, but I’m also using my own types rather than just object. I’m not clear what you mean by “useful way”, but you can annotate your members with BsonDictionaryOptionsAttribute and pass in DictionaryRepresentation.Document or DictionaryRepresentation.ArrayOfDocument to change the shape persisted to MongoDB if that’s what you mean?

    Otherwise, what did you mean by “useful way” with regards to the way it’s structured now? You’ll alwways get the "_t" discriminator as long as there’s more than one type possible. I’m guessing that you’re seeing that because you’re using IDictionary<>.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined the following class using COM to use the IGroupPolicyObject using C#.NET:
Possible Duplicate: Why shall I use the “using” keyword to access my base class
Suppose I have the following class: using System; using System.Collections.Generic; using System.Linq; using System.Text;
I would like to create the following using class syntax: $resp = new stdclass;
Consider the following class member: std::vector<sim_mob::Lane *> IncomingLanes_; the above container shall store the
I am testing a small stub of Matcher and Pattern class...see the following small
For following class mapping: <class name=Person table=Person discriminator-value=null> <id name=ID column=ID > <generator class=identity/>
The following class acts as a simple cache that gets updated very infrequently (say
The following class is not thread-safe (as proven in Proving the following code not
The following class is used by another program. When it is accessed, it throws

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.