Yalamarthi
Yalamarthi

Reputation: 11

unable to deserialize the object of type DialogManager(Microsoft.Bot.Builder.Dialogs)

private void LoadRootDialogAsync()
{
    var rootFile = this.resourceExplorer.GetResource("Main.dialog");
    this.rootDialog = DeclarativeTypeLoader.Load<AdaptiveDialog>(rootFile, this.resourceExplorer, this.sourceMap);
    this.dialogManager = new DialogManager(this.rootDialog);
}
string dm = JsonConvert.SerializeObject(this.dialogManager);
DialogManager dialogManager = JsonConvert.DeserializeObject<DialogManager>(dm);

but when i am trying to deserialize it, it is throwing

Could not create an instance of type Microsoft.Bot.Builder.Dialogs.Dialog. Type is an interface or abstract class and cannot be instantiated.

Please look into this

Upvotes: 0

Views: 230

Answers (1)

Kyle Delaney
Kyle Delaney

Reputation: 12274

You're almost certainly going about this the wrong way, which I will address in the linked GitHub issue, but since we're here I will answer the question you asked.

The JSON you're serializing does not contain enough information for Newtonsoft to know how to deserialize it. You're telling JsonConvert to deserialize that JSON as a DialogManager, and it sees that the RootDialog property is of the abstract type Dialog. Since it doesn't know what type of dialog the property contains, there's no way for it to be deserialized.

In order to get Newtonsoft to deserialize your DialogManager correctly, you must first serialize it correctly. You can do this by serializing it with type-name handling information.

var rootDialog = new WaterfallDialog("waterfall", new List<WaterfallStep>
{
    async (stepContext, cancellationToken) => await stepContext.EndDialogAsync()
});

var dm = new DialogManager(rootDialog);

var jss = new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All,
};

var json = JsonConvert.SerializeObject(dm, Formatting.Indented, jss);

Console.WriteLine(json);
Console.WriteLine(JsonConvert.DeserializeObject<DialogManager>(json, jss));

However, you will notice that the serialized waterfall dialog in this example contains only the ID and not the steps, so it still doesn't get serialized with all the information you need. This is because dialogs in the v4 SDK aren't meant to be serialized into JSON, and so you can't depend on JSON to contain all the information a dialog needs to work. You're probably going to need to find another way to do what you're trying to do by following instructions and samples about how adaptive dialogs are supposed to be used. Fortunately, there is a JSON schema that can be used to represent adaptive dialogs, so you might want to look into declarative dialogs.

Upvotes: 1

Related Questions