Skip to content

Fix Dictionary serialization in JSON sink for MongoDB nested documents#225

Merged
philnach merged 2 commits into
mainfrom
copilot/fix-nested-elements-json-sink
Dec 21, 2025
Merged

Fix Dictionary serialization in JSON sink for MongoDB nested documents#225
philnach merged 2 commits into
mainfrom
copilot/fix-nested-elements-json-sink

Conversation

Copilot AI commented Dec 20, 2025

Copy link
Copy Markdown
Contributor

MongoDB's BsonTypeMapper.MapToDotNetValue() converts nested BsonDocuments to Dictionary<string, object?>, which were serialized as "System.Collections.Generic.Dictionary2[System.String,System.Object]"` instead of proper JSON objects.

Changes

DataItemJsonConverter.cs

  • Added IDictionary<string, object?> check before fallback to .ToString()
  • Converts dictionaries to DictionaryDataItem for proper nested object serialization
  • Handles both field-level dictionaries and dictionaries within arrays

Tests

  • Unit tests for dictionary field values and arrays of dictionaries
  • Integration test validating complete MongoDB export scenario

Example

Before:

{
  "_id": "some_id",
  "content": ["System.Collections.Generic.Dictionary`2[System.String,System.Object]"]
}

After:

{
  "_id": {"$oid": "some_id"},
  "content": [{"text": "a message text", "type": "text"}]
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Nested elements not supported in JSON Sink</issue_title>
<issue_description>Hi

I am trying to eksport from MongoDB to JSON.

An element like the following:

{
  "_id": {
    "$oid": "some_id"
  },
  "thread_id": "thread_id",
  "content": [
    {
      "text": "a message text",
      "type": "text"
    }
  ],
  "role": "user"
}

Is in JSON exported as:

{
    "_id": "some_id",
    "thread_id": "thread_id",
    "content": [
        "System.Collections.Generic.Dictionary`2[System.String,System.Object]"
    ],
    "role": "user"
}

So while "content" is perserved as a list, its contents are gone.

</issue_description>

Comments on the Issue (you are @copilot in this section)

@philnach I was observing something similar. assigning to myself for investigation.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…ring, object?> as nested objects

Co-authored-by: markjbrown <800166+markjbrown@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix nested elements not supported in JSON Sink Fix Dictionary serialization in JSON sink for MongoDB nested documents Dec 20, 2025
Copilot AI requested a review from markjbrown December 20, 2025 00:29
@markjbrown markjbrown marked this pull request as ready for review December 20, 2025 00:46
@philnach philnach self-requested a review December 21, 2025 13:54
@philnach philnach merged commit dd51ed3 into main Dec 21, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested elements not supported in JSON Sink

3 participants