Json.net how to serialize object as value

前端 未结 6 1411
滥情空心
滥情空心 2020-12-15 19:06

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, n

相关标签:
6条回答
  • 2020-12-15 19:07

    This is a answer to Customise NewtonSoft.Json for Value Object serialisation, in regards to value objects in DDD. But that question is marked as duplicate to this one, which i don't think is completely true.

    I borrowed the code for the ValueObjectConverter from https://github.com/eventflow/EventFlow, I have only done minor changes.

    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using FluentAssertions;
    using Newtonsoft.Json;
    using Xunit;
    
    namespace Serialization
    {
        public class ValueObjectSerializationTests
        {
            class SomeClass
            {
                public IPAddress IPAddress { get; set; }
            }
    
            [Fact]
            public void FactMethodName()
            {
                var given = new SomeClass
                {
                    IPAddress = new IPAddress("192.168.1.2")
                };
    
                var jsonSerializerSettings = new JsonSerializerSettings()
                {
                    Converters = new List<JsonConverter>
                                 {
                                    new ValueObjectConverter()
                                 }
                };
                var json = JsonConvert.SerializeObject(given, jsonSerializerSettings);
    
                var result = JsonConvert.DeserializeObject<SomeClass>(json, jsonSerializerSettings);
    
                var expected = new SomeClass
                {
                    IPAddress = new IPAddress("192.168.1.2")
                };
    
                json.Should().Be("{\"IPAddress\":\"192.168.1.2\"}");
                expected.ShouldBeEquivalentTo(result);
            }
        }
    
        public class IPAddress:IValueObject
        {
            public IPAddress(string value)
            {
                Value = value;
            }
    
            public object GetValue()
            {
                return Value;
            }
    
            public string Value { get; private set; }
        }
    
        public interface IValueObject
        {
            object GetValue();
        }
    
        public class ValueObjectConverter : JsonConverter
        {
            private static readonly ConcurrentDictionary<Type, Type> ConstructorArgumenTypes = new ConcurrentDictionary<Type, Type>();
    
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                if (!(value is IValueObject valueObject))
                {
                    return;
                }
    
                serializer.Serialize(writer, valueObject.GetValue());
            }
    
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                var parameterType = ConstructorArgumenTypes.GetOrAdd(
                    objectType,
                    t =>
                    {
                        var constructorInfo = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).First();
                        var parameterInfo = constructorInfo.GetParameters().Single();
                        return parameterInfo.ParameterType;
                    });
    
                var value = serializer.Deserialize(reader, parameterType);
                return Activator.CreateInstance(objectType, new[] { value });
            }
    
            public override bool CanConvert(Type objectType)
            {
                return typeof(IValueObject).IsAssignableFrom(objectType);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-15 19:12
    public class IPAddress
    {
        byte[] bytes;
    
        public override string ToString() {... etc.
    }
    
    IPAddress ip = new IPAddress("192.168.1.2");
    var obj = new () {ipValue = ip.ToString()};
    string json = JsonConverter.SerializeObject(obj);
    

    You are serializing the whole IP address instance. Maybe just try to serialize the address as a string. (This presumes that you have implemented the ToString-method.)

    0 讨论(0)
  • 2020-12-15 19:25

    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);
        }
    }
    

    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()); 
        }
    }
    

    Here is a working demo:

    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; }
    }
    

    Output:

    {"stringValue":"Some String","ipValue":"192.168.1.2"}
    
    0 讨论(0)
  • 2020-12-15 19:27

    Here is a class for generic conversion of simple value objects that I plan to include in the next update of Activout.RestClient. A "simple value object" as an object that has:

    1. No default constructor
    2. A public property named Value
    3. A constructor taking the same type as the Value property

    var settings = new JsonSerializerSettings
    {
        Converters = new List<JsonConverter> {new SimpleValueObjectConverter()}
    };
    

    public class SimpleValueObjectConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var valueProperty = GetValueProperty(value.GetType());
            serializer.Serialize(writer, valueProperty.GetValue(value));
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
            JsonSerializer serializer)
        {
            var valueProperty = GetValueProperty(objectType);
            var value = serializer.Deserialize(reader, valueProperty.PropertyType);
            return Activator.CreateInstance(objectType, value);
        }
    
        public override bool CanConvert(Type objectType)
        {
            if (GetDefaultConstructor(objectType) != null) return false;
            var valueProperty = GetValueProperty(objectType);
            if (valueProperty == null) return false;
            var constructor = GetValueConstructor(objectType, valueProperty);
            return constructor != null;
        }
    
        private static ConstructorInfo GetValueConstructor(Type objectType, PropertyInfo valueProperty)
        {
            return objectType.GetConstructor(new[] {valueProperty.PropertyType});
        }
    
        private static PropertyInfo GetValueProperty(Type objectType)
        {
            return objectType.GetProperty("Value");
        }
    
        private static ConstructorInfo GetDefaultConstructor(Type objectType)
        {
            return objectType.GetConstructor(new Type[0]);
        }
    }
    
    0 讨论(0)
  • 2020-12-15 19:30

    There are a couple of different ways to approach this depending on the level of effort you are able to expend and the tolerance for changes to existing classes.

    One approach is to define your classes as DataContract and explicitly identify the elements within the class as DataMembers. Netwonsoft recognizes and uses these attributes in its serialization. The upside to this approach is that the classes will now be serializable using other approaches that use datacontract serialization.

        [DataContract]
        public class IPAddress
        {
            private byte[] bytes;
    
            // Added this readonly property to allow serialization
            [DataMember(Name = "ipValue")]
            public string Value
            {
                get
                {
                    return this.ToString();
                }
            }
    
            public override string ToString()
            {
                return "192.168.1.2";
            }
        }
    

    Here is the code that I used to serialize (I may be using an older version since I didn't see the SerializeObject method):

            IPAddress ip = new IPAddress();
    
            using (StringWriter oStringWriter = new StringWriter())
            {
                using (JsonTextWriter oJsonWriter = new JsonTextWriter(oStringWriter))
                {
                    JsonSerializer oSerializer = null;
                    JsonSerializerSettings oOptions = new JsonSerializerSettings();
    
                    // Generate the json without quotes around the name objects
                    oJsonWriter.QuoteName = false;
                    // This can be used in order to view the rendered properties "nicely"
                    oJsonWriter.Formatting = Formatting.Indented;
                    oOptions.NullValueHandling = NullValueHandling.Ignore;
    
                    oSerializer = JsonSerializer.Create(oOptions);
    
                    oSerializer.Serialize(oJsonWriter, ip);
    
                    Console.WriteLine(oStringWriter.ToString());
                }
            }
    

    Here is the output:

    {
      ipValue: "192.168.1.2"
    }
    

    Another approach is to create your own JsonConverter inheritor that can serialize exactly what you need without modifications to the internals of the class:

    public class JsonToStringConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return true;
        }
        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.WriteStartObject();
            writer.WritePropertyName(value.GetType().Name);
            writer.WriteValue(Convert.ToString(value));
            writer.WriteEndObject();
        }
    }
    

    This class just writes the tostring value of the class along with the class name. Changing the name can be accomplished through additional attributes on the class, which I have not shown.

    The class would then look like:

        [JsonConverter(typeof(JsonToStringConverter))]
        public class IPAddress
        {
            private byte[] bytes;
    
            public override string ToString()
            {
                return "192.168.1.2";
            }
        }
    

    And the output is:

    {
      IPAddress: "192.168.1.2"
    }
    
    0 讨论(0)
  • 2020-12-15 19:33

    With the Cinchoo ETL - an open source library to parsing / writing JSON files, you can control the serialization of each object member via ValueConverter or with callback mechanism.

    Method 1:

    The sample below shows how to serialize 'SomeOuterObject' using member level ValueConverters

    public class SomeOuterObject
    {
        public string stringValue { get; set; }
        [ChoTypeConverter(typeof(ToTextConverter))]
        public IPAddress ipValue { get; set; }
    }
    

    And the value converter is

    public class ToTextConverter : IChoValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToString();
        }
    }
    

    Finally to serialize the object to file

    using (var jr = new ChoJSONWriter<SomeOuterObject>("ipaddr.json")
        )
    {
        var x1 = new SomeOuterObject { stringValue = "X1", ipValue = IPAddress.Parse("12.23.21.23") };
        jr.Write(x1);
    }
    

    And the output is

    [
     {
      "stringValue": "X1",
      "ipValue": "12.23.21.23"
     }
    ]
    

    Method 2:

    This the alternative method to hook up value converter callback to 'ipValue' property. This approach is lean and no need to create value converter for just this operation.

    using (var jr = new ChoJSONWriter<SomeOuterObject>("ipaddr.json")
        .WithField("stringValue")
        .WithField("ipValue", valueConverter: (o) => o.ToString())
        )
    {
        var x1 = new SomeOuterObject { stringValue = "X1", ipValue = IPAddress.Parse("12.23.21.23") };
        jr.Write(x1);
    }
    

    Hope this helps.

    Disclaimer: I'm the author of the library.

    0 讨论(0)
提交回复
热议问题