2013-07-24 25 views
4

我們有這樣的代碼:ServiceStack JSONSerializer和HashSet的<x>

// 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's 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 :(

是否有關於HashSet的類型有些ServiceStack疑難雜症?

回答

2

這似乎是ServiceStack.Text中的一個錯誤。我已將該問題追溯到AssemblyTypeDefinition.cs第17行(撰寫本文時)。傳入的typeDefinition是「System.Collections.Generic.HashSet`1 [[System.String,mscorlib]],System.Core」,並且TypeDefinitionSeperator是','導致字符串在','的第一個實例中被中斷第二,它應該在哪裏。模擬正確拆分的字符串(在調試器中)會從您的代碼中返回正確的結果。

您可能想將此作爲Bug提交給ServiceStack社區。

+0

謝謝,我會去做那個 – welegan

3

雖然這不是最大的答案,但您可以通過創建一個從HashSet繼承的本地類來解決此問題。

實施例:

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

然後參考HashSetHack代替HashSet的,並且它似乎工作。

相關問題