Backslashes in json

47 views
Skip to first unread message

Gianluigi Belli

unread,
Jun 16, 2015, 7:34:19 AM6/16/15
to orient-...@googlegroups.com
Hi all,
I'm having some serious trouble with backslash characters within a json representation string in SQL queries.
It seems the parser is doubling backslashes in strings and it causes an exponential growth of them, at each update.

A simple example:
update person set test = {'@type':'d','a':'some \\ text'} where name = "john"

select test from person where name = "john"

and I get: {"@type":"document","@version":0,"a":"some \\\\ text"}

that it's wrong because the first backslash of the original json string is meant to escape the second single backslash.
see [2.5.  Strings] of https://tools.ietf.org/html/rfc4627

Now, if I get the output string from a select, I do my stuff, and then I recode it in a json representation string to get a record updated, the encoder will write an escaped version of the backslashes sequence (so they will become 4) and the SQL parser will double them again.
Have anyone  an idea how to prevent the parser to act in a such behaviour?

   Thanks

hartmut bischoff

unread,
Jun 16, 2015, 8:43:05 AM6/16/15
to orient-...@googlegroups.com
tried to verify your problem

wrote the following [quick&dirty] test (in my ruby-parser)


180     it "update strange text", focus:true do
181       strange_text = { strange_text: "'@type':'d','a':'some \\ text'"}
182 
183       res=  @r.create_or_update_document o_class: @rest_class , set: { a_new_property: 36 } , where: {con_id: 346, symbol: 'EWQrGZ' } do
184           strange_text   # <<< ----- strange text is inserted into the database
185       end
           ## res is the response from the database 
186       expect( res.strange_text ).to eq strange_text[:strange_text]
           ## lets be shure, reload the document by using the rid 
187       document_from_db =  @r.get_document res.rid
           ## and perform the test once more
188       expect( document_from_db.strange_text ).to eq strange_text[:strange_text]
189     end

14:35:51 - INFO - Running: spec/lib/rest_spec.rb
Run options: include {:focus=>true}

REST::OrientDB
  document-handling
D, [2015-06-16T14:35:53.037385 #89231] DEBUG -- REST::Base#initialize: property a_new_property assigned to REST::Model::Documebntklasse10
D, [2015-06-16T14:35:53.037697 #89231] DEBUG -- REST::Base#initialize: property con_id assigned to REST::Model::Documebntklasse10
D, [2015-06-16T14:35:53.037967 #89231] DEBUG -- REST::Base#initialize: property symbol assigned to REST::Model::Documebntklasse10
D, [2015-06-16T14:35:53.038185 #89231] DEBUG -- REST::Base#initialize: property strange_text assigned to REST::Model::Documebntklasse10
    update strange text

Finished in 1.21 seconds (files took 0.34114 seconds to load)
1 example, 0 failures


Anything works fine.

Thus – your programming environment is not setup correctly


p.s.
The ruby-interface handles the json-conversion with standard-equipment:  URI.encode does the transformation of incomming messages and content.to_json does the work for outgoing messages.  I don't think that ruby librarys differ much from those of other languages.

Gianluigi Belli

unread,
Jun 17, 2015, 7:53:01 AM6/17/15
to orient-...@googlegroups.com
Thank you Hartmut for your reply.
Well, you said my programming environment is misconfigured but I can't see how.
I've forgot to mention my enviroment specifications:
OS: Linux distro openSuse 13.2
Oracle java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
Orientdb 2.0.10 (but I've tested previous versions too).
Programming language PHP 5.6.7
I've tried PhpOrient driver (binary protocol) and Orientdb-ODM either (rest protocol)
I've tried to figure out if it is the driver which triggers the issue but it doesn't seem so.
I get the same behaviour from binary as well as from rest protocol too.

Studio acts in the same way and I get the same exact result from console.sh

I've wrote a similar script of yours but using PHP (PhpOrient driver):

require "vendor/autoload.php";
use PhpOrient\PhpOrient;

$client
= new PhpOrient( 'localhost', 2424 );
$ClusterMap
= $client->dbOpen( 'joshua4', 'admin', 'admin' );

$some_object
=array(
   
"stange_text"=>"some \ text"  
);

$strange_text
=json_encode($some_object);

echo
"Original text: ".$some_object['stange_text'].PHP_EOL;
echo
"OBJ json encoded: ".$strange_text.PHP_EOL;

$ret
=$client->command($sql="update person set test = $strange_text where name = 'bob'");

echo
"SQL command: ".$sql.PHP_EOL;

echo
"Command return:".PHP_EOL;
var_dump
($ret);

$ret
=$client->query( 'select test from person where name = "bob"' );

$vertex
=$ret[0]->getOData();

$res
=($i['test']['stange_text']===$some_object['stange_text']);

echo
"Match texts result:".PHP_EOL;
var_dump
($res);

echo
"Returned record:".PHP_EOL;
var_dump
($vertex);

and this is the output:

Original text: some \ text
OBJ json encoded
: {"stange_text":"some \\ text"}
SQL command
: update person set test = {"stange_text":"some \\ text"} where name = 'bob'
Command return:
string(1) "1"
Match texts result:
bool(false)
Returned record:
array
(1) {
 
'test' =>
  array
(1) {
   
'stange_text' =>
   
string(12) "some \\ text"
 
}
}



Any idea where it can be the misconfiguration? I haven't any clue.
Excluding drivers and PHP (because I'm noticing the same behaviour from Studio and console.sh) I've supposed it was the SQL parser of the DBMS acting wrongly.

It heppens not just on my developing machine but even on the production system.

hartmut bischoff

unread,
Jun 17, 2015, 9:30:55 AM6/17/15
to orient-...@googlegroups.com
Perhaps its the API.
https://github.com/topofocus/orientdb-rest   ist using the HTTP-REST interface mostly because the low-level binary driver is subject to many (and worst undocumentated and not properly communicated) changes.
The highl-level REST-Interface looks stable to me.

If you can manage to install ruby 2.x (rvm is not easily installed on openSuse) perhaps you give the ruby-driver a try.  The test is included in the test-suite there. Just install RSpec and run the tests.
Its  far from complete but does his job perfectly even in production here.

Gianluigi Belli

unread,
Jun 17, 2015, 9:36:17 AM6/17/15
to orient-...@googlegroups.com

Thank you. I'll give it a try as soon as possible and I let you know


--

---
You received this message because you are subscribed to a topic in the Google Groups "OrientDB" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/orient-database/EyD6aEW1c_o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages