I tried to serialize a LinearGradientBrush
(System.Windows.Media.LinearGradientBrush) with Json.net but without success:
var lg = new LinearGradientBrush();
lg.StartPoint = new Point(2,3);
lg.EndPoint = new Point(3.1,0);
lg.GradientStops = new GradientStopCollection(new []{ new GradientStop(Colors.Red, 0),new GradientStop(Colors.White, 1)});
var json = JsonConvert.SerializeObject(lg);
This code will output a string containing "System.Windows.Media.LinearGradientBrush"
that is obviously wrong.
I tried with xml serialization and it works as expected:
var sb = new StringBuilder();
var writer = XmlWriter.Create(sb, new XmlWriterSettings(){ Indent=true});
var ser = new XmlSerializer(lg.GetType(), new []{ typeof(System.Windows.Media.MatrixTransform)});
ser.Serialize(writer,lg);
var xml = sb.ToString();
Why the json serializer fails to serialize a LinearGradientBrush?
The library
Json.NET
has some problems while trying to serialize complex objects such as an array, a LinearGradientBrush object or an object with an advanced structure, like a Dictionary or something like that (in the case of a LinearGradientBrush, there's a GradientStopCollection data structure that could be the cause of your problem).Try to serialize your object, in a more controlled way, as suggested in the documentation.
If this doesn't help, maybe it's the case to create a custom converter for your object, as suggested here, and use it while invoking the method
SerializeObject()
for the serialization of your object.