json_encoder.c 4.4 KB

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