dotnet / dotnet/efcore

Stop escaping Unicode characters unnecessarily in relational JSON

Open
#32,152 12 comments 12 reactions 0 assignees View on GitHub
area-json consider-for-next-release customer-reported needs-design
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

The JSON stored in a SQL Server column always has its Unicode characters escaped. For example, this code:

```C#
context.Add(new Customer { Json = new MyJson { Be = "Füzér Castle in the Zemplén Mountains" } } );
await context.SaveChangesAsync();
```

Results in the following JSON in the database:

```json
{"Be":"F\u00FCz\u00E9r Castle in the Zempl\u00E9n Mountains"}
```

However, there is nothing preventing the JSON being stored as:

```json
{"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:

```C#
using (var context = new SomeDbContext())
{
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

context.Add(new Customer { Json = new MyJson { Be = "Füzér Castle in the Zemplén Mountains" } });

await context.SaveChangesAsync();
}

using (var context = new SomeDbContext())
{
foreach (var c in context.Set().ToList())
{
Console.WriteLine(c.Json.Be);
}
}

public class SomeDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Database=AllTogetherNow")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity().OwnsOne(e => e.Json).ToJson();
}
}

public class Customer
{
public int Id { get; set; }
public MyJson Json { get; set; }
}

public class MyJson
{
public string? Be { get; set; }
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.