json_encoder.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 privée --- */
  12. /**
  13. * Ajout un noeud au JsonEncoder
  14. * @param JsonEncoder* La structure pour encoder
  15. * @param char* La chaine à mettre dans le noeud
  16. */
  17. void add_node(JsonEncoder* this, char* str){
  18. //Création node
  19. JsonNode* node;
  20. node = malloc(sizeof(JsonNode));
  21. //Allocation node
  22. node->str = malloc(strlen(str) * sizeof(char));
  23. //Initialisation node
  24. strcpy(node->str, str);
  25. //Si 1er node
  26. if(this->head == NULL){
  27. this->head = node;
  28. node->prev = NULL;
  29. } else {
  30. node->prev = this->tail;
  31. node->prev->next = node;
  32. }
  33. this->tail = node;
  34. node->next = NULL;
  35. }
  36. /**
  37. * Supprimme un noeud
  38. * @param JsonNode* Le noeud à supprimer
  39. */
  40. void delete_node(JsonNode* node){
  41. free(node->str);
  42. }
  43. /* --- Fonctions publique --- */
  44. void ini_encoder(JsonEncoder* this){
  45. this->head = NULL;
  46. this->tail = NULL;
  47. this->length = 0;
  48. }
  49. void add_value(JsonEncoder* this, char* str){
  50. //Ajoute la longueur de la chaine au total
  51. this->length += strlen(str) + 2; //Chaine + ", "
  52. //Ajoute le noeud
  53. add_node(this, str);
  54. }
  55. void add_string(JsonEncoder* this, char* key, char* val){
  56. //Creation chaine
  57. char* str;
  58. str = malloc((strlen(key) + strlen(val) + 4 + 2 + 1) * sizeof(char)); //clef + val + 4 guillemet + ": " + \0
  59. sprintf(str, "\"%s\": \"%s\"", key, val);
  60. //Ajout
  61. add_value(this, str);
  62. free(str);
  63. }
  64. void add_number(JsonEncoder* this, char* key, double val, int ndigit){
  65. //Double en string
  66. char nombre[20];
  67. ftoa(val, nombre, ndigit);
  68. //Creation chaine
  69. char* str;
  70. str = malloc((strlen(key) + strlen(nombre) + 2 + 2 + 1) * sizeof(char)); //clef + val + 2 guillemets + ": " + \0
  71. sprintf(str, "\"%s\": %s", key, nombre);
  72. //Ajout
  73. add_value(this, str);
  74. free(str);
  75. }
  76. void add_integer(JsonEncoder* this, char* key, int val){
  77. //Creation chaine
  78. char* str;
  79. str = malloc((strlen(key) + ceil(val/10.0) + 2 + 2 + 1) * sizeof(char)); //clef + val + 2 guillemets + ": " + \0
  80. sprintf(str, "\"%s\": %d", key, val);
  81. //Ajout
  82. add_value(this, str);
  83. free(str);
  84. }
  85. void add_boolean(JsonEncoder* this, char* key, boolean val){
  86. //On determine le boolean
  87. char bool[6];
  88. if(val){
  89. strcpy(bool, "true");
  90. } else {
  91. strcpy(bool, "false");
  92. }
  93. //Creation chaine
  94. char* str;
  95. str = malloc((strlen(key) + strlen(bool) + 2 + 2 + 1) * sizeof(char)); //clef + val + 2 guillemets + ": " + \0
  96. sprintf(str, "\"%s\": %s", key, bool);
  97. //Ajout
  98. add_value(this, str);
  99. free(str);
  100. }
  101. void add_array(JsonEncoder* this, char* key, char* val){
  102. add_string(this, key, val); //Pas de gestion special
  103. }
  104. void add_object(JsonEncoder* this, char* key, JsonEncoder val){
  105. //Recup string du JsonEncoder
  106. char* json;
  107. json = json_encode(&val);
  108. //Creation chaine
  109. char* str;
  110. str = malloc((strlen(key) + strlen(json) + 2 + 2 + 1) * sizeof(char)); //clef + val + 2 guillemets + ": " + \0
  111. sprintf(str, "\"%s\": %s", key, json);
  112. //Ajout
  113. add_value(this, str);
  114. free(str);
  115. free(json);
  116. }
  117. char* json_encode(JsonEncoder* this){
  118. boolean first = true;
  119. //Allocation chaine
  120. char* str;
  121. str = malloc((this->length + 2) * sizeof(char)); // La chaine + {}
  122. //Creation de la chaine
  123. JsonNode* node;
  124. node = this->head;
  125. str[0] = '{';
  126. while(node != NULL){
  127. if(first){
  128. sprintf(str, "%s%s", str, node->str);
  129. first = false;
  130. } else {
  131. sprintf(str, "%s, %s", str, node->str);
  132. }
  133. node = node->next;
  134. }
  135. sprintf(str, "%s}", str);
  136. //Retour
  137. return str;
  138. }
  139. void clean_json_encoder(JsonEncoder* this){
  140. //Parcours les noeuds et les supprimes
  141. JsonNode* node, * tmp;
  142. node = this->head;
  143. while(node != NULL){
  144. tmp = node->next;
  145. delete_node(node);
  146. free(node);
  147. node = tmp;
  148. }
  149. //Reset la structure
  150. this->head = NULL;
  151. this->tail = NULL;
  152. this->length = 0;
  153. }