I use the following code to deserialize JSON files to .NET objects:
using (var textReader = File.OpenText(filePath))
{
var settings = new JsonSerializerSettings
{
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
TypeNameHandling = TypeNameHandling.All
};
var deserializer = JsonSerializer.CreateDefault(settings);
deserializer.Converters.Add(new StringEnumConverter());
return deserializer.Deserialize<T>(new JsonTextReader(textReader));
}
This works all quite fine when using that functionality in the context of a unit test for example. All classes are placed in several assemblies.
Now instead of using unit tests I want to control the flow of my components by PowerShell cmdlets.
I wrote an cmdlet and import the module that is still placed in the bin\Debug folder: Import-Module .\MigrationShell.dll
This assembly references all other assemblies and classes that are serialized / deserialized.
When the JSON functions are used in the PowerShell context I get the following exception:
Error resolving type specified in JSON 'System.Collections.Generic.List`1[[Migration.Data.MediaGalleryItem, Migration]], mscorlib'. Path '$values[0].MediaGalleryItems.$type', line 7, position 133.
So it seems that JSON.NET is not able to resolve the type that is defined in the Migration.dll when my code is being called in PowerShell context.
How can I solve this issue?
Update: I just checked that there are no problems in resolving my custom object types. The problem seems to be the generic list. But still the error occurs only when calling the functionality in a PowerShell cmdlet context.