RBT
RBT

Reputation: 25897

How to use string interpolation and verbatim string together to create a JSON string literal?

I'm trying to create a string literal representing an array of JSON objects so I thought of using string interpolation feature as shown in the code below:

    public static void MyMethod(string abc, int pqr)
    {
        string p = $"[{{\"Key\":\"{abc}\",\"Value\": {pqr}  }}]";
    }

Now I thought of using verbatim string so that I don't have to escape double quotes using backslashes. So I came to know through this answer that verbatim string and string interpolation can be used together. So I changed my code as below:

public static void MyMethod(string abc, int pqr)
{
    string p = $@"[{{"Key":"{abc}","Value": {pqr} }}]";
}

But it fails to compile. Can anyone help me if there is anything wrong in my usage or it will not be possible to escape double quotes in such a case using string verbatim feature of C#?

Upvotes: 20

Views: 20050

Answers (5)

JJS
JJS

Reputation: 6658

C# 11, introduced a feature called "Raw String Literals" which makes this easy.

A raw string uses a pair of """ tokens to designate that everything between the is the string value. It ignores the leading indentation based on the indentation of the closing pair.

You can add interpolation of local variables using {symbol} with a single $ in front of the """.

If the string you want already uses { and } as significant values, you can use $$ which means that your interpolation markers are {{symbols}}.

var json = $$"""
    [
        {
            "Key":"{{abc}}",
            "Value": {{pqr}}
        }
    ]
    """;

Happy string templating...

Upvotes: 1

dbc
dbc

Reputation: 116741

While you can use string interpolation to manually create well-formed JSON, you should not. Instead, in .NET Core 3.1 and later, use the built-in System.Text.Json to generate JSON:

public static string MyMethod(string abc, int pqr) => 
    System.Text.Json.JsonSerializer.Serialize(new [] { new { Key = abc, Value = pqr } });

Demo .NET Core fiddle here.

In earlier versions use Json.NET or DataContractJsonSerializer or even JavaScriptSerializer as detailed in How do I turn a C# object into a JSON string in .NET?, e.g.:

public static partial class DataContractJsonSerializerHelper
{
    public static string ToJson<T>(this T obj, DataContractJsonSerializerSettings settings = null)
    {
        var serializer = new DataContractJsonSerializer(obj == null ? typeof(T) : obj.GetType(), settings);
        using (var memory = new MemoryStream())
        {
            serializer.WriteObject(memory, obj);
            memory.Seek(0, SeekOrigin.Begin);
            using (var reader = new StreamReader(memory))
                return reader.ReadToEnd();
        }
    }
}

public static string MyMethod(string abc, int pqr) => 
    new [] { new Dictionary<string, object>() { { "Key", abc }, { "Value", pqr } } }
        .ToJson(new DataContractJsonSerializerSettings { UseSimpleDictionaryFormat = true });

Demo fiddle #2 here.

That being said, if you do want to use string interpolation to manually emit JSON, you must correctly handle the following to avoid creating malformed JSON:

  • As noted in comments by Jeroen Mostert, C# verbatim string literals require embedded double quotes to be doubled up like so:

    $@"[{{""Key"":""{abc}"",""Value"": {pqr} }}]";
    

    Keep in mind that it's easy to confuse C# string literal escaping (which is evaluated at compile time) with JSON string escaping (which is required at runtime.) As you write your C# code you will likely need to be typing both.

  • JSON numeric values must be formatted using the invariant culture to ensure that the decimal separator and negative sign used conform to JSON RFC 8259, so you must use either FormattableString or String.Create(CultureInfo.InvariantCulture, DefaultInterpolatedStringHandler):

    String.Create(CultureInfo.InvariantCulture, 
                  $@"[{{""Key"":""{abc}"",""Value"": {pqr} }}]");
    

    See Culture-specific formatting for details.

  • As stated in RFC 8259, certain characters in JSON string literals must be escaped:

    All Unicode characters may be placed within the quotation marks, except for the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

    You will need to do this yourself to your abc string.

Putting all this together, you must still create a method for serializing a string to JSON such as the following:

public static class JsonExtensions
{
    static Dictionary<char, string> GetMandatoryEscapes()
    {
        // Standard escapes from https://www.json.org/json-en.html
        var fixedEscapes = new KeyValuePair<char, string> []
        {
            new('\\', "\\\\"), // Nust be first.
            new('"',  "\\\""), // This is correct, but System.Text.Json preferrs the longer "\\u0022" for security reasons.
            new('/',  "\\/"),
            new('\b', "\\b"),
            new('\f', "\\f"),
            new('\n', "\\n"),
            new('\r', "\\r"),
            new('\t', "\\t"),
        };
        // Required control escapes from https://www.rfc-editor.org/rfc/rfc8259#section-7
        var controlEscapes = Enumerable.Range(0, 0x1F + 1)
            .Select(i => (char)i)
            .Except(fixedEscapes.Select(p => p.Key))
            .Select(c => new KeyValuePair<char, string>(c, @"\u"+((int)c).ToString("X4")));
        return fixedEscapes.Concat(controlEscapes).ToDictionary(p => p.Key, p => p.Value);
    }
    
    static Dictionary<char, string> Escapes { get; } = GetMandatoryEscapes();
    
    public static string ToJson(this string s) => 
        s.Aggregate(new StringBuilder("\""), (sb, c) => Escapes.TryGetValue(c, out var s) ? sb.Append(s) : sb.Append(c)).Append("\"").ToString();
}

And now you can use string interpolation to make well-formed JSON as follows:

public static string MyMethod(string abc, int pqr) =>
    String.Create(CultureInfo.InvariantCulture, 
                  $@"[{{""Key"":{abc.ToJson()},""Value"":{pqr} }}]");

Demo fiddle #3 here.

But again, just use a proper JSON serializer.

Upvotes: 1

Richa Garg
Richa Garg

Reputation: 1926

The best way is to use JSON serializers as they have in-built handling related to escape characters and other things. See here.

However, if we want to go through this path only to create the JSON string manually, then it can be solved as follows by changing the inner double quotes to single quotes :

public static string MyMethod(string abc, int pqr)
{
   string p = $@"[{{'Key':'{ abc}','Value': {pqr} }}]";
   return p;
}

Upvotes: 17

Zac Faragher
Zac Faragher

Reputation: 1001

I agree with everyone else that building it from strings is a bad idea. I also understand that you don't want to include an extra dependency.

Here's a bit of code I wrote previously to convert a Dictionary to a JSON string. It's pretty basic, only accepts string types, and doesn't escape quote marks in any of the names/values, but that can be done fairly easily.

If you're trying to serialize a large JSON string from basic types, this is the way I'd recommend to do it. It'll help you stay sane.

private static string DictToJson(Dictionary<string, string> Dict)
{
    var json = new StringBuilder();

    foreach (var Key in Dict.Keys)
    {
        if (json.Length != 0)
            json = json.Append(",\n");

        json.AppendFormat("\"{0}\" : \"{1}\"", Key, Dict[Key]);
    }
    return "{" + json.ToString() + "}";
}

Upvotes: 1

Shyam sundar shah
Shyam sundar shah

Reputation: 2523

you can create dictionary and serialize it to json using Json.NET does this.

Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("key1", "value1");
values.Add("key2", "value2");

string json = JsonConvert.SerializeObject(values);
// {
//   "key1": "value1",
//   "key2": "value2"
// }

you can see here more detail : http://www.newtonsoft.com/json/help/html/SerializingCollections.htm

Upvotes: 1

Related Questions