json_parser.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * File: json_parser.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 28 octobre 2018
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "json.h"
  10. /* --- Fonctions privées --- */
  11. /**
  12. * Passe un objet json dans une chaine json
  13. * @param char* La chaine json
  14. * @return int Le nombre de caractères à passer
  15. */
  16. int skip_object(char* json) {
  17. int compteur = 1;
  18. json++;
  19. //On compte le nombre caractere à sauter
  20. while (*json && *json != '}') {
  21. //Si on trouve un autre objet dans l'objet
  22. if (*json == '{') {
  23. skip_object(json);
  24. }
  25. compteur++;
  26. json++;
  27. }
  28. return compteur;
  29. }
  30. /**
  31. * Passe un tableau json dans une chaine json
  32. * @param char* La chaine json
  33. * @return int Le nombre de caractères à passer
  34. */
  35. int skip_array(char* json) {
  36. int compteur = 1;
  37. json++;
  38. //On compte le nombre caractere à sauter
  39. while (*json && *json != ']') {
  40. json++;
  41. compteur++;
  42. }
  43. return compteur;
  44. }
  45. /**
  46. * Compte le nombre de clef d'une chaine json
  47. * @param char* La chaine json
  48. * @return int Le nombre de clef
  49. */
  50. int key_counter(char* json){
  51. //Declaration variable
  52. int compteur = 0;
  53. boolean key = true;
  54. //On parcours la chaine une 1er fois pour compter le nombre de clef
  55. while (*json && *json != '}') {
  56. //Si on trouve le debut d'une clef
  57. if (*json == '"' && key) {
  58. compteur++;
  59. key = false;
  60. }
  61. //Si on tombe sur un autre objet json
  62. else if (*json == '{' && !key) {
  63. json += skip_object(json);
  64. }
  65. //Si on tombe sur un tableau
  66. else if (*json == '[' && !key) {
  67. json += skip_array(json);
  68. }
  69. //Si on trouve un separateur de valeur
  70. else if (*json == ',' && !key) {
  71. //Le prochain element est une clefS
  72. key = true;
  73. }
  74. json++;
  75. }
  76. //Retour
  77. return compteur;
  78. }
  79. /**
  80. * Extraction d'une clef
  81. * @param JsonParser* La structure pour sauvegarder l'extraction
  82. * @param int La position dans la structure
  83. * @param char* La chaine json
  84. * @return int JSON_ERROR ou le nombre de caractère de la clef
  85. */
  86. int parse_key(JsonParser* this, int index, char* json) {
  87. //Declaration variable
  88. int compteur = 0;
  89. //On parcours jusqu'a la fin de la clef
  90. json++;
  91. while (*json && *json != '"') {
  92. json++;
  93. compteur++;
  94. }
  95. //Si json mal formé
  96. if (!*json) {
  97. return JSON_ERROR;
  98. }
  99. //Setup les infos
  100. this->key[index] = json - compteur;
  101. this->key_length[index] = compteur;
  102. //Sinon retourne ok
  103. return ++compteur;
  104. }
  105. /**
  106. * Extraction d'une valeur
  107. * @param JsonParser* La structure pour sauvegarder l'extraction
  108. * @param int La position dans la structure
  109. * @param char* La chaine json
  110. * @return int JSON_ERROR ou le nombre de caractère de la valeur
  111. */
  112. int parse_val(JsonParser* this, int index, char* json){
  113. //Declaration variable
  114. int compteur = 0;
  115. //Regarde le type de valeur
  116. switch(*json){
  117. //String
  118. case '"':
  119. //Cherche le guillement de fin
  120. json++;
  121. while (*json && *json != '"') {
  122. json++;
  123. compteur++;
  124. }
  125. //Si json mal formé
  126. if (!*json) {
  127. return JSON_ERROR;
  128. }
  129. //Setup les infos
  130. this->val[index] = json - compteur;
  131. this->val_length[index] = compteur;
  132. this->type[index] = JSON_STRING;
  133. break;
  134. //Boolean
  135. case 't':
  136. compteur = 4;
  137. this->val[index] = json;
  138. this->val_length[index] = compteur;
  139. this->type[index] = JSON_BOOLEAN;
  140. break;
  141. case 'f':
  142. compteur = 5;
  143. this->val[index] = json;
  144. this->val_length[index] = compteur;
  145. this->type[index] = JSON_BOOLEAN;
  146. break;
  147. //Nombre
  148. case '-':
  149. case '0':
  150. case '1':
  151. case '2':
  152. case '3':
  153. case '4':
  154. case '5':
  155. case '6':
  156. case '7':
  157. case '8':
  158. case '9':
  159. //Cherche espace de fin ou fin json ou suite json
  160. while (*json && *json != ' ' && *json != '}' && *json != ',') {
  161. json++;
  162. compteur++;
  163. }
  164. //Si json mal formé
  165. if (!*json) {
  166. return JSON_ERROR;
  167. }
  168. //Setup les infos
  169. this->val[index] = json - compteur;
  170. this->val_length[index] = compteur;
  171. this->type[index] = JSON_NUMBER;
  172. break;
  173. //Tableau
  174. case '[':
  175. compteur = skip_array(json) + 1;
  176. this->val[index] = json;
  177. this->val_length[index] = compteur;
  178. this->type[index] = JSON_ARRAY;
  179. break;
  180. //Objet
  181. case '{':
  182. compteur = skip_object(json) + 1;
  183. this->val[index] = json;
  184. this->val_length[index] = compteur;
  185. this->type[index] = JSON_OBJECT;
  186. break;
  187. //Autre
  188. default:
  189. return JSON_ERROR;
  190. }
  191. //Retour
  192. return ++compteur;
  193. }
  194. /**
  195. * Erreur lors du la procedure de parse
  196. * Supprime la structure et retourne un code d'erreur
  197. * @param JsonParser* structure traitée pendant l'erreur
  198. * @return int JSON_ERROR
  199. */
  200. int parse_error(JsonParser* this){
  201. clean_json_parser(this);
  202. return JSON_ERROR;
  203. }
  204. /**
  205. * Cherche l'index d'une clef
  206. * @param JsonParser* La structure initialisé par json_parse
  207. * @param char* La clef à rechercher
  208. * @return int La position
  209. */
  210. int search_index(JsonParser* this, char* key){
  211. int length = strlen(key);
  212. //Cherche la clef
  213. for(int i = 0; i < this->elt; i++){
  214. if(strncmp(this->key[i], key, length) == 0){
  215. return i;
  216. }
  217. }
  218. //Si rien trouver
  219. return JSON_ERROR;
  220. }
  221. /* --- Fonctions publiques --- */
  222. int json_parse(JsonParser* this, char* json) {
  223. //Declaration variable
  224. char* tmp;
  225. int temp, compteur;
  226. boolean key = true;
  227. //Compte le nombre de clef
  228. compteur = key_counter(json);
  229. //Allocation de la taille des tableaux
  230. this->str = malloc(strlen(json) * sizeof (char));
  231. strcpy(this->str, json);
  232. this->elt = compteur;
  233. this->key = malloc(compteur * sizeof (char*));
  234. this->key_length = malloc(compteur * sizeof (int));
  235. this->val = malloc(compteur * sizeof (char*));
  236. this->val_length = malloc(compteur * sizeof (int));
  237. this->type = malloc(compteur * sizeof (int));
  238. //On reparcours le tableau pour parser
  239. tmp = this->str;
  240. compteur = 0;
  241. while (*tmp && *tmp != '}') {
  242. //Si on trouve une clef
  243. if (*tmp == '"') {
  244. //Lecture clef de la clef
  245. if((temp = parse_key(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
  246. tmp += temp;
  247. key = false;
  248. }
  249. //Si on trouve une valeur
  250. else if(*tmp == ':'){
  251. //Si pas de claf avant
  252. if(key){ return parse_error(this); }
  253. //Saute les espaces
  254. tmp++;
  255. while(*tmp == ' ') { tmp++; }
  256. //Lecture valeur
  257. if((temp = parse_val(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
  258. tmp += temp;
  259. key = true;
  260. compteur++;
  261. }
  262. tmp++;
  263. }
  264. //Si on s'arrete sur une clef
  265. if(!key){
  266. return parse_error(this);
  267. }
  268. //Sinon ok
  269. return JSON_OK;
  270. }
  271. char* key_index(JsonParser* this, int index){
  272. //Index incorrect
  273. if(index < 0 || index >= this->elt){
  274. return NULL;
  275. }
  276. //Creation string d'accueil
  277. char* val;
  278. val = malloc(this->key_length[index] * sizeof(char));
  279. //Copie valeur
  280. strncpy(val, this->key[index], this->key_length[index]);
  281. //Retour
  282. return val;
  283. }
  284. char* get_index(JsonParser* this, int index){
  285. //Index incorrect
  286. if(index < 0 || index >= this->elt){
  287. return NULL;
  288. }
  289. //Creation string d'accueil
  290. char* val;
  291. val = malloc(this->val_length[index] * sizeof(char));
  292. //Copie valeur
  293. strncpy(val, this->val[index], this->val_length[index]);
  294. //Retour
  295. return val;
  296. }
  297. int get_pos(JsonParser* this, char* key){
  298. //Recup index
  299. int index;
  300. if((index = search_index(this, key)) == JSON_ERROR){
  301. return JSON_ERROR;
  302. }
  303. return index;
  304. }
  305. int get_type(JsonParser* this, char* key){
  306. //Recup index
  307. int index;
  308. if((index = search_index(this, key)) == JSON_ERROR){
  309. return JSON_ERROR;
  310. }
  311. return this->type[index];
  312. }
  313. char* get_value(JsonParser* this, char* key){
  314. //Recup index
  315. int index;
  316. if((index = search_index(this, key)) == JSON_ERROR){
  317. return NULL;
  318. }
  319. return get_index(this, index);
  320. }
  321. double get_number(JsonParser* this, char* key){
  322. //Recup index
  323. int index;
  324. if((index = search_index(this, key)) == JSON_ERROR){
  325. return JSON_ERROR;
  326. }
  327. //Verif type
  328. if(this->type[index] != JSON_NUMBER){
  329. return JSON_ERROR;
  330. }
  331. //Recup valeur
  332. char* val;
  333. val = get_index(this, index);
  334. //Cast et retoure
  335. double res;
  336. res = atof(val);
  337. free(val);
  338. return res;
  339. }
  340. int get_integer(JsonParser* this, char* key){
  341. //Recup index
  342. int index;
  343. if((index = search_index(this, key)) == JSON_ERROR){
  344. return JSON_ERROR;
  345. }
  346. //Verif type
  347. if(this->type[index] != JSON_NUMBER){
  348. return JSON_ERROR;
  349. }
  350. //Recup valeur
  351. char* val;
  352. val = get_index(this, index);
  353. //Cast et retoure
  354. int res;
  355. res = atoi(val);
  356. free(val);
  357. return res;
  358. }
  359. boolean get_boolean(JsonParser* this, char* key){
  360. //Recup index
  361. int index;
  362. if((index = search_index(this, key)) == JSON_ERROR){
  363. return false;
  364. }
  365. //Verif type
  366. if(this->type[index] != JSON_BOOLEAN){
  367. return false;
  368. }
  369. //Recup valeur
  370. char* val;
  371. val = get_index(this, index);
  372. //Cast et retoure
  373. if(val[0] == 't'){
  374. return true;
  375. }
  376. return false;
  377. }
  378. char* get_array(JsonParser* this, char* key){
  379. //Recup index
  380. int index;
  381. if((index = search_index(this, key)) == JSON_ERROR){
  382. return NULL;
  383. }
  384. //Verif type
  385. if(this->type[index] != JSON_ARRAY){
  386. return NULL;
  387. }
  388. //Retour pas de gestion de tableau actuellement
  389. return get_index(this, index);
  390. }
  391. JsonParser* get_object(JsonParser* this, char* key){
  392. //Recup index
  393. int index;
  394. if((index = search_index(this, key)) == JSON_ERROR){
  395. return NULL;
  396. }
  397. //Verif type
  398. if(this->type[index] != JSON_OBJECT){
  399. return NULL;
  400. }
  401. //Recup valeur
  402. char* val;
  403. val = get_index(this, index);
  404. //Parse
  405. JsonParser* json;
  406. json = malloc(sizeof(JsonParser));
  407. if(json_parse(json, val) == JSON_ERROR){
  408. return NULL;
  409. }
  410. //Retour JSON utilisable
  411. free(val);
  412. return json;
  413. }
  414. void clean_json_parser(JsonParser* this) {
  415. free(this->key);
  416. free(this->key_length);
  417. free(this->val);
  418. free(this->val_length);
  419. free(this->type);
  420. free(this->str);
  421. }