3

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.

10
  • could this be a .net framework version issue? Assembly targeted on NET 4, powershell still running with 2.0/3.5? You can get the CLR version in powershell using environment variable $PSVersionTable Commented Aug 27, 2015 at 9:26
  • PowerShell is running with .NET 4 (4.0.30319.34209). My assemblies are targeting 4.5. May this be the problem? Commented Aug 27, 2015 at 9:44
  • no, NET 4.5 is still .NET 4 and your CLR version is that of NET 4.5.2, so that shouldn't be a problem. Can you manually create an object of type Migration.Data.MediaGalleryItem using New-Object in powershell? Just to make sure ps is actually complaining about that type and not about the generic list type. Commented Aug 27, 2015 at 9:56
  • Ah yes you are right. Currently it's not possible to create an object of that type via PowerShell. Commented Aug 27, 2015 at 10:03
  • then you could try creating a generic list of some known simple type like string to make sure that can be resolved. I have no experience with powershell modules, is your assembly actually a module? Or is it just a library? In that case, I've always loaded those like this in ps scripts: [System.Reflection.Assembly]::LoadFile($libPath) Commented Aug 27, 2015 at 10:07

1 Answer 1

4

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 :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.