json helper error: encode() on a non-object

Permalink
Hi,

I am trying to use json helper to encode one array:
$json = Loader::helper("json");
$j=0;
        foreach ($pages as $i ){
                $result[$j]['ID'] = $i->getCollectionID();
                $result[$j]['title'] = $i->getCollectionName();
                $result[$j]['desc']= $i->getCollectionDescription();
                $result[$j]['date'] = $i->getCollectionDatePublic('d/m/Y');
        $j++;
        }

When I try
echo $json->encode($result)
I get error: Call to a member function encode() on a non-object.

When trying to use PHP's json_encode, I get result but my special characters form croatian language are getting escaped with unicode numbers.

My concrete is upgraded from 5.4 to 5.6, and server is CentOS with PHP 5.3 version. I cannot use PHP json_encode with JSON_UNESCAPE as this version doesn't suport it and also, I would rather like to stick with concrete's helpers.
My database is UTF8 formated (that is what mb_detect_encoding is reporting )

 
JohntheFish replied on at Permalink Reply
JohntheFish
You could try installing the 5.6.3.2 version of the json helper as an override and use that.

That way, you would still be using the core and would keep future update compatibility through to 5.6.3.2.
eOne replied on at Permalink Reply
Firstly, thank you for your reply and assistance.

I tried to copy files from 5.6.3.2 to my updates/5.6.3.1 directory.
Also, I tried to copy them into main directory as an override, but no luck

I was copying:
concrete/core/helpers/json.php
concrete/helpers/json.php
concrete/libraries/3rdparty/JSON

am I missing something ?

Finaly I upgraded manually to 5.6.3.2 but I still get same error. Any ideas ?
JohntheFish replied on at Permalink Reply
JohntheFish
I have used code like below with the json helper many times without any problems. However, it is effectively the same as what you have already done, so while it may be worth trying, I don't see how it would make a difference.
$result=array();
foreach ($pages as $i ){
  $info = array()  
  $info['ID'] = $i->getCollectionID();
  $info['title'] = $i->getCollectionName();
  $info['desc']= $i->getCollectionDescription();
  $info['date'] = $i->getCollectionDatePublic('d/m/Y');
  $result[]=$info;
}


The other thing you could try is html escaping the text before adding it to $info, so any special characters are already taken care of before converting to json.
eOne replied on at Permalink Reply
Still same issue.

Tried fresh install with 5.6.3.2 and still same C5 json error: "...on a non-object"

I might pull off with PHP default routine if my custom ( croatian ) characters are not escaped. I don't know if it might be some database thing ? I set collation utf8_unicode_ci and other test was utf8_general_ci but same issue.
Is there some specific thing in the way C5 writes extended characters to database maybe ?
JohntheFish replied on at Permalink Reply
JohntheFish
You could be right, that some special character is throwing PHP's json encoding. I don't know enough to offer expertise on that. You would need someone like @mlocati or @remo who are heavily involved in internationalisation to help.

As an experiment, you could try various escaping of the strings before encoding. The risk is that something could get double-escaped and lead to problems in the javascript.