ServiceStack JSONSerializer and HashSet <x>

We have this code:

// using ServiceStack JSONSerializer string typeInfoString = JsonSerializer.SerializeToString<Type>(typeof(HashSet<string>)); // yields "System.Collections.Generic.HashSet`1[[System.String, mscorlib]], System.Core" // this string is the same thing, so it probably valid json string jsonTypeInfo = typeof(HashSet<string>).ToJson(); // this should work, I feel like Type desType = JsonSerializer.DeserializeFromString<Type>(jsonTypeInfo); // but desType ends up being null :( 

Is there any ServiceStack to get HashSet type information?

+4
source share
2 answers

This seems like an error in ServiceStack.Text. I traced this problem to the AssemblyTypeDefinition.cs 17 line (at the time of this writing). Incoming typeDefinition is "System.Collections.Generic.HashSet`1 [[System.String, mscorlib]], System.Core", and TypeDefinitionSeperator is ",", resulting in the line being split into the first instance "," instead of the second, where should he. Simulating the correct line break (in the debugger) returns the correct result from your code.

You might want to portray this as a bug for the ServiceStack community.

+2
source

Although this is not the best answer, you can work around this by creating a local class that inherits from HashSet.

Example:

 public class HashSetHack<T> : HashSet<T> { } 

Then access the HashSetHack instead of the HashSet and it seems to work.

+3
source

Source: https://habr.com/ru/post/1493271/


All Articles