I was writing some code to serialize a C# class to JSON and needed to export a System.Drawing.Color field as a hex string. Thankfully it's easy to customize the output using Json.NET. I created the following custom converter to save colors as hex strings (like #FFF0B6)
And example usage:
3 comments:
Just as a word of warning - in the ReadJson function, you want to use reader.Value as string, rather than reader.ReadAsString() as this consumes the next token rather than viewing the current one.
Here to echo what "Anonymous" said - "reader.ReadAsString()" should instead be "reader.Value.ToString()"
Updated the code. Thanks for those who pointed it out.
Post a Comment