json_encoder.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * File: json_parser.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 29 octobre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include "json.h"
  11. /* --- Fonctions publique --- */
  12. void ini_encoder(JsonEncoder* this){
  13. this->max = JSON_MAX_SIZE;
  14. this->pos = 1;
  15. memset(this->json, 0, this->max);
  16. this->json[0] = '{';
  17. }
  18. void add_value(JsonEncoder* this, char* str){
  19. //Verif que l'on ne depasse pas (on retire 1 pour garder un \0 et on retire 1 autre pour pouvoir mettre })
  20. if(this->pos + strlen(str) >= this->max - 2){
  21. return;
  22. }
  23. //Ajoute
  24. for(int i = 0; i < strlen(str); i++){
  25. this->json[this->pos++] = str[i];
  26. }
  27. }
  28. void add_string(JsonEncoder* this, char* key, char* val){
  29. int length = strlen(key) + strlen(val) + 4 + 2 + 2; //clef + val + 4 guillemet + ": " + ", "
  30. //Verif que l'on ne depasse pas
  31. if(this->pos + length >= this->max - 2){
  32. return;
  33. }
  34. //Ajoute
  35. if(this->pos != 1){
  36. //Si pas 1er valeur on ajoute un separateur
  37. this->json[this->pos++] = ',';
  38. this->json[this->pos++] = ' ';
  39. }
  40. this->json[this->pos++] = '"';
  41. for(int i = 0; i < strlen(key); i++){
  42. this->json[this->pos++] = key[i];
  43. }
  44. this->json[this->pos++] = '"';
  45. this->json[this->pos++] = ':';
  46. this->json[this->pos++] = ' ';
  47. this->json[this->pos++] = '"';
  48. for(int i = 0; i < strlen(val); i++){
  49. this->json[this->pos++] = val[i];
  50. }
  51. this->json[this->pos++] = '"';
  52. }
  53. void add_number(JsonEncoder* this, char* key, double val, int ndigit){
  54. //Double en string
  55. char nombre[20];
  56. ftoa(val, nombre, ndigit);
  57. //Creation chaine
  58. int length = strlen(key) + strlen(nombre) + 2 + 2 + 2; //clef + val + 2 guillemet + ": " + ", "
  59. //Verif que l'on ne depasse pas
  60. if(this->pos + length >= this->max - 2){
  61. return;
  62. }
  63. //Ajoute
  64. if(this->pos != 1){
  65. //Si pas 1er valeur on ajoute un separateur
  66. this->json[this->pos++] = ',';
  67. this->json[this->pos++] = ' ';
  68. }
  69. this->json[this->pos++] = '"';
  70. for(int i = 0; i < strlen(key); i++){
  71. this->json[this->pos++] = key[i];
  72. }
  73. this->json[this->pos++] = '"';
  74. this->json[this->pos++] = ':';
  75. this->json[this->pos++] = ' ';
  76. for(int i = 0; i < strlen(nombre); i++){
  77. this->json[this->pos++] = nombre[i];
  78. }
  79. }
  80. void add_integer(JsonEncoder* this, char* key, int val){
  81. //Double en string
  82. char nombre[20];
  83. snprintf(nombre, 20, "%d", val);
  84. //Creation chaine
  85. int length = strlen(key) + strlen(nombre) + 2 + 2 + 2; //clef + val + 2 guillemet + ": " + ", "
  86. //Verif que l'on ne depasse pas
  87. if(this->pos + length >= this->max - 2){
  88. return;
  89. }
  90. //Ajoute
  91. if(this->pos != 1){
  92. //Si pas 1er valeur on ajoute un separateur
  93. this->json[this->pos++] = ',';
  94. this->json[this->pos++] = ' ';
  95. }
  96. this->json[this->pos++] = '"';
  97. for(int i = 0; i < strlen(key); i++){
  98. this->json[this->pos++] = key[i];
  99. }
  100. this->json[this->pos++] = '"';
  101. this->json[this->pos++] = ':';
  102. this->json[this->pos++] = ' ';
  103. for(int i = 0; i < strlen(nombre); i++){
  104. this->json[this->pos++] = nombre[i];
  105. }
  106. }
  107. void add_boolean(JsonEncoder* this, char* key, boolean val){
  108. //On determine le boolean
  109. char bool[6];
  110. if(val){
  111. strcpy(bool, "true");
  112. } else {
  113. strcpy(bool, "false");
  114. }
  115. //Creation chaine
  116. int length = strlen(key) + strlen(bool) + 2 + 2 + 2; //clef + val + 2 guillemet + ": " + ", "
  117. //Verif que l'on ne depasse pas
  118. if(this->pos + length >= this->max - 2){
  119. return;
  120. }
  121. //Ajoute
  122. if(this->pos != 1){
  123. //Si pas 1er valeur on ajoute un separateur
  124. this->json[this->pos++] = ',';
  125. this->json[this->pos++] = ' ';
  126. }
  127. this->json[this->pos++] = '"';
  128. for(int i = 0; i < strlen(key); i++){
  129. this->json[this->pos++] = key[i];
  130. }
  131. this->json[this->pos++] = '"';
  132. this->json[this->pos++] = ':';
  133. this->json[this->pos++] = ' ';
  134. for(int i = 0; i < strlen(bool); i++){
  135. this->json[this->pos++] = bool[i];
  136. }
  137. }
  138. void add_array(JsonEncoder* this, char* key, JsonArray* val){
  139. //Verification
  140. if(val->mode != JSON_ARRAY_ENCODER){
  141. return;
  142. }
  143. //Recup string du JsonEncoder
  144. char* json;
  145. json = json_encode_array(val);
  146. //Creation chaine
  147. int length = strlen(key) + strlen(json) + 2 + 2 + 2; //clef + val + 2 guillemet + ": " + ", "
  148. //Verif que l'on ne depasse pas
  149. if(this->pos + length >= this->max - 2){
  150. free(json);
  151. return;
  152. }
  153. //Ajoute
  154. if(this->pos != 1){
  155. //Si pas 1er valeur on ajoute un separateur
  156. this->json[this->pos++] = ',';
  157. this->json[this->pos++] = ' ';
  158. }
  159. this->json[this->pos++] = '"';
  160. for(int i = 0; i < strlen(key); i++){
  161. this->json[this->pos++] = key[i];
  162. }
  163. this->json[this->pos++] = '"';
  164. this->json[this->pos++] = ':';
  165. this->json[this->pos++] = ' ';
  166. for(int i = 0; i < strlen(json); i++){
  167. this->json[this->pos++] = json[i];
  168. }
  169. //Nettoyage
  170. free(json);
  171. }
  172. void add_object(JsonEncoder* this, char* key, JsonEncoder* val){
  173. //Recup string du JsonEncoder
  174. char* json;
  175. json = json_encode(val);
  176. //Creation chaine
  177. int length = strlen(key) + strlen(json) + 2 + 2 + 2; //clef + val + 2 guillemet + ": " + ", "
  178. //Verif que l'on ne depasse pas
  179. if(this->pos + length >= this->max - 2){
  180. free(json);
  181. return;
  182. }
  183. //Ajoute
  184. if(this->pos != 1){
  185. //Si pas 1er valeur on ajoute un separateur
  186. this->json[this->pos++] = ',';
  187. this->json[this->pos++] = ' ';
  188. }
  189. this->json[this->pos++] = '"';
  190. for(int i = 0; i < strlen(key); i++){
  191. this->json[this->pos++] = key[i];
  192. }
  193. this->json[this->pos++] = '"';
  194. this->json[this->pos++] = ':';
  195. this->json[this->pos++] = ' ';
  196. for(int i = 0; i < strlen(json); i++){
  197. this->json[this->pos++] = json[i];
  198. }
  199. //Nettoyage
  200. free(json);
  201. }
  202. char* json_encode(JsonEncoder* this){
  203. char* json;
  204. //Ajoute } fin
  205. this->json[this->pos] = '}';
  206. //Creation chaine
  207. json = malloc(sizeof(char) * (this->pos + 2));
  208. memset(json, 0, this->pos + 2);
  209. strncpy(json, this->json, this->pos + 1);
  210. //Retourne
  211. return json;
  212. }
  213. void clean_json_encoder(JsonEncoder* this){
  214. ini_encoder(this);
  215. }