Questions on OOP in MQL5 - page 63

 
Igor Makanu:

put it in the Buy More.

imho, it's the right thing!

json is a well-established format, it's convenient for me to have 100% one-to-one classes in C# and in MQL , and to organize loading models into runtime - so far there's a deadlock in that direction

and pass binary data here and there.... Well, it's not a novelty and only lazy person can do it)))


ZS: Python also seems to work with json without any problems, i don't know what Metaquotes has given to pythonists with its new MT5 capabilities - just pump out historical data? - whats the point? it was not the problem, imho

Laziness. I don't need it (json) yet.

 

C#

namespace ConsoleApp4
{

    public class Rootobject
    {
        public Test test { get; set; }
        public int a { get; set; }
        public long b { get; set; }
    }

    public class Test
    {
        public int a { get; set; }
        public float b { get; set; }
        public string t { get; set; }
    }


    class Program
    {
        static void Main(string[] args)
        {
            var obj = new Rootobject { a = 555, b = long.MinValue, test = new Test { a = 10, b = 0.369 F, t = "Hi" } };
            string json = JsonSerializer.Serialize<Rootobject>(obj);
            Console.WriteLine(json);
            Console.ReadLine();
        }
    }
}

result:

C#

{"test":{"a":10,"b":0.368999988,"t":"Hi"},"a":555,"b":-9223372036854775808}

MQL:

{"test":{"a":10,"b":0.369,"t":"Hi"},"a":555,"b":-9223372036854775808}


definitely a WILDERFUL!

;)

 

I suspect I'm already boring you with my wishes, but I need arrays of objects

on my own, I can't ((()

need such an example:

class CJSon{
   string text;
public:
   CJSon():text(NULL){}
   CJSon* Push(string key,string value) {text+=COMMA+"\""+key+"\":\""+value+"\""; return &this;}
   CJSon* PushStruct(string key,string value) {text+=COMMA+"\""+key+"\":"+value; return &this;}
   CJSon* Push(string key,long value)   {text+=COMMA+"\""+key+"\":"+(string)value; return &this;}
   CJSon* Push(string key,double value) {text+=COMMA+"\""+key+"\":"+(string)value; return &this;}
   string Finish()   {return text+="}";}
};
#undef COMMA

class _CTest{
public:
   int a;
   double b;
   string t;
   _CTest(int _a,double _b,string _t):a(_a),b(_b),t(_t){}
   string JSon()  {return (CJSon()).VALUE(a).VALUE(b).VALUE(t).Finish();}
};

class CTest{
   _CTest test[5];
public:
   int a;
   long b;
public:
   CTest():a(555),b(LONG_MIN)
   { 
      for(int i=ArraySize(test)-1; i>=0; i--) 
         {
            test[i].a = i;
            test[i].b = (double)i/10.0;
            test[i].t = (string)(i*2); 
         }
   }
   string JSon() {return (CJSon()).STRUCT(test).VALUE(a).VALUE(b).Finish();}
};

the result should be:

{"test":[{"a":4,"b":0.4,"t":"8"},{"a":3,"b":0.3,"t":"6"},{"a":2,"b":0.2,"t":"4"},{"a":1,"b":0.1,"t":"2"},{"a":0,"b":0.0,"t":"0"}],"a":555,"b":-9223372036854775808}

 
Igor Makanu:

I suspect I'm already bored with my wishes, but I need arrays of objects
myself, I can't (((

Please tell me, maybe I missed it somewhere, but still,
what exactly is your problem with the ready-made json serialisation/deserialisation solution?

JSON Serialization and Deserialization (native MQL)
JSON Serialization and Deserialization (native MQL)
  • www.mql5.com
Сериализация и десериализация JSON-протокола. Портированный код со скоростной библиотеки С++. Практичный пример: авторизация на сайте и парсинг ответа Благодарности принимаются в виде примеров, кто как применяет MQL для работы с веб-ресурсами. Поделитесь своим опытом работы с JSON в MQL. В протокол добавлены функции Escape / Unescape...
 
Sergey Dzyublik:

Can you tell me if I missed it somewhere, but still,
what exactly is your problem with an out-of-the-box json serialisation/deserialisation solution?

The KB solution allows you to generate object fields in json by yourself.

the solution proposed by@Vladimir Simakov makes it more convenient

 
Igor Makanu:

the KB solution implies independent generation of object fields in json
in the solution proposed by@Vladimir Simakov this is done more conveniently

You have a very specific understanding of the term "independent generation":

class _CTest{
public:
   int a;
   double b;
   string t;
   _CTest(int _a,double _b,string _t):a(_a),b(_b),t(_t){}
   string JSon()  {return (CJSon()).VALUE(a).VALUE(b).VALUE(t).Finish();}
};

Apparently it's more convenient to have your task solved for you and you've got the taste for it...

 
Sergey Dzyublik:

You have a very specific understanding of the term 'self-forming':

Apparently the solution is more convenient because you have had your problem solved for you and you've got a taste for it...

It's hard to suggest what drives you, if you wanted to help, you helped, if you didn't want to, you closed the thread and went on with your business

and once again trying to discuss personal characteristics of forum members.... you should get married, sirin" - there will be someone to take a ride over your head when you have nothing better to do.


for the point:

i'm trying to figure out where to put the square bracket, but the results are disappointing.

#define ARRCOMMA (arrtext==NULL?"[":",")
...
CJSon* PushArray(string key,string value) {text+= COMMA + "\""+key+"\":"+ ARRCOMMA + value;  return &this;}

if I get bored with the selection, I'll stick [] in with dirtier tricks )))

 
Igor Makanu:

It's hard to guess what motivates you, if you wanted to help, you helped, if you didn't want to, you closed the thread and went about your business.

and once again try to discuss personal characteristics of forum participants.... in general, "you should get married, sir!" (s) - there will be someone to pick your brains when you have nothing better to do.


for the point:

i'm trying to figure out where to put the square bracket, but the results are disappointing.

if I get bored with the selection, I'll stick [] in with dirtier tricks )))

#define  COMMA (text==NULL?"{":",")
#define  VALUE(dVal) Value(#dVal,dVal)
#define  STRUCT(dVal) Struct(#dVal,dVal)
#define  STRUCT_PTR(dVal) StructPtr(#dVal,dVal)
#define  ARRAY(dVal) Array(#dVal,dVal)
#define  ARRAY_STRUCT(dVal) ArrayStruct(#dVal,dVal)
#define  ARRAY_STRUCT_PTR(dVal) ArrayStructPtr(#dVal,dVal)

class CJSon{
   string text;
public:
   CJSon():text(NULL){}
   CJSon* Manual(string key,string _text) {text+=COMMA+"\""+key+"\":"+_text; return &this;}
   template<typename T>
   CJSon* Value(string key,T value) {text+=COMMA+"\""+key+"\":"+_Text(value); return &this;}
   template<typename T>
   CJSon* Struct(string key,T &value) {text+=COMMA+"\""+key+"\":"+value.JSon(); return &this;}
   template<typename T>
   CJSon* StructPtr(string key,T* value) {text+=COMMA+"\""+key+"\":"+(!CheckPointer(value)?"null":value.JSon()); return &this;}
   template<typename T>
   CJSon* Array(string key,T &value[]){
      text+=COMMA+"\""+key+"\":[";
      for (int i=0,count=ArraySize(value);i<count;text+=_Text(value[i++])+",");
      StringSetCharacter(text,StringLen(text)-1,']');
      return &this;}
   template<typename T>
   CJSon* ArrayStruct(string key,T &value[]){
      text+=COMMA+"\""+key+"\":[";
      for (int i=0,count=ArraySize(value);i<count;text+=value[i++].JSon()+",");
      StringSetCharacter(text,StringLen(text)-1,']');
      return &this;}
   template<typename T>
   CJSon* ArrayStructPtr(string key,T &value[]){
      text+=COMMA+"\""+key+"\":[";
      for (int i=0,count=ArraySize(value);i<count;++i) text+=(CheckPointer(value[i])==POINTER_INVALID?"null":value[i].JSon())+",");
      StringSetCharacter(text,StringLen(text)-1,']');
      return &this;}
   string Finish()   {return text+="}";}
private:
   string _Text(string value) {return "\""+value+"\"";}
   template<typename T>
   string _Text(T value) {return (string)value;}
};

#undef  COMMA

Fixed it and added processing of pointers to serializable objects at the same time.

PS. Looks like I'll have to put it into "kodobaza"))))

 
Vladimir Simakov:
class CTest{
   _CTest test[5];
public:
   int a;
   long b;
public:
   CTest():a(555),b(LONG_MIN)
   {
      for(int i=ArraySize(test)-1; i>=0; i--) 
         {
            test[i].a = i;
            test[i].b = (double)i/10.0;
            test[i].t = (string)(i*2); 
         }
      
   }
   
   string JSon() {return (CJSon()).ARRAY_STRUCT(test).VALUE(a).VALUE(b).Finish();}
};

void OnStart(){
   CTest test;
   Print(test.JSon());
 }

it should be like this (line break is not necessary - I added it by hand):

{"test":[
         {"a":0,"b":0.0,"t":"0"},
         {"a":1,"b":0.1,"t":"2"},
         {"a":2,"b":0.2,"t":"4"},
         {"a":3,"b":0.3,"t":"6"},
         {"a":4,"b":0.4,"t":"8"}],
         "a":555,"b":-9223372036854775808}

now it's like this:

{"test":[
         "{" a":0," b":0.0," t":"0"}",
         "{" a":1," b":0.1," t":"2"}",
         "{" a":2," b":0.2," t":"4"}",
         "{" a":3," b":0.3," t":"6"}",
         "{" a":4," b":0.4," t":"8"}"],
         "a":555,"b":-9223372036854775808}
 
Vladimir Simakov:

PS. Looks like we'll have to put it in the "kodobase"))))

imho, great job, there are questions of course, how quickly you made it.... if here in the course of this one hour.... well that's kind of over the top!

checked the edited version - ok, everything works correctly