当使用JsonConvert.DeserializeObject(Newtonsoft.Json)进行反序列化时,丢失不可打印的ascii字符(组分隔符) [英] Losing non printable ascii character (group separator) when deserializing with JsonConvert.DeserializeObject (Newtonsoft.Json)

查看:499
本文介绍了当使用JsonConvert.DeserializeObject(Newtonsoft.Json)进行反序列化时,丢失不可打印的ascii字符(组分隔符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个令人沮丧的问题,我无法解决。我使用Newtonsoft.Json反序列化一些 .json 数据。我可以在原始的json字符串中看到有字符序列

  {'\\','u' '0','0','1','d'} 

(ascii 29或0x1D)。但是,当我使用 JsonConvert.DeserializeObject<> 反序列化时,这些字符序列未正确放入其对象。换成的是简单的一维。那就是如果你看看你看到的字符数组:



29'\\\'



6个单独的字符已被替换为1.



有谁知道为什么会发生这种情况 ?我已经在下面列出了一个测试程序。不幸的是,它的行为正是我期望的。也就是说,6个字符显示在对象字段描述中。显然,我错过了一些东西,没有捕捉到实际的问题,我意识到应该尽可能努力地尝试一个在大量代码中复制问题的小程序。不幸的是,我是空白的。所以,我要求关于要寻找的内容以及这可能发生的建议。这样做会在反序列化中做什么?实际的对象比我的坦克的例子更复杂,但是在坦克中有IEnumerable的想法。



非常感谢,
Dave

  class Program 
{
static void Main(string [] args)
{
JsonSerializerSettings settings = new JsonSerializerSettings {ReferenceLoopHandling = ReferenceLoopHandling.Ignore};
坦克坦克1 =新坦克();
char [] array1 = new char [] {'1','2','\\','u','0','0','1' 3','4'};
tank1.Description = new string(array1);
tank1.Id = 1;
Console.WriteLine(原始字符串(显示隐藏字符);
(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(反序列化的字符串字符);
(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.Group = new [] {tank1,tank2,tank3};
string tanksSerializedString = JsonConvert.SerializeObject(tanks,Formatting.Indented,settings);

坦克deserializedTanks = JsonConvert.DeserializeObject< Tanks>(tanksSerializedString,settings);

Console.WriteLine(Deserialized Tanks);
foreach(deserializedTanks.Group中的坦克坦克)
{
Console.WriteLine(反序列化字符串(显示隐藏字符);
for(int i = 0; i< tank.Description.ToArray()。Length; i ++)
{
Console.Write(tank.Description.ToArray()[i] +);
}
控制台.WriteLine();
}
}
}

接口ITank
{
int Id {get; set;}
string描述{get; set;}
}
public class Tank:ITank
{
public Tank(){}
public string描述{get; set; }
public int Id {get; set;}
}

public class Tanks
{
public Tanks(){}
public IEnumerable< Tank> Group {get; set;}
}


解决方案

串行器的行为正如预期的那样。根据



如果您不想要,则必须在字符串中转义 \ 字符:\\\\


I have a frustrating problem that I'm unable to solve. I am using Newtonsoft.Json to deserialize some .json data. I can plainly see in the raw json string that there are character sequences

{ '\\', 'u', '0', '0', '1', 'd' }

This represents "group separator" (ascii 29 or 0x1D). However, when I deserialize using JsonConvert.DeserializeObject<> these character sequences are not put correctly into their objects. What is put in instead is simply 1D. That is if you look at the character array you see:

29 '\u001d'

The 6 separate characters have been replaced by 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". 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.

Many thanks, Dave

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 \u001d:

If you don't want that, the \ character has to be escaped in the string: "\\u001d",

这篇关于当使用JsonConvert.DeserializeObject(Newtonsoft.Json)进行反序列化时,丢失不可打印的ascii字符(组分隔符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆