<small id='eoE68'></small><noframes id='eoE68'>

<tfoot id='eoE68'></tfoot>

      • <bdo id='eoE68'></bdo><ul id='eoE68'></ul>
    1. <legend id='eoE68'><style id='eoE68'><dir id='eoE68'><q id='eoE68'></q></dir></style></legend>

      <i id='eoE68'><tr id='eoE68'><dt id='eoE68'><q id='eoE68'><span id='eoE68'><b id='eoE68'><form id='eoE68'><ins id='eoE68'></ins><ul id='eoE68'></ul><sub id='eoE68'></sub></form><legend id='eoE68'></legend><bdo id='eoE68'><pre id='eoE68'><center id='eoE68'></center></pre></bdo></b><th id='eoE68'></th></span></q></dt></tr></i><div id='eoE68'><tfoot id='eoE68'></tfoot><dl id='eoE68'><fieldset id='eoE68'></fieldset></dl></div>

      Json.net 如何将对象序列化为值

      时间:2023-08-23

        1. <tfoot id='PGaJ6'></tfoot><legend id='PGaJ6'><style id='PGaJ6'><dir id='PGaJ6'><q id='PGaJ6'></q></dir></style></legend>
            <bdo id='PGaJ6'></bdo><ul id='PGaJ6'></ul>
              <tbody id='PGaJ6'></tbody>
            • <small id='PGaJ6'></small><noframes id='PGaJ6'>

              <i id='PGaJ6'><tr id='PGaJ6'><dt id='PGaJ6'><q id='PGaJ6'><span id='PGaJ6'><b id='PGaJ6'><form id='PGaJ6'><ins id='PGaJ6'></ins><ul id='PGaJ6'></ul><sub id='PGaJ6'></sub></form><legend id='PGaJ6'></legend><bdo id='PGaJ6'><pre id='PGaJ6'><center id='PGaJ6'></center></pre></bdo></b><th id='PGaJ6'></th></span></q></dt></tr></i><div id='PGaJ6'><tfoot id='PGaJ6'></tfoot><dl id='PGaJ6'><fieldset id='PGaJ6'></fieldset></dl></div>

                本文介绍了Json.net 如何将对象序列化为值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我仔细阅读了文档、StackOverflow 等,似乎找不到这个...

                I've pored through the docs, StackOverflow, etc., can't seem to find this...

                我想做的是将一个简单的值类型的对象序列化/反序列化为一个值,而不是一个对象,如下所示:

                What I want to do is serialize/deserialize a simple value-type of object as a value, not an object, as so:

                public class IPAddress
                {
                    byte[] bytes;
                
                    public override string ToString() {... etc.
                }
                
                public class SomeOuterObject
                {
                    string stringValue;
                    IPAddress ipValue;
                }
                
                IPAddress ip = new IPAddress("192.168.1.2");
                var obj = new SomeOuterObject() {stringValue = "Some String", ipValue = ip};
                string json = JsonConverter.SerializeObject(obj);
                

                我想要的是让 json 像这样序列化:

                What I want is for the json to serialize like this:

                // json = {"someString":"Some String","ipValue":"192.168.1.2"} <- value serialized as value, not subobject
                

                不是 ip 成为嵌套对象的地方,例如:

                Not where the ip becomes a nested object, ex:

                // json = {"someString":"Some String","ipValue":{"value":"192.168.1.2"}}
                

                有人知道怎么做吗?谢谢!(P.S. 我在一个庞大的遗留 .NET 代码库上使用 Json 序列化,所以我无法真正更改任何现有类型,但我可以扩充/分解/装饰它们以促进 Json 序列化.)

                Does anyone know how to do this? Thanks! (P.S. I am bolting Json serialization on a large hairy legacy .NET codebase, so I can't really change any existing types, but I can augment/factor/decorate them to facilitate Json serialization.)

                推荐答案

                您可以使用 IPAddress 类的自定义 JsonConverter 来处理这个问题.这是您需要的代码:

                You can handle this using a custom JsonConverter for the IPAddress class. Here is the code you would need:

                public class IPAddressConverter : JsonConverter
                {
                    public override bool CanConvert(Type objectType)
                    {
                        return (objectType == typeof(IPAddress));
                    }
                
                    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
                    {
                        return new IPAddress(JToken.Load(reader).ToString());
                    }
                
                    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
                    {
                        JToken.FromObject(value.ToString()).WriteTo(writer);
                    }
                }
                

                然后,将 [JsonConverter] 属性添加到您的 IPAddress 类中,您就可以开始了:

                Then, add a [JsonConverter] attribute to your IPAddress class and you're ready to go:

                [JsonConverter(typeof(IPAddressConverter))]
                public class IPAddress
                {
                    byte[] bytes;
                
                    public IPAddress(string address)
                    {
                        bytes = address.Split('.').Select(s => byte.Parse(s)).ToArray();
                    }
                
                    public override string ToString() 
                    { 
                        return string.Join(".", bytes.Select(b => b.ToString()).ToArray()); 
                    }
                }
                

                这是一个工作演示:

                class Program
                {
                    static void Main(string[] args)
                    {
                        IPAddress ip = new IPAddress("192.168.1.2");
                        var obj = new SomeOuterObject() { stringValue = "Some String", ipValue = ip };
                        string json = JsonConvert.SerializeObject(obj);
                        Console.WriteLine(json);
                    }
                }
                
                public class SomeOuterObject
                {
                    public string stringValue { get; set; }
                    public IPAddress ipValue { get; set; }
                }
                

                输出:

                {"stringValue":"Some String","ipValue":"192.168.1.2"}
                

                这篇关于Json.net 如何将对象序列化为值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 NewtonSoft 更新 JSON 对象的属性 下一篇:使用带有 ItemRequired = Required.Always 的 Json.Net 反序

                相关文章

                最新文章

                    <tfoot id='wD8eF'></tfoot>

                    <small id='wD8eF'></small><noframes id='wD8eF'>

                    • <bdo id='wD8eF'></bdo><ul id='wD8eF'></ul>
                    <i id='wD8eF'><tr id='wD8eF'><dt id='wD8eF'><q id='wD8eF'><span id='wD8eF'><b id='wD8eF'><form id='wD8eF'><ins id='wD8eF'></ins><ul id='wD8eF'></ul><sub id='wD8eF'></sub></form><legend id='wD8eF'></legend><bdo id='wD8eF'><pre id='wD8eF'><center id='wD8eF'></center></pre></bdo></b><th id='wD8eF'></th></span></q></dt></tr></i><div id='wD8eF'><tfoot id='wD8eF'></tfoot><dl id='wD8eF'><fieldset id='wD8eF'></fieldset></dl></div>
                    <legend id='wD8eF'><style id='wD8eF'><dir id='wD8eF'><q id='wD8eF'></q></dir></style></legend>