json_encoder.c 3.9 KB

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