title: How to serialize a List<object> while simultaneously escaping special characters?
body: <p>We have a very large <code>List&lt;object&gt;</code> that we are turning into a json string and then sending to a stored procedure.  To keep things simple for this post, let's say the object looks like this:</p>
  
  <pre><code>public class Customer
  {
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>And one of the customers is "Bob's Repair Shop".  When the list with this value in it gets serialized, it will contain this:</p>
  
  <pre><code>{
      "Name": "Bob's Repair Shop"
  }
  </code></pre>
  
  <p>That's ok, right up until we want to send this json into a stored procedure.  When it gets there, that single quote throws an error (which I understand).</p>
  
  <p>Originally we handled this by doing a <code>.replace("'", "~")</code> on the json string in the C# code, and then doing the reverse in the stored proc.  That worked for a while. But what we are running into now is that the object we have a List&lt;> of is much more complex that this little example, and we are also dealing with several hundred thousand records in the list. When the json string is this large, the <code>.replace("'", "~")</code> throws an <code>Out of Range</code> exception.  And let's be honest, this <code>.replace("'", "~")</code> approach is a hack anyway.</p>
  
  <p>What I'm hoping to find is a way to serialize our <code>List&lt;object&gt;</code> in such a way that the single quotes are escaped during the serialization process. Is this possible? And if so, how?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I really should have said this originally.  Sorry I missed it. Here's how the stored proc is receiving the json string:</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[name_of_proc]
      @jsonString NVARCHAR(MAX)
  AS
  </code></pre>
  
  <p>And then we convert the <code>@jsonString</code> to a table variable with <code>OPENJSON</code></p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>how the sp is called:</p>
  
  <pre><code>    public void UpdateBulk(List&lt;object&gt; myObject)
      {
          string json = Newtonsoft.Json.JsonConvert.SerializeObject(myObject);
          json.replace("'", "~");
          var dParam = new DynamicParameters();
          dParam.Add("@jsonString", json);
          QuerySP&lt;myObject&gt;("name_of_proc", dParam);
      }
  
      protected IEnumerable&lt;T&gt; QuerySP&lt;T&gt;(string storedProcedure, object param = null)
      {
          using (var db = this.Context)
          {
              var output = db.Query&lt;T&gt;(storedProcedure, param: param, commandType: CommandType.StoredProcedure,commandTimeout: 32767);
              return output;
          }
      }
  </code></pre>
  
  <p><strong>EDIT 3:</strong>
  The thread pointed to as a possible duplicate asks how to change this:</p>
  
  <pre><code>{"key" : "value"}
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>{'key' : 'value'}
  </code></pre>
  
  <p>That's not what I'm asking.  I'm asking how to change this:</p>
  
  <pre><code>{
      "Name": "Bob's Repair Shop"
  }
  </code></pre>
  
  <p>To this:</p>
  
  <pre><code>{
      "Name": "Bob\'s Repair Shop"
  }
  </code></pre>