//+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum ENUM_TYPE_DATA_JSON { TYPE_DATA_JSON_UNDEFINED = 0, TYPE_DATA_JSON_OBJECT, TYPE_DATA_JSON_ARRAY, TYPE_DATA_JSON_NULL, TYPE_DATA_JSON_BOOL, TYPE_DATA_JSON_COLOR, TYPE_DATA_JSON_LONG, TYPE_DATA_JSON_ULONG, TYPE_DATA_JSON_STRING, TYPE_DATA_JSON_DOUBLE, }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ struct MqlDataType { ENUM_TYPE_DATA_JSON type_data; bool bool_value; color color_value; long long_value; ulong ulong_value; string string_value; double double_value; MqlDataType::MqlDataType(void) { type_data = TYPE_DATA_JSON_UNDEFINED; bool_value = false; color_value = clrNONE; long_value = 0; ulong_value = 0; string_value = ""; double_value = 0.0; } }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CJson { public: CJson m_json[]; string m_key; MqlDataType m_value; public: CJson(const CJson &json); CJson(void); ~CJson(void); //--- Assigns key and returns object CJson *operator[](const string key); CJson *operator[](const int index); //--- Add template CJson *Add(const T value); CJson *Add(CJson &value); void Set(CJson &value); void Set(CJson &value[]); //--- Serialize/Deserialize string Serialize(bool skey = false, bool comma = false); bool Deserialize(uchar &json[], int &index, int len); bool Deserialize(string json, uint codepage=0); //--- Size/Clear int Size(void); void Clear(void); CJson *FindKey(string key); void Remove(string key); //--- Operators void operator=(CJson &value); void operator=(const char value); void operator=(const uchar value); void operator=(const bool value); void operator=(const short value); void operator=(const ushort value); void operator=(const int value); void operator=(const uint value); void operator=(const color value); void operator=(const long value); void operator=(const ulong value); void operator=(const datetime value); void operator=(const string value); void operator=(const double value); void operator=(const float value); //--- bool operator==(const char value) { return(m_value.long_value == value); }; bool operator==(const uchar value) { return(m_value.ulong_value == value); }; bool operator==(const bool value) { return(m_value.bool_value == value); }; bool operator==(const short value) { return(m_value.long_value == value); }; bool operator==(const ushort value) { return(m_value.ulong_value == value); }; bool operator==(const int value) { return(m_value.long_value == value); }; bool operator==(const uint value) { return(m_value.ulong_value == value); }; bool operator==(const color value) { return(m_value.color_value == value); }; bool operator==(const long value) { return(m_value.long_value == value); }; bool operator==(const ulong value) { return(m_value.ulong_value == value); }; bool operator==(const datetime value) { return(StringToTime(m_value.string_value) == value); }; bool operator==(const string value) { return(m_value.string_value == value); }; bool operator==(const double value) { return(m_value.double_value == value); }; bool operator==(const float value) { return(m_value.double_value == value); }; //--- bool operator!=(const char value) { return(m_value.long_value != value); }; bool operator!=(const uchar value) { return(m_value.ulong_value != value); }; bool operator!=(const bool value) { return(m_value.bool_value != value); }; bool operator!=(const short value) { return(m_value.long_value != value); }; bool operator!=(const ushort value) { return(m_value.ulong_value != value); }; bool operator!=(const int value) { return(m_value.long_value != value); }; bool operator!=(const uint value) { return(m_value.ulong_value != value); }; bool operator!=(const color value) { return(m_value.color_value != value); }; bool operator!=(const long value) { return(m_value.long_value != value); }; bool operator!=(const ulong value) { return(m_value.ulong_value != value); }; bool operator!=(const datetime value) { return(StringToTime(m_value.string_value) != value); }; bool operator!=(const string value) { return(m_value.string_value != value); }; bool operator!=(const double value) { return(m_value.double_value != value); }; bool operator!=(const float value) { return(m_value.double_value != value); }; //--- bool operator>(const char value) { return(m_value.long_value > value); }; bool operator>(const uchar value) { return(m_value.ulong_value > value); }; bool operator>(const bool value) { return(m_value.bool_value > value); }; bool operator>(const short value) { return(m_value.long_value > value); }; bool operator>(const ushort value) { return(m_value.ulong_value > value); }; bool operator>(const int value) { return(m_value.long_value > value); }; bool operator>(const uint value) { return(m_value.ulong_value > value); }; bool operator>(const color value) { return(m_value.color_value > value); }; bool operator>(const long value) { return(m_value.long_value > value); }; bool operator>(const ulong value) { return(m_value.ulong_value > value); }; bool operator>(const datetime value) { return(StringToTime(m_value.string_value) > value); }; bool operator>(const string value) { return(m_value.string_value > value); }; bool operator>(const double value) { return(m_value.double_value > value); }; bool operator>(const float value) { return(m_value.double_value > value); }; //--- bool operator>=(const char value) { return(m_value.long_value >= value); }; bool operator>=(const uchar value) { return(m_value.ulong_value >= value); }; bool operator>=(const bool value) { return(m_value.bool_value >= value); }; bool operator>=(const short value) { return(m_value.long_value >= value); }; bool operator>=(const ushort value) { return(m_value.ulong_value >= value); }; bool operator>=(const int value) { return(m_value.long_value >= value); }; bool operator>=(const uint value) { return(m_value.ulong_value >= value); }; bool operator>=(const color value) { return(m_value.color_value >= value); }; bool operator>=(const long value) { return(m_value.long_value >= value); }; bool operator>=(const ulong value) { return(m_value.ulong_value >= value); }; bool operator>=(const datetime value) { return(StringToTime(m_value.string_value) >= value); }; bool operator>=(const string value) { return(m_value.string_value >= value); }; bool operator>=(const double value) { return(m_value.double_value >= value); }; bool operator>=(const float value) { return(m_value.double_value >= value); }; //--- bool operator<(const char value) { return(m_value.long_value < value); }; bool operator<(const uchar value) { return(m_value.ulong_value < value); }; bool operator<(const bool value) { return(m_value.bool_value < value); }; bool operator<(const short value) { return(m_value.long_value < value); }; bool operator<(const ushort value) { return(m_value.ulong_value < value); }; bool operator<(const int value) { return(m_value.long_value < value); }; bool operator<(const uint value) { return(m_value.ulong_value < value); }; bool operator<(const color value) { return(m_value.color_value < value); }; bool operator<(const long value) { return(m_value.long_value < value); }; bool operator<(const ulong value) { return(m_value.ulong_value < value); }; bool operator<(const datetime value) { return(StringToTime(m_value.string_value) < value); }; bool operator<(const string value) { return(m_value.string_value < value); }; bool operator<(const double value) { return(m_value.double_value < value); }; bool operator<(const float value) { return(m_value.double_value < value); }; //--- bool operator<=(const char value) { return(m_value.long_value <= value); }; bool operator<=(const uchar value) { return(m_value.ulong_value <= value); }; bool operator<=(const bool value) { return(m_value.bool_value <= value); }; bool operator<=(const short value) { return(m_value.long_value <= value); }; bool operator<=(const ushort value) { return(m_value.ulong_value <= value); }; bool operator<=(const int value) { return(m_value.long_value <= value); }; bool operator<=(const uint value) { return(m_value.ulong_value <= value); }; bool operator<=(const color value) { return(m_value.color_value <= value); }; bool operator<=(const long value) { return(m_value.long_value <= value); }; bool operator<=(const ulong value) { return(m_value.ulong_value <= value); }; bool operator<=(const datetime value) { return(StringToTime(m_value.string_value) <= value); }; bool operator<=(const string value) { return(m_value.string_value <= value); }; bool operator<=(const double value) { return(m_value.double_value <= value); }; bool operator<=(const float value) { return(m_value.double_value <= value); }; //--- Get char ToChar(void); uchar ToUchar(void); bool ToBool(void); short ToShort(void); ushort ToUshort(void); int ToInt(void); uint ToUint(void); color ToColor(void); long ToLong(void); ulong ToUlong(void); datetime ToDatetime(void); string ToString(void); double ToDouble(void); float ToFloat(void); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CJson::CJson(const CJson &json) { this.Clear(); m_value = json.m_value; m_key = json.m_key; int size = ArraySize(json.m_json); ArrayFree(m_json); ArrayResize(m_json,size); for(int i=0;i= Size()) { CJson son; if(CheckPointer(Add(son))==POINTER_INVALID) { return(NULL); } } return GetPointer(m_json[index]); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CJson::Set(CJson &value) { if(m_value.type_data == TYPE_DATA_JSON_UNDEFINED) { m_value.type_data = TYPE_DATA_JSON_OBJECT; } int size = ArraySize(value.m_json); ArrayFree(m_json); ArrayResize(m_json,size); for(int i=0;i CJson *CJson::Add(const T value) { CJson json; json = value; return(this.Add(json)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CJson::operator=(CJson &value) { m_key = value.m_key; m_value = value.m_value; int size = ArraySize(value.m_json); ArrayFree(m_json); ArrayResize(m_json,size); for(int i=0;i0); } content += "]"; break; } case(TYPE_DATA_JSON_OBJECT): { content += "{"; int size_obj = ArraySize(m_json); for(int i=0;i0); } content += "}"; break; } case(TYPE_DATA_JSON_STRING): { content += "\"" + m_value.string_value + "\""; break; } case(TYPE_DATA_JSON_COLOR): { content += "\"" + m_value.string_value + "\""; break; } default: { content += m_value.string_value; break; } } return(content); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool CJson::Deserialize(string json, uint codepage=0) { this.Clear(); uchar uchar_json[]; int len = StringToCharArray(json,uchar_json,0,WHOLE_ARRAY,codepage), i = 0; return(this.Deserialize(uchar_json,i,len)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool CJson::Deserialize(uchar &json[], int &index, int len) { string key = ""; while(index=len) { return false; } } return json[index]==']' || json[index]==0; } case('"'): { if(m_value.type_data == TYPE_DATA_JSON_OBJECT) { index++; key = ""; for(int j=index;j= len || json[index] != 'e') { return(false); } return(true); } else if(json[index+1] == 'a') { this.operator=(false); index+=4; if(index >= len || json[index] != 'e') { return(false); } return(true); } else { if(json[index] == 'T' || json[index] == 't') { this.operator=(true); return(true); } else if(json[index] == 'F' || json[index] == 'f') { this.operator=(false); return(true); } } } else { return(false); } break; } case 'n': case 'N': { if(index+1 < len) { if(json[index+1] == 'u') { m_value.type_data = TYPE_DATA_JSON_NULL; index+=3; if(index >= len || (json[index] != 'l' && json[index] != 'L')) { return(false); } return(true); } else if(json[index] == 'N' || json[index] == 'n') { m_value.type_data = TYPE_DATA_JSON_NULL; return(true); } } else { return(false); } break; } case(':'): { if(key == "") { return(false); } else { index++; CJson new_json; CJson *child_json = Add(new_json); child_json.m_key = key; child_json.m_value.type_data = TYPE_DATA_JSON_NULL; if(!child_json.Deserialize(json,index,len)) { return(false); } } break; } case('}'): { return(m_value.type_data == TYPE_DATA_JSON_OBJECT); } case(']'): { break; } } index++; } return(true); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CJson::Size(void) { return(ArraySize(m_json)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CJson::Clear(void) { ArrayFree(m_json); m_key = ""; m_value.type_data = TYPE_DATA_JSON_NULL; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CJson *CJson::FindKey(string key) { for(int i=this.Size()-1;i>=0;i--) { if(m_json[i].m_key == key) { return(GetPointer(m_json[i])); } } return(NULL); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CJson::Remove(string key) { CJson *json = this.FindKey(key); if(json != NULL) { int size = ArraySize(m_json); for(int i=0;i