Having an issue where my object is being serialized using Newton where one of the properties is a string that contains values that are fractions such as 1/2", 1/4", etc...
After serializing I'm passing the variable to a SQL Server Stored Procedure that is using OPENJSON.
The double quote in the fraction value doesn't seem to be escaping properly as it fails as invalid format for JSON. When debugging I see below which would appear the " in the fraction value is not being escaped properly.
A little new to some of the serialization so could use a little help.
string strJson = JsonConvert.SerializeObject(myobject);
DECLARE @json nvarchar(max) = '{"Number":64260,"Notes":"1/2\\" testing"}';
SELECT *
FROM OPENJSON (@json, '$')
WITH(
[Number] int '$.Number'
,[Notes] nvarchar(max) '$.Notes'
) AS myDat
Msg 13609, Level 16, State 4, Line 2 JSON text is not properly formatted. Unexpected character 't' is found at position 32.
If I remove the second "\" from the fraction value it works fine.
public partial class TblEcr
{
public int Number { get; set; }
public string Notes { get; set; }
}
public JsonResult OnPostUpdate([DataSourceRequest] DataSourceRequest request, TblEcr ecr)
{
_context.TblEcr.Where(x => x.Number == ecr.Number).Select(x => ecr);
try
{
if (ModelState.IsValid)
{
string ecrJson = JsonConvert.SerializeObject(ecr);
var param = new SqlParameter[] {
new SqlParameter() {
ParameterName = "@json",
SqlDbType = System.Data.SqlDbType.VarChar,
Size = 8000,
Direction = System.Data.ParameterDirection.Input,
Value = ecrJson
},
new SqlParameter() {
ParameterName = "@Status",
SqlDbType = System.Data.SqlDbType.Bit,
Direction = System.Data.ParameterDirection.Output
//,Value = 10
},
new SqlParameter() {
ParameterName = "@ErrorDetails",
SqlDbType = System.Data.SqlDbType.VarChar,
Size =8000,
Direction = System.Data.ParameterDirection.Output,
}};
int affectedRows = _context.Database.ExecuteSqlCommand("dbo.usp_UpdateECR @json, @Status, @ErrorDetails out", param);
}
}
catch (Exception ex)
{
return new JsonResult(ex.Message);
}
return new JsonResult(new[] { ecr }.ToDataSourceResult(request, ModelState));
}