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
Related
I am using yeoman generator to create a code with the template, where the data for the template is passed in the option as a string showed as below,
yo express-no-stress myapp --data {"title":"Type"}
this string data can be parsed as JSON later in the install methods.
install() {
async app() {
let str = this.options.data;
if(str != undefined && typeof str == 'string') {
console.log(str);
let field_property = JSON.parse(str);
}
.....
} // parseapp
}// install
however when running the application, it always encounters an error saying
"syntaxError: Unexpected token t in JSON at position 1" in the JSON.parse() method.
I don't get why it causes this error, any idea, thanks.
You have to enclose your JSON in single quotes in the command line:
yo express-no-stress myapp --data '{"title":"Type"}'
Otherwise your shell tries to evaluate it.
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.
why would this string throw error in JSON.parse
[{"name":"listName","readonly":false,"value":"list"},{"name":"showHeader","readonly":true,"value":false},{"name":"showBorder","readonly":true,"value":false},{"name":"transparent","readonly":true,"value":true},{"name":"showTitle","readonly":false,"value":false},{"name":"showDesc","readonly":false,"value":false},{"name":"showMods","readonly":false,"value":false},{"name":"showTools","readonly":false,"value":true}]
This is the code. the above string is returned via AJAX as widgetInstance.data
if ($scope.widgetInstance.widgetId == 6)
{
$scope.widgetData = JSON.parse($scope.widgetInstance.data);
} else {
$scope.widgetData = JSON.parse($scope.widgetInstance.dataSanitized);
}
I had the same problem once in java while I tried to parse a json object decrypted with an RSA key. Because of the padding, the decrpytion had result in many trailing "\0" at the end of the string. This leads to the json parser error. Perhaps you are in the same trouble?
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
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
}