✅ Serializing/Deserializing a Dictionary with a struct key not working
I have been pulling my hair out to support this library that uses a struct key.
Since the Dictionary uses a weird key, I have to change the serialization.
The class is as follows:
My test is written as followed (some code removed for readability):
The test here fails because
Features
is incorrectly filled with just the comment from FeatureBase
and the rest seems to be skipped. Before this I didn't serialize the object to JSON so it even breaks without BCCNamespaceJsonConverter
. How do I support this?4 Replies
Converter:
Verifier objects.
Example 1. This result is expected:
But this is the actual:
Example 2 with the actual dictionary.
Current verifier object:
See this initially works fine, but it skips the keys.
Now adding the actual serialization, the key now works but it omits basically everything else:
So you see as soon as I change the test to serialize the JSON, it is unable to properly serialize the features. If I use verifier directly it works fine, but I have to pass this custom serializer for my Dictionary.
For clarification, the test initially looked like this to get the working objects minus the keys:
So no serialization
well the comments problem is because
Features
is declared as Collection<FeatureBase>
, and FeatureBase
simply only contains Comment
.
you'll need to mark FeatureBase
with [JsonDerivedType(typeof(BCCFeatureBase))]
for that to work.
but i see a much more glaring issue, which is the keys. json keys cannot be anything but strings, so {"IsStrict":true,"Name":"Foo"}: {
absolutely cannot workIt was mostly for testing but on second thought I do want to eventually have this serialize properly so it can be used elsewhere. Perhaps it's better if the actual key here is fixed
I didn't know
JsonDerivedType
was a thing
I think it's best if I simplify this code so it just uses the namespace name instead, or something.
I probably do end up requiring the strict boolean so I suppose in this case the best idea is to convert the struct into a string?
I suppose it would work if I were to make the key JSON. Otherwise maybe create a comma delimited string
Otherwise I'd have to get rid of the dictionary in general which I would rather not have
$closeIf you have no further questions, please use /close to mark the forum thread as answered