Skip to content
This repository was archived by the owner on Jan 15, 2025. It is now read-only.
This repository was archived by the owner on Jan 15, 2025. It is now read-only.

Newtonsoft.Json.JsonReaderException - Unexpected character encountered while parsing value when trying to utilize #1257

Open

Description

@MCKRUZ

Versions

I'm using version 4.13.3 of the CLI
Version "luis_schema_version": "7.0.0", of the schema.

Describe the bug

I exported out the attached
PS-CustomerService-Test.txt
json file into a new class and then ran this code.

var recognizerResult = await _botServices.Dispatch.RecognizeAsync<LuisModel>(stepContext.Context, cancellationToken);

Where _botServices.Dispatch is of type LuisRecognizer

It's failing in this piece of code

var app = JsonConvert.DeserializeObject<LuisModel>(
    JsonConvert.SerializeObject(
        result,
        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Error = OnError }
    )
);

This is the error log

Newtonsoft.Json.JsonReaderException
HResult=0x80131500
Message=Unexpected character encountered while parsing value: [. Path 'entities.BugReport_ML[0].Description[0].Bug', line 1, position 390.
Source=Newtonsoft.Json
StackTrace:
at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
at Newtonsoft.Json.JsonTextReader.ReadAsString()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonSerializer.Deserialize(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at Luis.LUISModel.Convert(Object result) in C:\Users\kruz7\Desktop\Demo5\Demo\Demo\After\PluralsightBot\PluralsightBot\Models\LUISModel.cs:line 94
at Microsoft.Bot.Builder.AI.Luis.LuisRecognizer.d__27`1.MoveNext()

This exception was originally thrown at this call stack:
[External Code]
Luis.LUISModel.Convert(dynamic) in LUISModel.cs
[External Code]

Activity

  1. added
    bug
    needs-triage
    on Jun 29, 2021
  2. added
    customer-reported
    Bot Services
    on Jun 30, 2021
  3. tsuwandy commented on Jun 30, 2021

    @tsuwandy
    Contributor

    Matt Kruczek (@MCKRUZ), could you clarify? You used bf luis to esport a LUIS app? The bot runs against a LUIS Dispatch app, correct? This error is run time error.

  4. MCKRUZ commented on Jun 30, 2021

    @MCKRUZ
    Author

    I exported out the LUIS XML using the LUIS gui then created the class using the BF CLI code generator, yes it runs against a dispatch app that is correct. And yes it's a runtime error also correct.

  5. MCKRUZ commented on Jul 2, 2021

    @MCKRUZ
    Author

    So I figured out the issue as to the "why" it's failing..

    // <auto-generated>
    // Code generated by luis:generate:cs
    // Tool github: https://github.com/microsoft/botframework-cli
    // Changes may cause incorrect behavior and will be lost if the code is
    // regenerated.
    // </auto-generated>
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    using System;
    using System.Collections.Generic;
    using Microsoft.Bot.Builder;
    using Microsoft.Bot.Builder.AI.Luis;
    namespace Luis
    {
        public partial class LuisModel : IRecognizerConvert
        {
            [JsonProperty("text")]
            public string Text;
    
            [JsonProperty("alteredText")]
            public string AlteredText;
    
            public enum Intent
            {
                GreetingIntent,
                NewBugReportIntent,
                None,
                QueryBugTypeIntent
            };
            [JsonProperty("intents")]
            public Dictionary<Intent, IntentScore> Intents;
    
            public class _Entities
            {
                // Built-in entities
                public DateTimeSpec[] datetime;
                public string[] phonenumber;
    
                // Lists
                public string[][] BugTypes_List;
    
    
                // Composites
                public class _InstanceDescription
                {
                    public InstanceData[] Bug;
                }
                public class DescriptionClass
                {
                    public string[] Bug;
                    [JsonProperty("$instance")]
                    public _InstanceDescription _instance;
                }
                public DescriptionClass[] Description;
    
                public class _InstanceBugReport_ML
                {
                    public InstanceData[] CallbackTime;
                    public InstanceData[] PhoneNumber;
                    public InstanceData[] Description;
                }
                public class BugReport_MLClass
                {
                    public string[] CallbackTime;
                    public string[] PhoneNumber;
                    public DescriptionClass[] Description;
                    [JsonProperty("$instance")]
                    public _InstanceBugReport_ML _instance;
                }
                public BugReport_MLClass[] BugReport_ML;
    
                // Instance
                public class _Instance
                {
                    public InstanceData[] Bug;
                    public InstanceData[] BugReport_ML;
                    public InstanceData[] BugTypes_List;
                    public InstanceData[] CallbackTime;
                    public InstanceData[] Description;
                    public InstanceData[] PhoneNumber;
                    public InstanceData[] datetime;
                    public InstanceData[] phonenumber;
                }
                [JsonProperty("$instance")]
                public _Instance _instance;
            }
            [JsonProperty("entities")]
            public _Entities Entities;
    
            [JsonExtensionData(ReadData = true, WriteData = true)]
            public IDictionary<string, object> Properties { get; set; }
    
            public void Convert(dynamic result)
            {
                var app = JsonConvert.DeserializeObject<LuisModel>(
                    JsonConvert.SerializeObject(
                        result,
                        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Error = OnError }
                    )
                );
                Text = app.Text;
                AlteredText = app.AlteredText;
                Intents = app.Intents;
                Entities = app.Entities;
                Properties = app.Properties;
            }
    
            private static void OnError(object sender, ErrorEventArgs args)
            {
                // If needed, put your custom error logic here
                Console.WriteLine(args.ErrorContext.Error.Message);
                args.ErrorContext.Handled = true;
            }
    
            public (Intent intent, double score) TopIntent()
            {
                Intent maxIntent = Intent.None;
                var max = 0.0;
                foreach (var entry in Intents)
                {
                    if (entry.Value.Score > max)
                    {
                        maxIntent = entry.Key;
                        max = entry.Value.Score.Value;
                    }
                }
                return (maxIntent, max);
            }
        }
    }
    
    

    The Description Class above should read

     public string[][] Bug;
    

    instead of

     public string[] Bug;
    

    Then it's all unicorns and rainbows. Not sure how this translates in the code generator but hopefully this helps.

  6. axelsrz commented on Jul 7, 2021

    @axelsrz
    Member

    Hello Matt Kruczek (@MCKRUZ) thanks for submitting this. We will provide a fix in our next milestone R15. Thanks

  7. added
    customer-replied-to
    on Jul 7, 2021
  8. added this to the R15 milestone on Jul 7, 2021
  9. added
    P1
    on Sep 20, 2021
  10. added
    backlog
    on Sep 23, 2021
  11. modified the milestones: R15, r16 on Sep 23, 2021
  12. jag43 commented on Jan 14, 2022

    @jag43

    Just wanted to say I encountered the same error following this pluralsight course link

    The above workaround fixed it 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Bot ServicesP1backlogbugcustomer-replied-tocustomer-reportedneeds-triage

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions