如何从serilaized字符串中删除引号?

时间:2021-09-10 06:25:23

I need to produce a JSON document, that will be parsed by a SSI mechanism on a device. The document will actually be a json serialized dictionary. For the sake of simplicity, let's say, that it should look like this:

我需要生成一个JSON文档,它将由设备上的SSI机制解析。该文档实际上是一个json序列化字典。为简单起见,让我们说,它应该是这样的:

var x = new Dictionary<string,object> 
    { 
    ["A"]=new {x = "<!-- ?A.x -->"},
    ["B"]=new {x = "<!-- ?B.x -->"} 
    };

JsonConvert.SerializeObject(x).Dump();

Which produces in LinqPad:

在LinqPad中生产:

{"A":{"x":"<!-- ?A.x -->"},"B":{"x":"<!-- ?B.x -->"}}

But actually those "x" fields are numbers, and when fetched from the device, they will contain numbers. So I would need to serialize this dictionary without quotes around a field value that is string on C# side:

但实际上那些“x”字段是数字,当从设备中取出时,它们将包含数字。所以我需要序列化这个字典而不用C#侧字符串的字段值引号:

{"A":{"x":<!-- ?A.x -->},"B":{"x":<!-- ?B.x -->}}

How can I force Newtonsoft Json.NET serializer not to add quotes to the value of specific fields (not all) during serialization?

如何强制Newtonsoft Json.NET序列化程序在序列化期间不要在特定字段(不是全部)的值中添加引号?

Thank you.

谢谢。

2 个解决方案

#1


0  

One way to do it is by introducing new JsonConverter (sample). To separate the functionality of "raw serialization", you could introduce new type that would just wrap a string value, eg.

一种方法是引入新的JsonConverter(样本)。要分离“原始序列化”的功能,您可以引入只包装字符串值的新类型,例如。

public class RawJson
{
    public string Value { get; private set; }

    public RawJson(string value)
    {
        Value = value;
    }
}

Then you just check for this type in converter's CanConvert() and in WriteJson() you can just write

然后你只需要在转换器的CanConvert()和WriteJson()中检查这种类型即可

writer.WriteRawValue(((RawJson)value).Value);

#2


0  

And below is the actual solution, based on @kiziu's suggestion to use custom converter. But without custom type. As the converter can be added with the attribute to members too, and not only to classes or the converter itself, I can use it on the property I need. The above LinqPad scratch updated:

以下是实际解决方案,基于@ kiziu建议使用自定义转换器。但没有自定义类型。由于转换器也可以将属性添加到成员中,而不仅仅是类或转换器本身,我可以在我需要的属性上使用它。以上LinqPad临时更新:

internal class RawJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(string);
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteRawValue((string)value);
        }
    }

class myClass 
{
    [JsonConverter(typeof(RawJsonConverter))]
    public string x;
}

void Main()
{
    var x = new Dictionary<string,object> 
        { 
        ["A"]=new myClass {x = "<!-- ?A.x -->"},
        ["B"]=new myClass {x = "<!-- ?B.x -->"} 
        };


    JsonConvert.SerializeObject(x).Dump();
}

And the result is, as expected:

结果如预期:

{"A":{"x":<!-- ?A.x -->},"B":{"x":<!-- ?B.x -->}}

#1


0  

One way to do it is by introducing new JsonConverter (sample). To separate the functionality of "raw serialization", you could introduce new type that would just wrap a string value, eg.

一种方法是引入新的JsonConverter(样本)。要分离“原始序列化”的功能,您可以引入只包装字符串值的新类型,例如。

public class RawJson
{
    public string Value { get; private set; }

    public RawJson(string value)
    {
        Value = value;
    }
}

Then you just check for this type in converter's CanConvert() and in WriteJson() you can just write

然后你只需要在转换器的CanConvert()和WriteJson()中检查这种类型即可

writer.WriteRawValue(((RawJson)value).Value);

#2


0  

And below is the actual solution, based on @kiziu's suggestion to use custom converter. But without custom type. As the converter can be added with the attribute to members too, and not only to classes or the converter itself, I can use it on the property I need. The above LinqPad scratch updated:

以下是实际解决方案,基于@ kiziu建议使用自定义转换器。但没有自定义类型。由于转换器也可以将属性添加到成员中,而不仅仅是类或转换器本身,我可以在我需要的属性上使用它。以上LinqPad临时更新:

internal class RawJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(string);
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteRawValue((string)value);
        }
    }

class myClass 
{
    [JsonConverter(typeof(RawJsonConverter))]
    public string x;
}

void Main()
{
    var x = new Dictionary<string,object> 
        { 
        ["A"]=new myClass {x = "<!-- ?A.x -->"},
        ["B"]=new myClass {x = "<!-- ?B.x -->"} 
        };


    JsonConvert.SerializeObject(x).Dump();
}

And the result is, as expected:

结果如预期:

{"A":{"x":<!-- ?A.x -->},"B":{"x":<!-- ?B.x -->}}