Download MetaTrader 5

Libraries: JSON Serialization and Deserialization (native MQL)

To add comments, please log in or register
Automated-Trading
Admin
111011
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
801
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:
Andrey Osorgin
Moderator
59
Andrey Osorgin  
The author has published an update which addresses this issue.
Amy Liu
801
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
801
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.
Andrey Osorgin
Moderator
59
Andrey Osorgin  
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
801
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
332
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?

To add comments, please log in or register