Libraries: JSON Serialization and Deserialization (native MQL) - page 2

 
Виктор Василюк:
What do we do now? Is there any other way to solve this?
Don't look for a one-size-fits-all solution. Look for a different solution for each problem.
 
Виктор Василюк:

Thanks for the lib, but there are some problems.

I can't figure out how I can stick an array using the library?

For example, there are

string a[] = {"1", "2", "3"};
int b[] = {1, 2, 3};
double c[] = {1.1, 2.9, 3.03};

The problems arise at the level of declaring a variable of CJAVal type. Using CJAVal primer(jtARRAY, a); does not help.


Could you please supplement or expand the examples file ?????


Greetings Victor,

Thank you for your remark. Yes indeed, the library version does not have a function to add array elements explicitly.

The library has been updated.

Version 1.03 will be available in Codebase after verification.

You can download 1.03 attached to the post for now.


Working example for an array:

  string a[] = {"1", "2", "3"};
  int b[] = {1, 2, 3};
  double c[] = {1.1, 2.9, 3.03};

  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"].Add(c[0]);
  js["c"].Add(c[1]);
  js["c"].Add(c[2]);

  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["a"].m_e[2].ToStr()); // Вывод   3


Files:
JAson.mqh  16 kb
 

Alexey, thank you for your labours! Very necessary library!!!

I have such a practical task.

There is 1 CJAVal object of database type. And there are 2 CJAVal objects of order properties type.

I want the database to contain data about these orders.

Approximately like this:

//--- objects
   CJAVal jv_database, // DB
   jv_order_1,         // order 1
   jv_order_2;         // warrant 2
//--- DB
   jv_database["Result"]="Success";
   jv_database["ErrorMessage"]="No error";
   // jv_database["Response"][2]={jv_order_1,jv_order_2}; // WANTED.
//--- order 1
   jv_order_1["symbol"]="EURUSD";
   jv_order_1["ticket"]="1505254";
   jv_order_1["open_price"]="1.1065";
   jv_order_1["open_time"]="19.09.2016 11:53:31";
//--- warrant  2
   jv_order_2["symbol"]="USDJPY";
   jv_order_2["ticket"]="1505321";
   jv_order_2["open_price"]="100.02";
   jv_order_2["open_time"]="20.09.2016 14:12:53";

I would like the Response field to be an array of order properties.

And that such a record jv_database["Response"][idx] should return a pointer or the order object itself.

Please advise. Thanks

 
Dennis Kirichenko:
And that such a record jv_database["Response"][idx] should return a pointer or the order object itself.

Do I understand correctly that we are talking about a reference by a numeric index? Should a pointer to an object from DB["Response"] array be returned at index [idx]?

I think you can.

---

but initialisation by type {x1, x2} - probably can't be done. As I remember, this type of initialisation is available only at the moment of variable declaration, i.e. as a form of constructor. But not as a form of setting values in the created array.

That's why you will have to do it this way:

jv_database["Response"][0]=jv_order_1;
jv_database["Response"][1]=jv_order_2;
 
o_O:

Do I understand correctly that we are talking about handling by numeric index? Should a pointer to an object from DB["Response"] array be returned by index [idx]?

I think you can.

Yes, that's right.
 

Another question. There is an object of orders:

CJAVal order;
order["symbol"];
order["type"];
order["lot"];
order["price_open"];
order["price_close"];
order["stop_loss"];
order["take_profit"];

The source line from which we read contains information about several orders. It is not known in advance how many of them there are.

How can I read all the information from the string without specifying the size for the order object?

 

What line are we talking about?

 
o_O:

what string are we talking about?

A string containing data from the database for all orders (from the server).
 

Wrong answer.)

I need to see exactly what you want to parse, is it json with an array or are you making up your own. It's hard to guess.

 

That's the kind of string:

{
     "symbol": "USDJPY",
     "type": "Buy",
     "lot": 0.5,
     "price_open": 102.36,
     "price_close": 102.44,
     "stop_loss": 99.25,
     "take_profit": 103.25 
    },
    {
     "symbol": "EURUSD",
     "type": "Sell",
     "lot": 0.2,
     "price_open": 1.1044,
     "price_close": 1.1252,
     "stop_loss": 1.1434,
     "take_profit": 1.0922
    }
}