123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "json.h"
- /* --- Fonctions privées --- */
- int skip_object(char* json) {
- int compteur = 1;
- json++;
- //On compte le nombre caractere à sauter
- while (*json && *json != '}') {
- //Si on trouve un autre objet dans l'objet
- if (*json == '{') {
- skip_object(json);
- }
- compteur++;
- json++;
- }
- return compteur;
- }
- int skip_array(char* json) {
- int compteur = 1;
- json++;
- //On compte le nombre caractere à sauter
- while (*json && *json != ']') {
- json++;
- compteur++;
- }
- return compteur;
- }
- int key_counter(char* json){
- //Declaration variable
- int compteur = 0;
- boolean key = true;
- //On parcours la chaine une 1er fois pour compter le nombre de clef
- while (*json && *json != '}') {
- //Si on trouve le debut d'une clef
- if (*json == '"' && key) {
- compteur++;
- key = false;
- }
- //Si on tombe sur un autre objet json
- else if (*json == '{' && !key) {
- json += skip_object(json);
- }
- //Si on tombe sur un tableau
- else if (*json == '[' && !key) {
- json += skip_array(json);
- }
- //Si on trouve un separateur de valeur
- else if (*json == ',' && !key) {
- //Le prochain element est une clefS
- key = true;
- }
- json++;
- }
- //Retour
- return compteur;
- }
- int parse_key(JsonParser* this, int index, char* json) {
- //Declaration variable
- int compteur = 0;
- //On parcours jusqu'a la fin de la clef
- json++;
- while (*json && *json != '"') {
- json++;
- compteur++;
- }
- //Si json mal formé
- if (!*json) {
- return JSON_ERROR;
- }
- //Setup les infos
- this->key[index] = json - compteur;
- this->key_length[index] = compteur;
- //Sinon retourne ok
- return ++compteur;
- }
- int parse_val(JsonParser* this, int index, char* json){
- //Declaration variable
- int compteur = 0;
- //Regarde le type de valeur
- switch(*json){
- //String
- case '"':
- //Cherche le guillement de fin
- json++;
- while (*json && *json != '"') {
- json++;
- compteur++;
- }
- //Si json mal formé
- if (!*json) {
- return JSON_ERROR;
- }
- //Setup les infos
- this->val[index] = json - compteur;
- this->val_length[index] = compteur;
- this->type[index] = JSON_STRING;
- break;
- //Boolean
- case 't':
- compteur = 4;
- this->val[index] = json;
- this->val_length[index] = compteur;
- this->type[index] = JSON_BOOLEAN;
- break;
- case 'f':
- compteur = 5;
- this->val[index] = json;
- this->val_length[index] = compteur;
- this->type[index] = JSON_BOOLEAN;
- break;
- //Nombre
- case '-':
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- //Cherche espace de fin ou fin json ou suite json
- while (*json && *json != ' ' && *json != '}' && *json != ',') {
- json++;
- compteur++;
- }
- //Si json mal formé
- if (!*json) {
- return JSON_ERROR;
- }
- //Setup les infos
- this->val[index] = json - compteur;
- this->val_length[index] = compteur;
- this->type[index] = JSON_NUMBER;
- break;
- //Tableau
- case '[':
- compteur = skip_array(json) + 1;
- this->val[index] = json;
- this->val_length[index] = compteur;
- this->type[index] = JSON_ARRAY;
- break;
- //Objet
- case '{':
- compteur = skip_object(json) + 1;
- this->val[index] = json;
- this->val_length[index] = compteur;
- this->type[index] = JSON_OBJECT;
- break;
- //Autre
- default:
- return JSON_ERROR;
- }
- //Retour
- return ++compteur;
- }
- int parse_error(JsonParser* this){
- clean_json_parser(this);
- return JSON_ERROR;
- }
- int search_index(JsonParser* this, char* key){
- int length = strlen(key);
- //Cherche la clef
- for(int i = 0; i < this->elt; i++){
- if(strncmp(this->key[i], key, length) == 0){
- return i;
- }
- }
- //Si rien trouver
- return JSON_ERROR;
- }
- /* --- Fonctions publiques --- */
- int json_parse(JsonParser* this, char* json) {
- //Declaration variable
- char* tmp;
- int temp, compteur;
- boolean key = true;
- //Compte le nombre de clef
- compteur = key_counter(json);
- //Allocation de la taille des tableaux
- this->str = malloc(strlen(json) * sizeof (char));
- strcpy(this->str, json);
- this->elt = compteur;
- this->key = malloc(compteur * sizeof (char*));
- this->key_length = malloc(compteur * sizeof (int));
- this->val = malloc(compteur * sizeof (char*));
- this->val_length = malloc(compteur * sizeof (int));
- this->type = malloc(compteur * sizeof (int));
- //On reparcours le tableau pour parser
- tmp = this->str;
- compteur = 0;
- while (*tmp && *tmp != '}') {
- //Si on trouve une clef
- if (*tmp == '"') {
- //Lecture clef de la clef
- if((temp = parse_key(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
- tmp += temp;
- key = false;
- }
- //Si on trouve une valeur
- else if(*tmp == ':'){
- //Si pas de claf avant
- if(key){ return parse_error(this); }
- //Saute les espaces
- tmp++;
- while(*tmp == ' ') { tmp++; }
- //Lecture valeur
- if((temp = parse_val(this, compteur, tmp)) == JSON_ERROR){ return parse_error(this); }
- tmp += temp;
- key = true;
- compteur++;
- }
- tmp++;
- }
- //Si on s'arrete sur une clef
- if(!key){
- return parse_error(this);
- }
- //Sinon ok
- return JSON_OK;
- }
- int get_type(JsonParser* this, char* key){
- //Recup index
- int index;
- if((index = search_index(this, key)) == JSON_ERROR){
- return JSON_ERROR;
- }
- return this->type[index];
- }
- char* get(JsonParser* this, char* key){
- //Recup index
- int index;
- if((index = search_index(this, key)) == JSON_ERROR){
- return NULL;
- }
- return get_index(this, index);
- }
- char* get_index(JsonParser* this, int index){
- //Index incorrect
- if(index < 0 || index >= this->elt){
- return NULL;
- }
- //Creation string d'accueil
- char* val;
- val = malloc(this->val_length[index] * sizeof(char));
- //Copie valeur
- strncpy(val, this->val[index], this->val_length[index]);
- //Retour
- return val;
- }
- void clean_json_parser(JsonParser* this) {
- free(this->key);
- free(this->key_length);
- free(this->val);
- free(this->val_length);
- free(this->type);
- free(this->str);
- }
|