Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.NET error resolving type in PowerShell cmdlet

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.

like image 581
Robin Güldenpfennig Avatar asked Aug 11 '26 02:08

Robin Güldenpfennig


1 Answers

I found a solution for my problem. The issue hereby is the the way of how JSON.NET resolves type names. I think it's not JSON.NET fault at all because it seems so that the PowerShell domain handles the type resolving in a different way.

The point is that I needed to provide the fully qualified type name instead of the short one for serializing and deserializing.

So I changed the serializer settings to this:

var settings = new JsonSerializerSettings
{
    TypeNameAssemblyFormat = FormatterAssemblyStyle.Full,
    TypeNameHandling = TypeNameHandling.All
};

So the JSON changed from this type declaration

"$type": "System.Collections.Generic.List`1[[Migration.Data.MediaGalleryItem, Migration]], mscorlib"

to this one

"$type": "System.Collections.Generic.List`1[[Migration.Data.MediaGalleryItem, Migration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

Everything works fine now. Thanks to Dirk for his comments :)

like image 125
Robin Güldenpfennig Avatar answered Aug 12 '26 17:08

Robin Güldenpfennig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!