Download MetaTrader 5

Libraries: JSON Serialization and Deserialization (native MQL)

To add comments, please log in or register
Share your knowledge. Become a well-known developer!
Automated-Trading
Admin
99094
Automated-Trading 2015.10.12 14:47 

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
640
Amy Liu 2016.12.22 08:12  

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:
Andrey Osorgin
Moderator
70
Andrey Osorgin 2016.12.22 08:45  
The author has published an update which addresses this issue.
Amy Liu
640
Amy Liu 2016.12.22 08:57  
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
640
Amy Liu 2016.12.22 09:00  
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.
Andrey Osorgin
Moderator
70
Andrey Osorgin 2016.12.22 09:05  
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
640
Amy Liu 2016.12.22 09:17  
Resophonic:
You can download it here: https://www.mql5.com/en/code/13663
Thank you very much!
To add comments, please log in or register