Browse Source

:tada: Ajout encoder tableau JSON

Arthur Brandao 6 years ago
parent
commit
64e6e37634
6 changed files with 234 additions and 33 deletions
  1. 33 26
      Serveur/json.h
  2. 181 0
      Serveur/json_array.c
  3. 2 2
      Serveur/json_encoder.c
  4. 1 1
      Serveur/json_parser.c
  5. 2 2
      Serveur/makefile
  6. 15 2
      Serveur/test.c

+ 33 - 26
Serveur/json.h

@@ -45,16 +45,17 @@ typedef struct{
     char* str; //La chaine
     char** val; //Pointeur vers les valeurs du tableau
     int* val_length; //Taille des valeurs
+    int length; //Taille du tableau
 }JsonArrayParser;
 
 typedef struct{
     JsonNode* tab; //Liste des elements du tableau
-    int length; //Taille du tableau
+    JsonNode* last; //Dernier noeud ajouté
+    int length; //Taille de la chaine
 }JsonArrayEncoder;
 
 typedef struct{
     int mode; //Le mode (Parse ou Encode)
-    int type; //Le type de données du tableau
     JsonArrayParser* parser;
     JsonArrayEncoder* encoder;
 }JsonArray;
@@ -234,7 +235,7 @@ void add_array(JsonEncoder*, char*, char*);
  * @param char* La clef pour acceder à la valeur
  * @param JsonEncoder La valeur
  */
-void add_object(JsonEncoder*, char*, JsonEncoder);
+void add_object(JsonEncoder*, char*, JsonEncoder*);
 
 /**
  * Transforma en chaine de caractère au format JSON
@@ -254,63 +255,69 @@ void clean_json_encoder(JsonEncoder*);
 int json_parse_array(JsonArray*, char*);
 
 //JsonArrayEncoder
-boolean ini_array_encoder(JsonArray*, int);
+
+/**
+ * Initialise le JsonArray en mode encoder
+ * @param JsonArray* La structure à initialiser
+ */
+void ini_array_encoder(JsonArray*);
+
 /**
  * Ajoute une valeur au JSON
- * @param JsonEncoder* La structure pour encoder
- * @param char* La chaine à ajouter (sous la forme "key": val)
+ * @param JsonArray* La structure pour encoder
+ * @param char* La chaine à ajouter
  */
-void add_array_value(JsonArray*, char*);
+boolean add_array_value(JsonArray*, char*);
 
 /**
  * Ajoute un string au JSON
- * @param JsonEncoder* La structure pour encoder
- * @param char* La clef pour acceder à la valeur
+ * @param JsonArray* La structure pour encoder
  * @param char* La valeur
  */
-void add_array_string(JsonArray*, char*, char*);
+boolean add_array_string(JsonArray*, char*);
 
 /**
  * Ajoute un nombre au JSON
- * @param JsonEncoder* La structure pour encoder
+ * @param JsonArray* La structure pour encoder
  * @param char* La clef pour acceder à la valeur
  * @param double La valeur
  * @param int Le nombre de chiffre après la virgule
  */
-void add_array_number(JsonArray*, char*, double, int);
+boolean add_array_number(JsonArray*, double, int);
 
 /**
  * Ajoute un entier au JSON
- * @param JsonEncoder* La structure pour encoder
- * @param char* La clef pour acceder à la valeur
+ * @param JsonArray* La structure pour encoder
  * @param int La valeur
  */
-void add_iarray_nteger(JsonArray*, char*, int);
+boolean add_array_integer(JsonArray*, int);
 
 /**
  * Ajoute un boolean au JSON
- * @param JsonEncoder* La structure pour encoder
- * @param char* La clef pour acceder à la valeur
+ * @param JsonArray* La structure pour encoder
  * @param boolean La valeur
  */
-void add_array_boolean(JsonArray*, char*, boolean);
+boolean add_array_boolean(JsonArray*, boolean);
 
 /**
  * Ajoute un tableau au JSON
- * @param JsonEncoder* La structure pour encoder
- * @param char* La clef pour acceder à la valeur
- * @param char* La valeur
+ * @param JsonArray* La structure pour encoder
+ * @param JsonArray* La valeur
  */
-void add_array_array(JsonArray*, char*, JsonArray*);
+boolean add_array_array(JsonArray*, JsonArray*);
 
 /**
  * Ajoute un objet JSON au JSON
- * @param JsonEncoder* La structure pour encoder
- * @param char* La clef pour acceder à la valeur
- * @param JsonEncoder La valeur
+ * @param JsonArray* La structure pour encoder
+ * @param JsonEncoder* La valeur
  */
-void add_array_object(JsonArray*, char*, JsonEncoder);
+boolean add_array_object(JsonArray*, JsonEncoder*);
+
 char* json_encode_array(JsonArray*);
 
+//JsonArray
+
+void clean_json_array(JsonArray*);
+
 #endif /* JSON_H */
 

+ 181 - 0
Serveur/json_array.c

@@ -0,0 +1,181 @@
+/* 
+ * File:   json_array.c
+ * Author: Arthur Brandao
+ *
+ * Created on 24 novembre 2018
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include "json.h"
+
+/* --- Fonctions privées parser --- */
+
+/* --- Fonctions privées encoder --- */
+
+/**
+ * Ajout un noeud au JsonArrayEncoder
+ * @param JsonArrayEncoder* La structure pour encoder 
+ * @param char* La chaine à mettre dans le noeud
+ */
+void add_json_array_node(JsonArray* this, char* str){
+    //Création node
+    JsonNode* node;
+    node = malloc(sizeof(JsonNode));
+    //Allocation node
+    int length = strlen(str) + 1;
+    node->str = malloc(length * sizeof(char));
+    memset(node->str, 0, length);
+    strncpy(node->str, str, length - 1);
+    //Si 1er node
+    if(this->encoder->tab == NULL){
+        this->encoder->tab = node;
+        node->prev = NULL;
+    } else {
+        node->prev = this->encoder->last;
+        node->prev->next = node;
+    }
+    this->encoder->last = node;
+    node->next = NULL;
+}
+
+/**
+ * Supprimme un noeud
+ * @param JsonNode* Le noeud à supprimer
+ */
+void delete_json_array_node(JsonNode* node){
+    free(node->str);
+}
+
+/* --- Fonctions publiques parser --- */
+
+/* --- Fonctions publiques encoder --- */
+
+void ini_array_encoder(JsonArray* this){
+    //Initialisation en mode encoder
+    this->mode = JSON_ARRAY_ENCODER;
+    this->encoder = malloc(sizeof(JsonArrayEncoder));
+}
+
+boolean add_array_value(JsonArray* this, char* str){
+    //Verification
+    if(!this->mode){
+        return false;
+    }
+    //Ajoute la longueur de la chaine au total
+    this->encoder->length += strlen(str) + 2; //Chaine + ", "
+    //Ajoute le noeud
+    add_json_array_node(this, str);
+    return true;
+}
+
+boolean add_array_string(JsonArray* this, char* val){
+    //Verification
+    if(!this->mode){
+        return false;
+    }
+    //Creation chaine
+    int length = strlen(val) + 2 + 1; //val + 2 guillemet + \0
+    char* str = malloc(length * sizeof(char)); 
+    memset(str, 0, length);
+    sprintf(str, "\"%s\"", val);
+    //Ajout
+    add_array_value(this, str);
+    free(str);
+    return true;
+}
+
+boolean add_array_number(JsonArray* this, double ndigit, int val){
+    //Verification
+    if(!this->mode){
+        return false;
+    }
+    //Double en string
+    char nombre[20];
+    memset(nombre, 0, 20);
+    ftoa(val, nombre, ndigit);
+    //Ajout
+    add_array_value(this, nombre);
+    return true;
+}
+
+boolean add_array_integer(JsonArray* this, int val){
+    //Verification
+    if(!this->mode){
+        return false;
+    }
+    //Creation chaine
+    int length = ceil(val/10.0) + 1; //val + \0
+    char* str = malloc(length * sizeof(char)); 
+    memset(str, 0, length);
+    sprintf(str, "%d", val);
+    //Ajout
+    add_array_value(this, str);
+    free(str);
+    return true;
+}
+
+boolean add_array_boolean(JsonArray* this, boolean val){
+    //Verification
+    if(!this->mode){
+        return false;
+    }
+    //On determine le boolean
+    char bool[6];
+    if(val){
+        strcpy(bool, "true");
+    } else {
+        strcpy(bool, "false");
+    }
+    //Ajout
+    add_array_value(this, bool);
+    return true;
+}
+
+boolean add_array_array(JsonArray* this, JsonArray* val){
+    //Verification
+    if(!this->mode){
+        return false;
+    }
+    //Ajout
+    add_array_value(this, json_encode_array(val));
+    return true;
+}
+
+
+boolean add_array_object(JsonArray* this, JsonEncoder* val){
+    //Verification
+    if(!this->mode){
+        return false;
+    }
+    //Ajout
+    add_array_value(this, json_encode(val));
+    return true;
+}
+
+char* json_encode_array(JsonArray* this){
+    boolean first = true;
+    //Allocation chaine
+    char* str;
+    str = malloc((this->encoder->length + 2) * sizeof(char)); // La chaine + []
+    memset(str, 0, this->encoder->length + 2);
+    //Creation de la chaine
+    JsonNode* node;
+    node = this->encoder->tab;
+    str[0] = '[';
+    while(node != NULL){
+        if(first){
+            sprintf(str, "%s%s", str, node->str);
+            first = false;
+        } else {
+            sprintf(str, "%s, %s", str, node->str);
+        }
+        node = node->next;
+    }
+    sprintf(str, "%s]", str);
+    //Retour
+    return str;
+}
+
+/* --- Fonctions publiques --- */

+ 2 - 2
Serveur/json_encoder.c

@@ -118,10 +118,10 @@ void add_array(JsonEncoder* this, char* key, char* val){
     add_string(this, key, val); //Pas de gestion special
 }
 
-void add_object(JsonEncoder* this, char* key, JsonEncoder val){
+void add_object(JsonEncoder* this, char* key, JsonEncoder* val){
     //Recup string du JsonEncoder
     char* json;
-    json = json_encode(&val);
+    json = json_encode(val);
     //Creation chaine
     int length = strlen(key) + strlen(json) + 2 + 2 + 1; //clef + val + 2 guillemets + ": " + \0
     char* str = malloc(length * sizeof(char));

+ 1 - 1
Serveur/json_parser.c

@@ -344,7 +344,7 @@ char* get_string(JsonParser* this, char* key){
     }
     //Verif type
     if(this->type[index] != JSON_STRING){
-        return JSON_ERROR;
+        return NULL;
     }
     return get_index(this, index);
 }

+ 2 - 2
Serveur/makefile

@@ -3,7 +3,7 @@
 #
 
 EXEC = main test
-OBJETS = str.o json.o json_parser.o json_encoder.o error.o arraylist.o server_tcp.o server_udp.o bomberstudent_server.o client.o file.o handler.o
+OBJETS = str.o json_parser.o json_encoder.o json_array.o error.o arraylist.o server_tcp.o server_udp.o bomberstudent_server.o client.o file.o handler.o
 NOM_PROJET = Projet Reseau
 
 #
@@ -95,9 +95,9 @@ archive: clean
 
 # DEPENDANCES
 str.o: str.c str.h
-json.o: json.c json.h str.h constante.h
 json_parser.o: json_parser.c json.h str.h constante.h
 json_encoder.o: json_encoder.c json.h str.h constante.h
+json_array.o: json_array.c json.h str.h constante.h
 error.o: error.c str.h error.h
 arraylist.o: arraylist.c str.h arraylist.h constante.h server.h json.h
 server_tcp.o: server_tcp.c error.h server.h constante.h

+ 15 - 2
Serveur/test.c

@@ -64,7 +64,7 @@ int encode(){
     JsonEncoder json2;
     ini_encoder(&json2);
     add_integer(&json2, "vie", 42);
-    add_object(&json2, "obj", json);
+    add_object(&json2, "obj", &json);
     printf("\nJson 2\n");
     printf("%s\n", json_encode(&json2));
     
@@ -184,6 +184,18 @@ int files(){
     return 1;
 }
 
+int array_encode(){
+    JsonArray ja;
+    ini_array_encoder(&ja);
+    add_array_number(&ja, 3, 4.5896);
+    add_array_string(&ja, "String");
+    add_array_boolean(&ja, 1);
+    add_array_array(&ja, &ja);
+    printf("%s\n", json_encode_array(&ja));
+    
+    return 1;
+}
+
 int main(){
     //return parse();
     //return encode();
@@ -209,5 +221,6 @@ int main(){
     error.exit();
     //*/
     
-    return files();
+    //return files();
+    return array_encode();
 }