You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stop escaping Unicode characters unnecessarily in relational JSON #32152
The JSON stored in a SQL Server column always has its Unicode characters escaped. For example, this code:
context.Add(newCustomer{Json=newMyJson{Be="Füzér Castle in the Zemplén Mountains"}});awaitcontext.SaveChangesAsync();
Results in the following JSON in the database:
{"Be":"F\u00FCz\u00E9r Castle in the Zempl\u00E9n Mountains"}
However, there is nothing preventing the JSON being stored as:
{"Be":"Füzér Castle in the Zemplén Mountains"}
EF reads the correct string back in both cases, but other tools may not be expecting escaped JSON for all Unicode characters.
Test code:
using(varcontext=newSomeDbContext()){awaitcontext.Database.EnsureDeletedAsync();awaitcontext.Database.EnsureCreatedAsync();context.Add(newCustomer{Json=newMyJson{Be="Füzér Castle in the Zemplén Mountains"}});awaitcontext.SaveChangesAsync();}using(varcontext=newSomeDbContext()){foreach(varcincontext.Set<Customer>().ToList()){Console.WriteLine(c.Json.Be);}}publicclassSomeDbContext:DbContext{protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder)=>optionsBuilder.UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow").LogTo(Console.WriteLine,LogLevel.Information).EnableSensitiveDataLogging();protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){modelBuilder.Entity<Customer>().OwnsOne(e =>e.Json).ToJson();}}publicclassCustomer{publicintId{get;set;}publicMyJsonJson{get;set;}}publicclassMyJson{publicstring?Be{get;set;}}
Reacted by Frank Buckley, Yury Romanov, Mateusz Kleczkowski, austinw-fineart, hemast, Sveinung, fiseni-pcl, Fati Iseni, Nachman Aryeh Berkowitz, dradovic and 2 more
However, for people using non-Unicode encodings in the database, or wanting to interact with the JSON documents from other tools which may not support special chars, we should probably have some sort of mechanism for specifying the encoding/escaping policy; this basically boils down to allowing users to specify the JsonSerializerOptions (which has this one it).
Maybe it makes sense to keep the current behavior as the default (as that's the S.T.Json default), and just tell people to specify UnsafeRelaxedJsonEscaping via JsonSerializerOptions if they want unescaped chars.
Reacted by Arthur Vickers, Frank Buckley and hemast
We also escape all unicode chars in JSON property names, however when building a JSON path for query or update we don't escape those chars. This leads to us not fetching/updating the requested JSON fragments
Note that we'd need to not escape - and deal with non-escaped JSON data - everywhere in our stack (SaveChanges, JSON path generation, shaper - see #33771.
As in #33771 (review), we should consider doing this for 9.0 as well, since #33771 is a breaking change for people querying externally-inputted JSON data that isn't escaped.
Note for the implementer: (as pointed out by @Charlieface here: #33443 (comment)) when we allow un-escaped JSON strings, we need to escape single and double quotes and also pre-pend N to the JSON path string.
We won't make any effort to support both escaped and non-escaped JSON content in the same database. For example, a single context option is probably sufficient as an application-wide setting, as opposed to e.g. a column-by-column setting.
Important: with EF 10 it becomes possible to use ExecuteUpdate to do partial updates inside JSON documents. Unlike our previous partial updating via SaveChanges, ExecuteUpdate is a 100% server-side operation, where none of our client-side JSON serialization functionality can be involved (i.e. JsonValueReaderWriter). In other words, starting with 10 we'll start to generate SQL roughly similar to the following:
UPDATE x SETx.JsonThing= JSON_MODIFY(x.JsonThing, '$.a', x.SomeUnrelatedColumn);
Importantly, the SQL Server JSON functions (e.g. JSON_MODIFY) do not perform Unicode escaping in the way that we do client-side, and so such partial updates would cause unescaped content to be saved to the database.
That seems to be a pretty good reason to stop doing the cilent-side escaping (by default) on the client side: we should be aligning to what the database does as much as possible.
Activity
maumar commentedon Oct 25, 2023
related: #30315
roji commentedon Nov 2, 2023
Here's the relevant docs page on encoding/escaping in System.Text.Json; we may want to specify JavaScriptEncoder.UnsafeRelaxedJsonEscaping.
However, for people using non-Unicode encodings in the database, or wanting to interact with the JSON documents from other tools which may not support special chars, we should probably have some sort of mechanism for specifying the encoding/escaping policy; this basically boils down to allowing users to specify the JsonSerializerOptions (which has this one it).
Maybe it makes sense to keep the current behavior as the default (as that's the S.T.Json default), and just tell people to specify UnsafeRelaxedJsonEscaping via JsonSerializerOptions if they want unescaped chars.
maumar commentedon May 10, 2024
related: #33443
We also escape all unicode chars in JSON property names, however when building a JSON path for query or update we don't escape those chars. This leads to us not fetching/updating the requested JSON fragments
roji commentedon Jun 2, 2024
Note that we'd need to not escape - and deal with non-escaped JSON data - everywhere in our stack (SaveChanges, JSON path generation, shaper - see #33771.
As in #33771 (review), we should consider doing this for 9.0 as well, since #33771 is a breaking change for people querying externally-inputted JSON data that isn't escaped.
maumar commentedon Jun 3, 2024
Note for the implementer: (as pointed out by @Charlieface here: #33443 (comment)) when we allow un-escaped JSON strings, we need to escape single and double quotes and also pre-pend N to the JSON path string.
roji commentedon Jun 4, 2024
Design discussion:
12 remaining items
AndriySvyryd commentedon Jun 2, 2025
@roji Also see #36146
roji commentedon Sep 9, 2025
Important: with EF 10 it becomes possible to use ExecuteUpdate to do partial updates inside JSON documents. Unlike our previous partial updating via SaveChanges, ExecuteUpdate is a 100% server-side operation, where none of our client-side JSON serialization functionality can be involved (i.e. JsonValueReaderWriter). In other words, starting with 10 we'll start to generate SQL roughly similar to the following:
Importantly, the SQL Server JSON functions (e.g. JSON_MODIFY) do not perform Unicode escaping in the way that we do client-side, and so such partial updates would cause unescaped content to be saved to the database.
That seems to be a pretty good reason to stop doing the cilent-side escaping (by default) on the client side: we should be aligning to what the database does as much as possible.