Libraries: JSON Serialization and Deserialization (native MQL)

To add comments, please log in or register
Automated-Trading
146033
Automated-Trading  

JSON Serialization and Deserialization (native MQL):

Serialization and deserialization of JSON protocol. The code is ported from a high-speed С++ library.

Practical example: authorization on a website and parsing the response

CJAVal jv;
jv["login"]="Login"; // login
jv["password"]="Pass"; // password

//--- serialize to string  {"login":"Login","password":"Pass"}
char data[]; 
ArrayResize(data, StringToCharArray(jv.Serialize(), data, 0, WHOLE_ARRAY)-1);

//--- send data
char res_data[];
string res_headers=NULL;
int r=WebRequest("POST", "http://my.site.com/Authorize", "Content-Type: text/plain\r\n", 5000, data, res_data, res_headers);

//--- assume the answer {"accessToken":"ABRAKADABRA","session_id":124521}
//--- get AccessToken
jv.Deserialize(res_data);
string AccessToken=jv["accessToken"].ToStr();


Author: o_O

Amy Liu
948
Amy Liu  

After update MetaEditor to Version: 5.00 build 1498, compiling the code generates error bellow:

cannot implicitly convert type 'string' to 'bool'.

Line 53: void operator=(string a) { m_type=a?jtSTR:jtNULL; m_sv=a; m_iv=StringToInteger(m_sv); m_dv=StringToDouble(m_sv); m_bv=a!=NULL; }

It seems that  m_type=a?jtSTR:jtNULL is not supported in this version.

Files:
Resophonic
25
Resophonic  
The author has published an update which addresses this issue.
Amy Liu
948
Amy Liu  
Amy Liu:

After update MetaEditor to Version: 5.00 build 1498, compiling the code generates error bellow:

cannot implicitly convert type 'string' to 'bool'.

Line 53: void operator=(string a) { m_type=a?jtSTR:jtNULL; m_sv=a; m_iv=StringToInteger(m_sv); m_dv=StringToDouble(m_sv); m_bv=a!=NULL; }

It seems that  m_type=a?jtSTR:jtNULL is not supported in this version.

Fixes:

Line  53: void operator=(string a) { m_type=a!=NULL?jtSTR:jtNULL; m_sv=a; m_iv=StringToInteger(m_sv); m_dv=StringToDouble(m_sv); m_bv=a!=NULL; }

Line 80: case jtSTR: m_sv=Unescape(a); m_type=m_sv!=NULL?jtSTR:jtNULL; m_iv=StringToInteger(m_sv); m_dv=StringToDouble(m_sv); m_bv=m_sv!=NULL; break;

Amy Liu
948
Amy Liu  
Resophonic:
The author has published an update which addresses this issue.
I have fixed this issue. But it will be good to see what the author did. How can I find it? Thanks.
Resophonic
25
Resophonic  
Amy Liu:
I have fixed this issue. But it will be good to see what the author did. How can I find it? Thanks.
You can download it here: https://www.mql5.com/en/code/13663
Amy Liu
948
Amy Liu  
Resophonic:
You can download it here: https://www.mql5.com/en/code/13663
Thank you very much!
spalle
23
spalle  
Hi there, firstly thanks for this very powerful tool - is there by any chance some documentation available anywhere? or some more examples of usage? Warm regards
Mohammad Mohi
320
Mohammad Mohi  
Hi there, thanks for this native MQL and fast library, it will be more helpful if there is a documentation, special when working with JSON arrays and objects, how to extract data from them:
Suppose:
[[0.88678,true],[0.88668,false]]
How can I map this to MQL types after Deserialize?

danielsokolowsk
69
danielsokolowsk  

Would anyone or the author answer if this is supposed to support nested JSON? That is a CJAVal instance contains a keys '2', and '3' which are another CJAVal instance. Code seems to run but when serialized the keys are blank.

Example, I am getting:

{"":{"sl":5270.98000000,"tp":5285.26000000,"order":2},"":{"sl":5274.80000000,"tp":5289.06000000,"order":3}}

But should be getting 

{"2":{"sl":5270.98000000,"tp":5285.26000000,"order":2},"3":{"sl":5274.80000000,"tp":5289.06000000,"order":3}}
Taras Slobodyanik
33069
Taras Slobodyanik  
danielsokolowsk:

Would anyone or the author answer if this is supposed to support nested JSON? That is a CJAVal instance contains a keys '2', and '3' which are another CJAVal instance. Code seems to run but when serialized the keys are blank.

Example, I am getting:


But should be getting 

https://www.mql5.com/ru/forum/63015

a few examples

   string a[] = {"1", "2", "3"};
   int b[] = {1, 2, 3};
   
   CJAVal js;
   js["a"].Add(a[0]);
   js["a"].Add(a[1]);
   js["a"].Add(a[2]);
        
   js["b"].Add(b[0]);
   js["b"].Add(b[1]);
   js["b"].Add(b[2]);
   
   js["c"][0]=1.1;
   js["c"][1]=2.9;
   js["c"][2]=3.03;
   
   string t=js.Serialize();
   Print(t);   // {"a":["1","2","3"],"b":[1,2,3],"c":[1.10000000,2.90000000,3.03000000]}   
   
   
   js.Clear();
   js.Deserialize(t); 
   Print(js["c"][2].ToStr()); //  3.03000000

array of arrays

JAval j1; j1.Add(1);

JAval j2; j2.Add(2); j2.Add(3);

JAval js;

js["a"].Add(j1);
js["a"].Add(j2);

// { a: [ [1], [2,3] ]; }
Библиотеки: JSON Serialization and Deserialization (native MQL)
Библиотеки: JSON Serialization and Deserialization (native MQL)
  • 2015.08.18
  • www.mql5.com
Статьи и техническая библиотека по автоматическому трейдингу: Библиотеки: JSON Serialization and Deserialization (native MQL)
12
To add comments, please log in or register