❔ Cannot deserialize System.Drawing.Color from JSON
It seems like the System.Text.Json library is not able to deserialize a Color from a JSON string it serialized before.
Code I used for testing:
75 Replies
Write your own converter then
tuple issue i guess
whoopie
Yup, seems likely
I dont think I understand?
Tuples can be serialized but not easily deserialized. You will need a custom converter afaik
but the Color struct doesnt use Tuples
What Color struct is this? From System.Drawing?
yes
ah, seems like it's read-only
yeah you'd probably need to write your own STJ converter for it
How to write custom converters for JSON serialization - .NET
Learn how to create custom converters for the JSON serialization classes that are provided in the System.Text.Json namespace.
ah ok I get it then. I was just confused because the Newtonsoft.Json library was able to do it when I still used that
tbh serializing the other properties are unnecessarily
idk how Newtonsoft would be able to do that since the properties are get-only
What if I create my own color struct and add an implicit cast to System.Drawing.Color
maybe newtonsoft had a custom converter for Color
ero
REPL Result: Success
Result: string
Quoted by
<@542772576905199626> from #bot-spam (click here)
Compile: 549.845ms | Execution: 48.130ms | React with ❌ to remove this embed.
:/
what's the deal with tuples? System.Drawing.Color isn't a tuple
ARGB is
no it's not
that's just how it displays it
looks like a debugger display attribute
if you look at the code, they're clearly serializing
toJson
, which is a Color
Iirc it uses reflection to get the private setter
ah
there is no setter actually
it's a readonly struct
it also has no public constructor
except for the default parameterless one i guess
Ill just go ahead an write a custom converter
prevents it from saving all the unnecessary fields as well
u could try this
TheRanger
REPL Result: Success
Console Output
Compile: 811.230ms | Execution: 124.396ms | React with ❌ to remove this embed.
Thanks but I dont want to only serialize one color. I have a bunch of dictionaries with dictionaries with colors in them
u mean a color array/list?
No
I mean dictionaries with more dictionaries and then colors in those
i see, then ud probably need a custom converter then
So I am having trouble writing this converter
I would like my colors to be converted to the following
"FieldName" : "R, G, B"
Obviously for writing I could just do
but I am not sure how the reader would work with thisread the docs
u can define a method called Read
yes I know
I dont know how to properly extract the values though
would I have to just call ReadString and then do some string manipulation there? Because obviously its all in one string
you can use .Split, or regex
Ok
i'm late but the contents of "fromJson" aren't valid json to begin with
looks like it just
.ToString()
ed the colorwhat
fromJson isnt even a string
ignore me i can't read
theyre writing the converter now
too many variables called json
happens to the best of us
this is what you want
it allocates more data tho
theres a space after each
does ConvertFromString work if the colors are seperated by commas opposed to semicolons?
yes
that is how it serializes by default as well
well. ColorConverter does some special stuff. consider:
it does not work with commas it seems, unfortunate
reflectronic
REPL Result: Success
Result: <>f__AnonymousType0#1<string, string, string, string>
Compile: 520.415ms | Execution: 74.759ms | React with ❌ to remove this embed.
reflectronic
REPL Result: Success
Result: Color
Compile: 563.665ms | Execution: 36.646ms | React with ❌ to remove this embed.
huh
what do you mean by it not working
Im not sure either now
I get this exception
but this might be due to a bad json file?
ill check
oh, i see what the problem is
no defnitely doesnt work with commas in the json
use
ConvertToString(null, CultureInfo.InvariantCulture, value)
huh it works here
yeah it uses the list separator based on the current culture
oh their culture
oh, actuall
but wait
shouldnt converter.ConvertToString(value) return 255. 255. 255?
you can just use
(Color)converter.ConvertFromInvariantString(reader.GetString() ?? "")
and converter.ConvertToInvariantString(color)
or whatever the list seperator their culture uses
it would but i imagine this JSON was serialized before
or maybe they used an already serialized json
whats this whole culture thing
like is it due to me living in a different country I have different list seperators?
yes
what a stupid feature
based on ur pc settings
its not
some cultures uses . as the default seperator
it is actually much more stupid to show someone that lives in Germany
27.5
instead of 27,5
in their user interfaceit is, I wouldnt mind having to use semicolons but if people in other countries share their files they would be incompatible I guess
that is why you use
ConvertFromInvariantString
like i said
it fixes this issueYea I get it. I just didnt expect list seperators to be different in other cultures
I thought there would be some standard to it
Yea thanks I will try that
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.