[英]Losing non printable ascii character (group separator) when deserializing with JsonConvert.DeserializeObject (Newtonsoft.Json)
I have a frustrating problem that I'm unable to solve.我有一个令人沮丧的问题,我无法解决。 I am using Newtonsoft.Json to deserialize some
.json
data.我正在使用 Newtonsoft.Json 反序列化一些
.json
数据。 I can plainly see in the raw json string that there are character sequences我可以在原始 json 字符串中清楚地看到有字符序列
{ '\\', 'u', '0', '0', '1', 'd' }
This represents "group separator" (ascii 29 or 0x1D).这表示“组分隔符”(ascii 29 或 0x1D)。 However, when I deserialize using
JsonConvert.DeserializeObject<>
these character sequences are not put correctly into their objects.但是,当我使用
JsonConvert.DeserializeObject<>
反序列化时,这些字符序列没有正确放入它们的对象中。 What is put in instead is simply 1D.相反,放入的只是一维。 That is if you look at the character array you see:
也就是说,如果您查看您看到的字符数组:
29 '\'
The 6 separate characters have been replaced by 1. 6 个单独的字符已替换为 1。
Does anyone know why this might happen ?有谁知道为什么会发生这种情况? I have included a test program below.
我在下面包含了一个测试程序。 Unfortunately, it behaves exactly how I would expect.
不幸的是,它的行为完全符合我的预期。 That is, the 6 characters show up in the objects field "Description".
也就是说,6 个字符显示在对象字段“描述”中。 Clearly, I've missed something and not captured that actual problem and I realize one is supposed to try as hard as possible to come up with a small program that duplicates the problem in the large body of code.
很明显,我错过了一些东西并且没有捕捉到真正的问题,我意识到应该尽可能努力地想出一个小程序,在大量代码中复制问题。 Unfortunately, I'm coming up blank.
不幸的是,我来了空白。 So, I'm asking for advice on what to look for and how this could possibly happen.
所以,我正在寻求关于寻找什么以及这可能如何发生的建议。 What would do this replacement on a deserialization?
这个替换对反序列化有什么作用? The actual objects are more complicated than my Tanks example, but there is the ideas of IEnumerable in Tanks.
实际对象比我的 Tanks 示例更复杂,但是在 Tanks 中有 IEnumerable 的想法。
class Program
{
static void Main(string[] args)
{
JsonSerializerSettings settings = new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
Tank tank1 = new Tank();
char[] array1 = new char[] { '1', '2', '\\', 'u', '0', '0', '1', 'd', '3', '4' };
tank1.Description = new string(array1);
tank1.Id = 1;
Console.WriteLine("Original string (showing hidden characters");
for (int i = 0; i < tank1.Description.ToArray().Length; i++)
{
Console.Write(tank1.Description.ToArray()[i] + " ");
}
Console.WriteLine();
string conversion1 = JsonConvert.SerializeObject(tank1);
Tank deserializedTank1 = JsonConvert.DeserializeObject<Tank>(conversion1, settings);
Console.WriteLine("Deserialized string (showing hidden characters");
for (int i = 0; i < deserializedTank1.Description.ToArray().Length; i++)
{
Console.Write(deserializedTank1.Description.ToArray()[i] + " ");
}
Console.WriteLine();
Tank tank2 = new Tank() { Id = 2 };
tank2.Description = new string(array1);
Tank tank3 = new Tank() { Id = 3 };
tank3.Description = new string(array1);
Tanks tanks = new Tanks();
tanks.Group = new [] { tank1, tank2, tank3};
string tanksSerializedString = JsonConvert.SerializeObject(tanks,Formatting.Indented,settings);
Tanks deserializedTanks = JsonConvert.DeserializeObject<Tanks>(tanksSerializedString, settings);
Console.WriteLine("Deserialized Tanks");
foreach (Tank tank in deserializedTanks.Group)
{
Console.WriteLine("Deserialized string (showing hidden characters");
for (int i = 0; i < tank.Description.ToArray().Length; i++)
{
Console.Write(tank.Description.ToArray()[i] + " ");
}
Console.WriteLine();
}
}
}
interface ITank
{
int Id { get; set; }
string Description { get; set; }
}
public class Tank : ITank
{
public Tank() { }
public string Description { get; set; }
public int Id { get; set; }
}
public class Tanks
{
public Tanks() { }
public IEnumerable<Tank> Group { get; set; }
}
The serializer is behaving as expected.序列化程序按预期运行。 According to the JSON Standard , a sequence of characters in the pattern
\\u four-hex-digits
represent a single (utf16) Unicode character literal, the group separator character for \
:根据JSON 标准,模式
\\u four-hex-digits
的字符序列表示单个 (utf16) Unicode 字符文字,即\
的组分隔符:
If you don't want that, the \\
character has to be escaped in the string: "\\\"
,如果你不想那样,
\\
字符必须在字符串中转义: "\\\"
,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.