jsonobject.c 2.8 KB

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