2

I'm trying to deserialize json using they $type property. However, I get an error stating "Type specified in JSON was not resolved. I'm not sure what I'm doing wrong. Any help is greatly appreciated.

My JSON

{
movies:
[
    {
        $type:"RTMoviePageWithSlides",
        title:"Reservoir Dogs",
        slides:[
            {$type:"RTCharacterPage", title:"Mr. Orange",       img:""},
            {$type:"RTCharacterPage", title:"Mr. Blonde",       img:""},
            {$type:"RTCharacterPage", title:"Mr. White",        img:""},
            {$type:"RTCharacterPage", title:"Mr. Pink",         img:""},
            {$type:"RTCharacterPage", title:"Nice Guy Eddie",   img:""},
        ]
    }
    {
        $type:"RTMoviePageWithSubpages",
        title:"Jackie Brown",
        pages:[
            {$type:"RTActorPage", title:"Pam Gier",             other_movies:[]},
            {$type:"RTActorPage", title:"Samuel L. Jackson",    other_movies:[]},
            {$type:"RTActorPage", title:"Robert Forester",      other_movies:[]}
        ]
    }
]
}

My Classes

using System.Collections;
using System.Collections.Generic;

public class RTMovieData {
    public List<RTMoviePage> movies;    
}

public class RTMoviePage {
    public string title;
}

public class RTMoviePageWithSlides : RTMoviePage{
    public List<RTMoviePage> slides = new List<RTSlidePageData>{};
}

public class RTMoviePageWithSubpages : RTMoviePage{
    public List<RTMoviePage> pages = new List<RTPageData>{};
}

public class RTCharacterPage : RTMoviePage
{
    public string img;
}

public class RTActorPage : RTMoviePage
{
    public  List<string>other_movies;
}

Deserialization code

var settings = new JsonSerializerSettings();
    settings.TypeNameHandling = TypeNameHandling.Auto;

    RTMovieData data = JsonConvert.DeserializeObject<RTMovieData>(jsonString, settings);

@Programmer mentions the post "Deserialization of JSON using MiniJSON" in Unity C#" as a duplicate to this post. However, the two posts are not related at all. The other post describes a user who was using a different JSON deserializer and was was having a problem getting an array to deserialize. My issue was specific to using the $type property to help the deserializer instantiate subclasses which is stated in the title of this post.

znerolnoht
  • 122
  • 11

1 Answers1

1

Solution: In my JSON I needed to add "Assembly-CSharp" to the value of the $type property.

$type:"RTMoviePageWithSlides, Assembly-CSharp"

and

$type:"RTCharacterPage, Assembly-CSharp"
znerolnoht
  • 122
  • 11