Why do you force json_decode?


#1

Hello,

Why do you force json_decode ?
https://github.com/couchbase/php-couchbase/blob/master/stub/CouchbaseBucket.class.php#L328

I should be able to receive raw data (json format or whatever) and not a json_decoded version.


#2

N1QL wire protocol uses JSON as an encoding, you cannot substitute it in a protocol. results array, is not an array of values, but rather array of objects, where key is a bucket name and value actual value, and if your value is binary, it will be binary string.


#3

I don’t understand.
In CouchBase i have :

{
    "type": "city",
     "name": "Palaiseau",
     "country": { "type": "country", "name": "France" }
}

When I do this

        $couchbaseResult = $this->bucket->query(\CouchbaseN1qlQuery::fromString('select * from `odm-test` as city limit 1'));
        var_dump($couchbaseResult);

The var_dump is

object(stdClass)#22 (3) {
  ["rows"]=>
  array(1) {
    [0]=>
    object(stdClass)#13 (1) {
      ["city"]=>
      object(stdClass)#19 (3) {
        ["country"]=>
        object(stdClass)#21 (2) {
          ["name"]=>
          string(6) "France"
          ["type"]=>
          string(7) "country"
        }
        ["name"]=>
        string(9) "Palaiseau"
        ["type"]=>
        string(4) "city"
      }
    }
  }
  ["status"]=>
  string(7) "success"
  ["metrics"]=>
  array(4) {
    ["elapsedTime"]=>
    string(11) "11.543773ms"
    ["executionTime"]=>
    string(11) "11.496255ms"
    ["resultCount"]=>
    int(1)
    ["resultSize"]=>
    int(239)
  }
}

I expected this one

object(stdClass)#22 (3) {
  ["rows"]=>
  array(1) {
    [0]=>
    '{
        "type": "city",
         "name": "Palaiseau",
         "country": { "type": "country", "name": "France" }
    }'
  }
  ["status"]=>
  string(7) "success"
  ["metrics"]=>
  array(4) {
    ["elapsedTime"]=>
    string(11) "11.543773ms"
    ["executionTime"]=>
    string(11) "11.496255ms"
    ["resultCount"]=>
    int(1)
    ["resultSize"]=>
    int(239)
  }
}

#4

Well, i found myself the solution.
I can change this with other transcoder : couchbase_default_decoder


#5

We use:
json_decode(json_encode($couchBucket->get("DOCUMENT_NAME")->value), true);

This returns an array that we can work with.


#6

And that use lots of ressource for nothing :wink:
If you want decode json as an array you can do this :

$bucket->query(\CouchbaseN1qlQuery::fromString($query), true);

The second argument “true” decode json as array.


#7

Thank you, optimisation is certainly not my strong point. I’ll roll this out! Got an issue though, should I PM you and see if you know what it is? Don’t want to steal your thread.


#8

Make a new thread, so everyone can participate, and i’ll try to give you an answer.


#9

Do you mean you don’t want the server to store your data as JSON, but rather as a string blob?


#10

I want store them as JSON, but retrieve them as JSON, not decoded.
I can do that with setTranscoder