是否可以在T4模板中使用NewtonSoft.Json?

时间:2014-06-05 09:57:58

标签: c# json.net t4

我想在T4模板中反序列化一些JSON。我引用了这个程序集,所有这一切都看起来不错,但是当我这样称呼时:

var root = JsonConvert.DeserializeObject<RootObject>(response);

我明白了:

Error   4   Running transformation: System.TypeLoadException: Could not find Windows Runtime type 'Windows.Data.Json.IJsonValue'. ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
   --- End of inner exception stack trace ---
   at Newtonsoft.Json.Converters.JsonValueConverter.CanConvert(Type objectType)
   at Newtonsoft.Json.JsonSerializer.GetMatchingConverter(IList`1 converters, Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
   at Microsoft.VisualStudio.TextTemplating051377DF83FAD6CEB531E8C53BDF7AF0925D52AE4E0D72D32E8A27AD05041FA9D55204F2BCFCC44A6977B1EA4AD1DA5E9682BFE9EB924836019C6E14E1AED232.GeneratedTextTransformation.GetInfoForCountries() in c:\************\CalabashTestGeneration\Template1.tt:line 117
   at Microsoft.VisualStudio.TextTemplating051377DF83FAD6CEB531E8C53BDF7AF0925D52AE4E0D72D32E8A27AD05041FA9D55204F2BCFCC44A6977B1EA4AD1DA5E9682BFE9EB924836019C6E14E1AED232.GeneratedTextTransformation.TransformText() in c:\****************\CalabashTestGeneration\Template1.tt:line 24  c:\*************\CalabashTestGeneration\Template1.tt    117 1   CalabashTestGeneration

这甚至可能吗?我不明白为什么T4不能使用newtonsoft,但这种暗示它不能!

1 个答案:

答案 0 :(得分:0)

我最终使用了restsharp - 似乎到目前为止工作