vicara-hq / kai-dotnet

The .NET client for using the Kai SDK.
https://vicara.co
4 stars 6 forks source link

GetObjectAs<T> throwing ArgumentException for multiple data types #4

Closed r4reejh closed 5 years ago

r4reejh commented 5 years ago

Encountered problems with types:

modifying code from:

var type = input.GetObjectAs<string>(Constants.Type);

to

var type = input[Constants.Type].ToObject<string>();

eliminates the error.

Error encountered when Parsing incoming data:

|Error|WebSocket.messagec|System.ArgumentException: Can not convert Object to String.
                               at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
                               at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)
                               at Newtonsoft.Json.Linq.JToken.ToObject[T]()
                               at Kai.Module.JObjectUtils.GetObjectAs[T](JObject jObject, String key, Boolean& success, T defaultValue, JTokenType unknownType)
                               at Kai.Module.JObjectUtils.GetObjectAs[T](JObject jObject, String key, T defaultValue, JTokenType unknownType)
                               at Kai.Module.KaiListener.ParseIncomingData(JObject input)
                               at Kai.Module.KaiListener.Handle(String data)
                               at Kai.Module.KaiListener.OnWebSocketMessage(Object sender, MessageEventArgs e)
                               at WebSocketSharp.Ext.Emit[TEventArgs](EventHandler`1 eventHandler, Object sender, TEventArgs e)
rakshith-ravi commented 5 years ago

I've just pushed a fix. Could you test this and let me know if it works? I'll test properly once I'm done with the SDK. Once the SDK gets done, we'll have a lot more freedom to work with these things. We're kinda crippled without the SDK right now. I'll get that done ASAP.

r4reejh commented 5 years ago

still throwing error

|Error|WebSocket.messagec|System.ArgumentException: Can not convert Object to String.
                             at Newtonsoft.Json.Linq.JToken.op_Explicit(JToken value)
                             at Newtonsoft.Json.Linq.JToken.ToObject(Type objectType)
                             at Newtonsoft.Json.Linq.JToken.ToObject[T]()
                             at Kai.Module.KaiListener.Handle(String data)
                             at WebSocketSharp.Ext.Emit[TEventArgs](EventHandler`1 eventHandler, Object sender, TEventArgs
e)
                             at WebSocketSharp.WebSocket.messagec(MessageEventArgs e)
rakshith-ravi commented 5 years ago

Is there an example Json that I can use to reproduce this?

r4reejh commented 5 years ago

I got it for this {"type":"authenticate", "success":true, "authenticated":true}

rakshith-ravi commented 5 years ago

Reverted back to ToObject<T>() under a try/catch. It's less verbose, doesn't explain a lot, but will do for now until that function is implemented properly