[CSHARP-4573] Serializing a PROCO containing a JsonObject fails. Created: 17/Mar/23 Updated: 27/Oct/23 Resolved: 17/Mar/23 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 2.19.0 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Unknown |
| Reporter: | Sam Stonew | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Documentation Changes Summary: | 1. What would you like to communicate to the user about this feature? |
| Description |
| Comments |
| Comment by Robert Stam [ 17/Mar/23 ] | ||
|
The way our POCO mapping works is that when serializing a POCO we recursively serialize each of its properties (or public fields). For this to work there can't be any circular references, or the serialization gets into an infinite loop. We throw an exception when the depth gets too deep to prevent infinite loops. Unfortunately for your use case, the Json.NET POCOs contain infinite loops. For example, the Parent property points to the parent, and when we attempt to serialize the Parent one of its properties is the node you started from, at which point the same Parent will get serialized again, which is the cause of the infinite loop. There is nothing we can do on our side to handle this. The easiest work around is what you already suggested, which is using Json.NET to serialize the JsonObject to a string, after which you can either work with the string or Parse it into something else. Alternatively, you could write custom serializers for Json.Net objects. It's possible that instead of writing custom serializers you could create custom class maps and register those (which might allow the standard BsonClassMapSerializer to work without getting into an infinite loop), but I haven't actually tried that. I'm sorry I don't have better news for you, but we just can't support this scenario out of the box. | ||
| Comment by Sam Stonew [ 17/Mar/23 ] | ||
|
The Error:
|