File size: 2,901 Bytes
24b81cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
class JsonObject
{
ref map<string, string> m_Strings;
ref map<string, int> m_Ints;
ref map<string, float> m_Floats;
ref map<string, bool> m_Bools;
ref map<string, ref Vector2> m_Vectors2;
void JsonObject()
{
m_Strings = new map<string, string>;
m_Ints = new map<string, int>;
m_Floats = new map<string, float>;
m_Bools = new map<string, bool>;
m_Vectors2 = new map<string, ref Vector2>;
}
void ~JsonObject()
{
Clear();
}
void Clear()
{
m_Strings.Clear();
m_Ints.Clear();
m_Floats.Clear();
m_Bools.Clear();
m_Vectors2.Clear();
}
void AddString(string name, string value)
{
array<string> strgs = new array<string>;
value.Split("\"", strgs);
if ( strgs.Count() > 1 )
{
value = "";
int str_count = strgs.Count();
for ( int i = 0; i < str_count; ++i )
{
string s = strgs[i];
int length = s.Length();
if ( s[length - 1] != "\\" )
{
value += s;
if (i < str_count - 1 )
{
value += "\\";
value += "\"";
}
}
}
}
m_Strings.Insert(name, value);
}
void AddInt(string name, int value)
{
m_Ints.Insert(name, value);
}
void AddFloat(string name, float value)
{
m_Floats.Insert(name, value);
}
void AddBool(string name, bool value)
{
m_Bools.Insert(name, value);
}
void AddVector2(string name, float x, float y)
{
Vector2 v = new Vector2(x, y);
m_Vectors2.Insert(name, v);
}
string GetJson()
{
string name;
int i;
string jsn = "";
jsn += "{";
// Parse Strings
for ( i = 0; i < m_Strings.Count(); ++i )
{
if ( jsn.Length() > 1 )
{
jsn += ",";
}
name = m_Strings.GetKey(i);
string value_str = m_Strings.GetElement(i);
jsn += "\""+name+"\":\""+value_str+"\"";
}
// Parse Ints
for ( i = 0; i < m_Ints.Count(); ++i )
{
if ( jsn.Length() > 1 )
{
jsn += ",";
}
name = m_Ints.GetKey(i);
int value_int = m_Ints.GetElement(i);
jsn += "\""+name+"\":"+value_int;
}
// Parse Floats
for ( i = 0; i < m_Floats.Count(); ++i )
{
if ( jsn.Length() > 1 )
{
jsn += ",";
}
name = m_Floats.GetKey(i);
float value_flt = m_Floats.GetElement(i);
jsn += "\""+name+"\":"+value_flt;
}
// Parse Bools
for ( i = 0; i < m_Bools.Count(); ++i )
{
if ( jsn.Length() > 1 )
{
jsn += ",";
}
name = m_Bools.GetKey(i);
if ( m_Bools.GetElement(i) )
{
jsn += "\""+name+"\":true";
}
else
{
jsn += "\""+name+"\":false";
}
}
// Parse Vectors2
for ( i = 0; i < m_Vectors2.Count(); ++i )
{
if ( jsn.Length() > 1 )
{
jsn += ",";
}
name = m_Vectors2.GetKey(i);
Vector2 value_vct = m_Vectors2.GetElement(i);
jsn += "\""+name+"\":{\"x\":"+value_vct.x+",\"y\":"+value_vct.y+"}";
}
jsn += "}";
return jsn;
}
}; |