json_parser.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "json.h"
  4. /* --- Fonctions privées --- */
  5. int skip_object(char* json) {
  6. int compteur = 1;
  7. json++;
  8. //On compte le nombre caractere à sauter
  9. while (*json && *json != '}') {
  10. //Si on trouve un autre objet dans l'objet
  11. if (*json == '{') {
  12. skip_object(json);
  13. }
  14. compteur++;
  15. json++;
  16. }
  17. return compteur;
  18. }
  19. int skip_array(char* json) {
  20. int compteur = 1;
  21. json++;
  22. //On compte le nombre caractere à sauter
  23. while (*json && *json != ']') {
  24. json++;
  25. compteur++;
  26. }
  27. return compteur;
  28. }
  29. int key_counter(char* json){
  30. //Declaration variable
  31. int compteur = 0;
  32. boolean key = true;
  33. //On parcours la chaine une 1er fois pour compter le nombre de clef
  34. while (*json && *json != '}') {
  35. //Si on trouve le debut d'une clef
  36. if (*json == '"' && key) {
  37. compteur++;
  38. key = false;
  39. }
  40. //Si on tombe sur un autre objet json
  41. else if (*json == '{' && !key) {
  42. json += skip_object(json);
  43. }
  44. //Si on tombe sur un tableau
  45. else if (*json == '[' && !key) {
  46. json += skip_array(json);
  47. }
  48. //Si on trouve un separateur de valeur
  49. else if (*json == ',' && !key) {
  50. //Le prochain element est une clefS
  51. key = true;
  52. }
  53. json++;
  54. }
  55. //Retour
  56. return compteur;
  57. }
  58. int parse_key(JsonParser* this, int index, char* json) {
  59. //Declaration variable
  60. int compteur = 0;
  61. //On parcours jusqu'a la fin de la clef
  62. json++;
  63. while (*json && *json != '"') {
  64. json++;
  65. compteur++;
  66. }
  67. //Si json mal formé
  68. if (!*json) {
  69. return JSON_ERROR;
  70. }
  71. //Setup les infos
  72. printf("Key[%d] : %d\n", index, compteur);
  73. this->key[index] = json - compteur;
  74. this->key_length[index] = compteur;
  75. //Sinon retourne ok
  76. return ++compteur;
  77. }
  78. int parse_val(JsonParser* this, int index, char* json){
  79. //Declaration variable
  80. int compteur = 0;
  81. //Regarde le type de valeur
  82. switch(*json){
  83. //String
  84. case '"':
  85. //Cherche le guillement de fin
  86. json++;
  87. while (*json && *json != '"') {
  88. json++;
  89. compteur++;
  90. }
  91. //Si json mal formé
  92. if (!*json) {
  93. return JSON_ERROR;
  94. }
  95. //Setup les infos
  96. this->val[index] = json - compteur;
  97. this->val_length[index] = compteur;
  98. this->type[index] = JSON_STRING;
  99. break;
  100. //Boolean
  101. case 't':
  102. compteur = 4;
  103. this->val[index] = json;
  104. this->val_length[index] = compteur;
  105. this->type[index] = JSON_BOOLEAN;
  106. break;
  107. case 'f':
  108. compteur = 5;
  109. this->val[index] = json;
  110. this->val_length[index] = compteur;
  111. this->type[index] = JSON_BOOLEAN;
  112. break;
  113. //Nombre
  114. case '-':
  115. case '0':
  116. case '1':
  117. case '2':
  118. case '3':
  119. case '4':
  120. case '5':
  121. case '6':
  122. case '7':
  123. case '8':
  124. case '9':
  125. //Cherche espace de fin ou fin json ou suite json
  126. while (*json && *json != ' ' && *json != '}' && *json != ',') {
  127. json++;
  128. compteur++;
  129. }
  130. //Si json mal formé
  131. if (!*json) {
  132. return JSON_ERROR;
  133. }
  134. //Setup les infos
  135. printf("cpt : %d\n", compteur);
  136. this->val[index] = json - compteur;
  137. this->val_length[index] = compteur;
  138. this->type[index] = JSON_NUMBER;
  139. break;
  140. //Tableau
  141. case '[':
  142. compteur = skip_array(json) + 1;
  143. this->val[index] = json;
  144. this->val_length[index] = compteur;
  145. this->type[index] = JSON_ARRAY;
  146. break;
  147. //Objet
  148. case '{':
  149. compteur = skip_object(json) + 1;
  150. this->val[index] = json;
  151. this->val_length[index] = compteur;
  152. this->type[index] = JSON_OBJECT;
  153. break;
  154. //Autre
  155. default:
  156. return JSON_ERROR;
  157. }
  158. //Retour
  159. return ++compteur;
  160. }
  161. int parse_error(JsonParser* this){
  162. clean_json_parser(this);
  163. return JSON_ERROR;
  164. }
  165. /* --- Fonctions publiques --- */
  166. int json_parse(JsonParser* this, char* json) {
  167. //Declaration variable
  168. char* tmp;
  169. int temp, compteur;
  170. boolean key = true;
  171. //Compte le nombre de clef
  172. compteur = key_counter(json);
  173. //Allocation de la taille des tableaux
  174. this->str = malloc(strlen(json) * sizeof (char));
  175. strcpy(this->str, json);
  176. this->key = malloc(compteur * sizeof (char*));
  177. this->key_length = malloc(compteur * sizeof (int));
  178. this->val = malloc(compteur * sizeof (char*));
  179. this->val_length = malloc(compteur * sizeof (int));
  180. this->type = malloc(compteur * sizeof (int));
  181. //On reparcours le tableau pour parser
  182. tmp = this->str;
  183. compteur = 0;
  184. while (*tmp && *tmp != '}') {
  185. //Si on trouve une clef
  186. if (*tmp == '"') {
  187. //Lecture clef de la clef
  188. if((temp = parse_key(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
  189. tmp += temp;
  190. key = false;
  191. }
  192. //Si on trouve une valeur
  193. else if(*tmp == ':'){
  194. //Si pas de claf avant
  195. if(key){ return parse_error(this); }
  196. //Saute les espaces
  197. tmp++;
  198. while(*tmp == ' ') { tmp++; }
  199. //Lecture valeur
  200. printf("%s\n", tmp);
  201. if((temp = parse_val(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
  202. tmp += temp;
  203. key = true;
  204. compteur++;
  205. }
  206. tmp++;
  207. }
  208. //Si on s'arrete sur une clef
  209. if(!key){
  210. return parse_error(this);
  211. }
  212. //Sinon ok
  213. return JSON_OK;
  214. }
  215. void clean_json_parser(JsonParser* this) {
  216. free(this->key);
  217. free(this->key_length);
  218. free(this->val);
  219. free(this->val_length);
  220. free(this->type);
  221. free(this->str);
  222. }