[CSHARP-1122] Add support for parsing JSON strings with placeholders Created: 20/Nov/14  Updated: 09/Nov/22  Resolved: 09/Nov/22

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Robert Stam Assignee: Unassigned
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

It is often useful to represent a BSON document as a JSON string, if for no other reason than it is usually easier to construct a string value representing a complex document than to directly construct the BsonDocument itself.

However, if the document is not a constant, then we need some way to inject values into it.

For example, rather than:

var name = "John";
var age = 11;
var document = Json.Parse(string.Format("{{ name : \"{0}\", age: {1} }}", name, age));

It would be much nicer to write:

var document = Json.Parse("{ name : #, age : # }", name, age);

Or something similar to that (the exact syntax needs to be determined).

Not only would this be much friendlier than formatting up strings with values inside it, it would also avoid all sorts of dangers that might crop up if the values inserted into the formatted string aren't properly formatted or escaped.



 Comments   
Comment by James Kovacs [ 09/Nov/22 ]

No longer needed since C# added string interpolation. Now you can write:

var document = Json.Parse($"{{ name : {name}, age : {age} }}");

Generated at Wed Feb 07 21:38:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.