If you are storing JSON data in MySQL database (by using built-in JSON datatype) and reading it via .NET Connector you might get some problems with encoding.
For example, you store the following object in the MySQL:
{"Content": "особенно порадовала запись о силе свободного ПО."}
And when you read it you get something like this:
{"Content": "оÑобенно порадовала запиÑÑŒ о Ñиле Ñвободного ПО."}
Why encoding is broken when reading JSON from MySQL?
This is happening due to a bug in the MySQL .NET Connector
Basically, .NET Connector ignores the character encoding setting of the table, a value in the connection string and the SET NAMES
command.
So if your MySQL server is using latin1
character set but the table has utf8mb4
encoding, .NET Connector will always use latin1
to decode the string.
Fix wrong encoding when reading JSON from MySQL
There are two possible fixed for this problem and they both have some tradeoffs.
Use another connector to read data from MySQL
The first solution is to use a different connector to read the data from MySQL. For example, you can use MySQLConnecteror - High-Performance MySQL Library for .NET
Change the character set on the server
Another option is to set a matching character set in the MySQL server, in our example it's utf8mb4
You can do this in two ways:
Edit the MySQL config and include the following settings:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character_set_server=utf8mb4
collation_server=utf8mb4_unicode_ci
Or start mysqld
with the following command-line argument: --character-set-server=utf8mb4