Adaptive card with Luis intent - adaptive-cards

this approach was working for me nicely until last month. But now i am getting error msg that my bot code is havin an issue. Is there any changes to it recently. Here is my code>
protected override Task<string> GetLuisQueryTextAsync(IDialogContext context, IMessageActivity message)
{
if (message.Value != null)
{
dynamic value = message.Value;
// assuming your DataJson has a type property like :
// DataJson = "{ \"Type\": \"Curse\" }"
string submitType = value.x.ToString();
return Task.FromResult(submitType);
}
else
{
// no Adaptive Card value, let's call the base
return base.GetLuisQueryTextAsync(context, message);
}
}
Datajson> "x":"introduction"

When you debug into this method, which line does the error occur?
There are a number of places in the method that 'could' be potential problems.
Is 'message' null?
Are you sure message.Value is a properly formed json string?
Does the properly formatted json string actuall contain 'x'?
Let us know where in the method the error is occurring and what the exception is.

Related

JSON decoding from stream in Kotlin

I have a server set up to send messages over a local host port. I am trying to decode the serialized json messages sent by the server and get this error.
Error decoding message: kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 55: Expected EOF after parsing, but had instead at path: $
JSON input: .....mber":13,"Timestamp":5769784} .....
The Racer State messages are formatted in JSON as follows: { “SensorId”: “value”, “RacerBibNumber” : “value”, “Timestamp” : “value” }, where the value’s are character string representations of the field values. I have also tried changing my RacerStatus Class to take String instead of Int but to a similar error. Am I missing something here? The symbol that is missing in the error was not able to be copied over so I know it's not UTF-8.
I have also added
val inputString = bytes.toString(Charsets.UTF_8)
println("Received input: $inputString")
This gets
Received input: {"SensorId":0,"RacerBibNumber":5254,"Timestamp":3000203}
with a bunch of extraneous symbols at the end.
data class RacerStatus(
var SensorId: Int,
var RacerBibNumber: Int,
var Timestamp: Int
) {
fun encode(): ByteArray {
return Json.encodeToString(serializer(), this).toByteArray()
}
companion object {
fun decode(bytes: ByteArray): RacerStatus {
print(bytes[0])
try {
val mstream = ByteArrayInputStream(bytes)
return Json.decodeFromStream<RacerStatus>(mstream)
} catch (e: SerializationException) {
println("Error decoding message: $e")
return RacerStatus(0, 0, 0)
}
// return Json.decodeFromString(serializer(), mstream.readBytes().toString())
}
}
}
So I found an answer to my question. I added a regex to include just the json components I know my json contains.
val str = bytes.toString(Charsets.UTF_8)
val re = Regex("[^A-Za-z0-9{}:,\"\"]")
return Json.decodeFromString<RacerStatus>(re.replace(str,""))
I thought that Charsets.UTF_8 would remove the misc characters but it did not. Is there a more intiuative solution? Also is there a regex that would cover all possible values in json?

PHP7, JSON and Zend (decoding failed: syntax error)

I recently upgraded to PHP 7.0.4 and nginx 1.8.1, my application uses the Zend Framework (Magento 1.9.2.1). Since that upgrade, our customers sometimes get an "Decoding failed: Syntax error" when submitting an orderm which is thrown in
public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
{
$encodedValue = (string) $encodedValue;
if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
$decode = json_decode($encodedValue, $objectDecodeType);
// php < 5.3
if (!function_exists('json_last_error')) {
if (strtolower($encodedValue) === 'null') {
return null;
} elseif ($decode === null) {
#require_once 'Zend/Json/Exception.php';
throw new Zend_Json_Exception('Decoding failed');
}
// php >= 5.3
} elseif (($jsonLastErr = json_last_error()) != JSON_ERROR_NONE) {
#require_once 'Zend/Json/Exception.php';
switch ($jsonLastErr) {
case JSON_ERROR_DEPTH:
throw new Zend_Json_Exception('Decoding failed: Maximum stack depth exceeded');
case JSON_ERROR_CTRL_CHAR:
throw new Zend_Json_Exception('Decoding failed: Unexpected control character found');
case JSON_ERROR_SYNTAX:
throw new Zend_Json_Exception('Decoding failed: Syntax error');
default:
throw new Zend_Json_Exception('Decoding failed');
}
}
return $decode;
}
I read about a bug that PHP7 and JSON decode behaves differently when encoding an empty string. Does anyone know if this bug is related to PHP7 or to my application/server?
Thanks
This is an absolutely normal behaviour of json_decode.
It will throw this exception if the given string is not a valid JSON String.
As you already mentioned, an empty string is also not a valid JSON String.
json_decode('Hello') // invalid
json_decode("Hello") //invalid
But:
json_decode("'Hello'") // valid JSON string
Empty strings will raise an exception since PHP7!
"Calling json_decode with 1st argument equal to empty PHP string or value that after casting to string is empty string (NULL, FALSE) results in JSON syntax error."
So... to answer your question: In my opinion, your application has the problem you need to solve, if downgrading your PHP version is not an option.
The Magenta function needs to check for a valid representation of a JSON string BEFORE passing it to the json_decode function.
Try to set the variable to {} if it is not a string (is_string) or if it is empty.
If this is not the problem, it might be possible that your string is not encoded as it should be. Many times encoding of the strings passed to json_decode will result into that exception also.
One possible (dirty) fix is to set the build in decoder of Zend to true, while this may be much slower this does work on PHP7 where the default package errors out.
in class Zend_Json, set $useBuiltinEncoderDecoder to true;
public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
{
$encodedValue = (string) $encodedValue;
if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
This way the return Zend_Json_Decoder::decode($encodedValue, $objectDecodeType); will be used which seems to be working just fine.
Dirty hack:
In my case it was a problem in Magento 2.2 with PHP7 where an empty object would throw an error. It would prevent the product indexer from completing.
So I added an empty array return for this case (in file vendor/magento/zendframework1/library/Zend/Json.php):
public static function decode($encodedValue, $objectDecodeType = Zend_Json::TYPE_ARRAY)
{
$encodedValue = (string) $encodedValue;
if($encodedValue == "a:0:{}") { return []; } //<- added: return an empty array
if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
....
I have the same problem. I have a temporary fix as follows, waiting for a better solution
//return Zend_Json::decode($data->params);
return json_decode($data->params);
This was fixed in zend-mvc 3.1.1

Detect an empty [{}] json object in managed code

Our application uses json files for configuration.
If the file has configuration data we parse it and all goes well.
If the file has bad data [{ asdf 34453 %^$% dfgdsf }] we handle just fine
If the config file is missing then we handle that.
The scenario I'm trying to figure out is if it is an empty object such as [{}] I'm looking for a reliable way by checking the first Xnode, last Xnode, Xattribute..something because HasElements will return true for an empty object.
Please don't suggest using 3rd part libraries etc. Not happening as decided by others.
The code I am using:
private static bool TryParseJson(MemoryStream msJson)
{
bool IsValid = false;
XElement root;
XmlReader xReader = JsonReaderWriterFactory.CreateJsonReader(msJson, new XmlDictionaryReaderQuotas());
//an empty json file ( [] ) will parse but have no elements hence the elements check.
//a file full of giberish...incorrect json format will fail to parse and throw exception hence the try catch
try
{
root = XElement.Load(xReader);
}
catch
{
return IsValid;
}
//an empty file ( [{}] will return positive for HasElements so we also get ?????
XNode ???? = root.FirstNode;
if (root.HasElements && !???)
{
IsValid = true;
return IsValid;
}
return IsValid;
}
I think I have found a way to handle an empty object. My testing with valid json and empty json shows that an empty object [{}] will still return a single element but there will be NO nodes in that element so I went with this:
//an empty file ( [{}] will return positive for HasElements so we also get
bool IsEmptyObject = (root.Elements().Nodes().Count() > 0) ? true : false;
I'd still be interested in hearing the contributions of others.
Thanks

Grails: Parsing through JSON String using JSONArray/JSONObject

I have the below JSON string coming in as a request parameter into my grails controller.
{
"loginName":"user1",
"timesheetList":
[
{
"periodBegin":"2014/10/12",
"periodEnd":"2014/10/18",
"timesheetRows":[
{
"task":"Cleaning",
"description":"cleaning description",
"paycode":"payCode1"
},
{
"task":"painting",
"activityDescription":"painting description",
"paycode":"payCode2"
}
]
}
],
"overallStatus":"SUCCESS"
}
As you can see, the timesheetList might have multiple elements in it. In this ( above ) case, we have only one. So, I expect it to behave like an Array/List.
Then I had the below code to parse through it:
String saveJSON // This holds the above JSON string.
def jsonObject = grails.converters.JSON.parse(saveJSON) // No problem here. Returns a JSONObject. I checked the class type.
def jsonArray = jsonArray.timesheetList // No problem here. Returns a JSONArray. I checked the class type.
println "*** Size of jsonArray1: " + jsonArray1.size() // Returns size 1. It seemed fine as the above JSON string had only one timesheet in timesheetList
def timesheet1 = jsonArray[1] // This throws the JSONException, JSONArray[1] not found. I tried jsonArray.getJSONObject(1) and that throws the same exception.
Basically, I am looking to seamlessly iterate through the JSON string now. Any help?
1st off to simplify your code, use request.JSON. Then request.JSON.list[ 0 ] should be working

Json.Net boolean parsing issue

JObject.Parse(jsonString) is causing issue for boolean data. e.g. The json is :
{
"BoolParam": true
}
I used the following code to parse:
JObject data = JObject.Parse(str1);
foreach (var x in data)
{
string name = x.Key;
Console.Write(name + " (");
JToken value = x.Value;
Console.Write(value.Type + ")\n");
Console.WriteLine(value);
}
This print out the value as :
BoolParam (Boolean) : True
The case sensitivity causes issue as I save this json for later use. The saved json looks like
{
"BoolParam": True
}
However, when i later use it, the JObject.Parse(str) throws error as invalid Json :Unexpected character encountered while parsing value: T. Path 'BoolParam', line 2, position 15.
If I change the case from "True" to "true", it works. I dont want to add this hack to change the case when saving but is there a better way to handle this scenario.
I dont want to add this hack to change the case when saving but is
there a better way to handle this scenario.
No, you have to produce valid JSON when saving if you want to be able to later deserialize it with a JSON serializer such as Newtonsoft JSON. So fixing your saving routing is the right way to go here.
One could use anonymous types and no worry about case sensitivity of boolean type variables
public static void Main()
{
bool a = true;
JObject c = JObject.FromObject(new {BoolParam= a});
Console.WriteLine(c);
}
Output:
{
"BoolParam": true
}