Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# string interpolation-escaping double quotes and curly braces

guys,

I'm building a JSON object from an interpolated string, and not getting how escaping works. I have to use double quotes for the API.

This does not interpolate the expressions between the curly braces:

@"{{
                        ""name"":""{taskName}"", 
                        ""products"": [    
                                    {""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]}, 
                                    {""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}
                                  ], 
                        ""recurring"":true,
                        ""query"":    {
                                        ""date_from"": ""{dateFromString}"",
                                        ""date_to"": ""{dateToString}"",
                                        ""aoi"": {polygon}
                                    },
                        ""aoi_coverage_percentage"":90
                        }}";

This throws a bunch of errors-apparently, the curly brackets are not being escaped properly:

$"{{
                        ""name"":""{taskName}"", 
                        ""products"": [    
                                    {""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]}, 
                                    {""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}
                                  ], 
                        ""recurring"":true,
                        ""query"":    {
                                        ""date_from"": ""{dateFromString}"",
                                        ""date_to"": ""{dateToString}"",
                                        ""aoi"": {polygon}
                                    },
                        ""aoi_coverage_percentage"":90
                        }}";

How should I format it in order to preserve the internal double quotes and outer brackets while allowing for the values inside the single brackets to be interpolated?

like image 243
Boris K Avatar asked Apr 26 '17 13:04

Boris K


2 Answers

It seems that you have missed escape for the products and query objects:

$@"{{
    ""name"":""{taskName}"",
    ""products"": [
        {{""product"": ""ndvi_image"", ""actions"": [""mapbox"", ""processed""]}},
        {{""product"": ""true_color"", ""actions"": [""mapbox"", ""processed""]}}
    ],
    ""recurring"":true,
    ""query"": {{
        ""date_from"": ""{dateFromString}"",
        ""date_to"": ""{dateToString}"",
        ""aoi"": {polygon}
    }},
    ""aoi_coverage_percentage"":90
}}";
like image 139
Uladzimir Palekh Avatar answered Oct 22 '22 15:10

Uladzimir Palekh


Just in case someone else is considering doing the same, it would be better to create an anonymous type and serialize it to json for two reasons:

  1. it's a lot more readable and maintainable (how long would it take someone to change the code because the json structure has changed while keeping all escapes in order -- especially if there are no unit tests?)
  2. it's a lot more reliable (what if taskName has a double quote?)

Below uses json.net for serialization.

var jsonObj = new {
  name = taskName,
  products = new[] {
    new { product = "ndvi_image", actions = new [] { new { mapbox = "processed" } },
    new { product = "true_color", actions = new [] { new { mapbox = "processed" } }
  },
  recurring = true,
  query = new {
    date_from = dateFromString,
    date_to = dateToString,
    aoi = polygon
  },
  aoi_coverage_percentage = 90
};

var jsonString = JsonConvert.SerializeObject(jsonObj);
like image 28
Igor Pashchuk Avatar answered Oct 22 '22 16:10

Igor Pashchuk