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

 

Artem Maltsev, I recommend you to add my version, because those who downloaded the module will not be able to do so: .

if ((value = data["key1"]) == NULL) {
  Print("Value is NULL! Line: ", __LINE__);
  return;
}

if ((value = value[0]) == NULL) {
  Print("Value is NULL! Line: ", __LINE__);
  return;
}

if ((value = value["key2"]) == NULL) {
  Print("Value is NULL! Line: ", __LINE__);
  return;
}

if ((value = value[0]) == NULL) {
  Print("value is NULL! Line: ", __LINE__);
  return;
}

if ((value = value["key3"]) == NULL) {
  Print("Value is NULL! Line: ", __LINE__);
  return;
}
 

Can you tell me how to add rounding to double?


There is an option below, but it adds square brackets.

      CJAVal js(NULL,jtUNDEF);
      double d = 0.05;
      js["OrderLot"].Add(d,2); //результат "OrderLot":[0.05].   

How to get?

"OrderLot":0.05
 
Json sample


{
  "id": 54,
  "nome": "12.01 alterado",
  "condicoes": [
    {
      "id": 47,
      "estrategiaId": 54,
      "indicadorAId": 87,
      "indicadorA": {
        "id": 87,
        "nome": "iATR",
        "parametros": [
          {
            "id": 46,
            "nome": null,
            "valor": 10.0
          }
        ]
      },
      "retornoA": 0,
      "comparador": ">",
      "indicadorBId": 88,
      "indicadorB": {
        "id": 88,
        "nome": "iAMA",
        "parametros": [
          {
            "id": 47,
            "nome": "Aplicar a",
            "valor": 1.0
          },
          {
            "id": 48,
            "nome": "Periodo rapido",
            "valor": 2.0
          },
          {
            "id": 49,
            "nome": "Periodo lento",
            "valor": 3.0
          },
          {
            "id": 50,
            "nome": "Deslocalmento",
            "valor": 4.0
          },
          {
            "id": 51,
            "nome": "Aplicar a",
            "valor": 0.0
          }
        ]
      },
      "retornoB": 0
    },
    {
      "id": 48,
      "estrategiaId": 54,
      "indicadorAId": 90,
      "indicadorA": {
        "id": 90,
        "nome": "iATR",
        "parametros": [
          {
            "id": 52,
            "nome": null,
            "valor": 10.0
          }
        ]
      },
      "retornoA": 0,
      "comparador": ">",
      "indicadorBId": 89,
      "indicadorB": {
        "id": 89,
        "nome": "iAMA",
        "parametros": [
          {
            "id": 53,
            "nome": "Aplicar a",
            "valor": 1.0
          },
          {
            "id": 54,
            "nome": "Periodo rapido",
            "valor": 2.0
          },
          {
            "id": 55,
            "nome": "Periodo lento",
            "valor": 3.0
          },
          {
            "id": 56,
            "nome": "Deslocalmento",
            "valor": 4.0
          },
          {
            "id": 57,
            "nome": "Aplicar a",
            "valor": 0.0
          }
        ]
      },
      "retornoB": 0
    }
  ]
}


I need get array of key "condicoes" 
  "condicoes": [ { "id": 47, ... } , { "id": 48, ... }]

My code mql

 

   string pJS=" ... " // Json Sample
   js.Deserialize(pJS);
   Print(js["nome"].ToStr()); // is correct = 12.01 alterado
   Print(js["condicoes"].m_type); // is correct type 6 or jtARRAY
   Print(js["condicoes"].Size()); //not work ... return 1, but correct 2

Can help me !?

Tanks

 
Andre Luiz Medeiros Peixoto #:
Json sample




I need get array of key "condicoes" 
  "condicoes": [ { "id": 47, ... } , { "id": 48, ... }]

My code mql

 


Can help me !?

Tanks

It works properly on my computer. How do you put the json string into the pJS variable? 

 

@Artem Maltsev Thank you for creating this JSON library. Is it based on some C++ library?

Do you have some performance benchmarks on this? Also, does it internally use 1 byte or 2 byte chars to store the data?

 

When a value is null, Deserialize() sets the type to jtNULL, but doesn't assign NULL to m_sv, so if ToStr() method is compared to NULL it returns false:

#include <JAson.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   string serialized="{\"a\":1,\"b\":null}";
   CJAVal json;
   json.Deserialize(serialized);
   if(json["b"].ToStr()==NULL)
   {
      Alert("b is NULL");
   }
   else if(json["b"].ToStr()=="")
   {
      Alert("b is an empty string");
   }
   else
   {
      Alert("b is ", json["b"].ToStr());
   }
}

The output is:

2022.03.28 17:50:02.833 test-jsonNULLComparison (GBPUSD,M30) Alert: b is an empty string

The workaround would be comparing the type of value to jtNULL or change the source code at line 220 by adding "m_sv=NULL;":

if (i+3<slen) if (StringCompare(GetStr(js, i, 4), "null", false)==0) { m_sv=NULL; i+=3; return true; }
 

Good afternoon!

Cool library helps a lot, thank you very much.

I just encountered this problem - how to extract a substring from a string:

There is a string

{"type": "settings", "data":{"auto_trade":true, "stop_trade":true}}}

I need to pull out the data substring.

It doesn't work that way:

CJAVal js;

js.Deserialise(stroka);

js["data"].ToStr();

 
Ruslan Piraliyev #:

Good afternoon!

Cool library is very helpful, thank you very much.

I just encountered this problem - how to extract a substring from a string:

There is a string

{"type": "settings", "data":{"auto_trade":true, "stop_trade":true}}

I need to pull out the data substring.

It doesn't work that way:

CJAVal js;

js.Deserialize(stroka);

js["data"].ToStr();

I have found only one way to pull out the required substring so far:

js.Deserialize(stroka);

string data2=js["data"].Serialize();

js2.Deserialise(data2);

I.e. the required substring should be serialized and deserialised separately.

 
Sergey Likho #:

Can you tell me how to add rounding to double?

There is a variant below, but it adds square brackets.

How to get?

Try it like this:

      CJAVal js(NULL,jtUNDEF);
      double d = 0.05;
      js["OrderLot"] = d;
 

Unfortunately, something didn't work again.

How are you supposed to learn from codes that don't work?

#include <JAson.mqh>
 CJAVal jv;
//+------------------------------------------------------------------+
//||
//+------------------------------------------------------------------+
void OnStart()
  {
 
   string user = "Test User";
   string account="testuser mail";
   string EA="010000";
   bool reply=false;

   jv["user"]=user;
   jv["account"]=account;
   jv["itemnumber"]=EA;

   char data[];
   ArrayResize(data, StringToCharArray(jv.Serialize(), data, 0, WHOLE_ARRAY)-1);

//--- The transmission of data
   char res_data[];
   string res_headers=NULL;
   int r=WebRequest("POST", "meine adresse", "Content-Type: text/plain\r\n", 5000, data, res_data, res_headers);
//ArrayPrint(data);

//--- Suppose we take from the response {"reply": "true"}
//--- the AccessToken
   jv.Deserialize(res_data);
   string replyasstrinng=jv["isValid"].ToStr();
   Print("reply as string: ",replyasstrinng);
}

I have now tried it with GET and POST.

"meine adresse"

is of course the URL to a page on which the response is displayed as

{"isValid":true}

Or

{"isValid":false}

as the response.

However, if I use

  Print("reply as string: ",replyasstrinng);

absolutely nothing.

What is the reason for this?