Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

2.7K
Views
System.Text.Json.JsonSerializer.Serialize adds \u0022

How can I set JsonSerializer to not add "\u0022" to string for EventData property? Because I get:

{"Id":5,"CreateDate":"2021-04-21T05:26:30.9817284Z","EventData":"{\u0022Id\u0022:1,\u0022Email\u0022:\u0022test@test.test\u0022}"}

I will never deserialize EventData, it must be readable. And I want:

{"Id":5,"CreateDate":"2021-04-21T05:26:30.9817284Z","EventData":"{Id:1,Email:test@test.test}"}

My code:

public class EmailSent
{
    public int Id { get; set; }
    public string Email { get; set; }
}

public class UserCreated
{
    public int Id { get; set; }
    public DateTime CreateDate { get; set; }
    public string EventData { get; set; }
}

var emailSent = new EmailSent
{
    Id = 1,
    Email = "test@test.test"
};

var userCreated = new UserCreated
{
    Id = 5,
    CreateDate = DateTime.UtcNow,
    EventData = JsonSerializer.Serialize(emailSent) // I will never deserialize it
};

string result = JsonSerializer.Serialize(userCreated);
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use, for example, UnsafeRelaxedJsonEscaping:

var serializeOptions = new JsonSerializerOptions
{
    WriteIndented = true,
    Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
    };

string json = JsonSerializer.Serialize(userCreated, serializeOptions);

This will produce the following output:

{
  "Id": 5,
  "CreateDate": "2021-04-21T07:49:23.4378969Z",
  "EventData": "{\"Id\":1,\"Email\":\"test@test.test\"}"
}

Reference: How to customize character encoding with System.Text.Json. Please read the caution there:

Caution

Compared to the default encoder, the UnsafeRelaxedJsonEscaping encoder is more permissive about allowing characters to pass through unescaped: (...)

over 4 years ago · Santiago Trujillo Report

0

You can specify 'JSONSerializerOptions' Encoder equal to JavaScriptTenCoder. Create(new TextEncoderSettings(UnicodeRanges.All)) like imageenter image description here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!