main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "json.h"
  4. int parse(){
  5. char str[200];
  6. char* key = NULL;
  7. char* val = NULL;
  8. JsonParser json;
  9. strcpy(str, "{\"name\" : \"Jack\", \"age\": 27, \"test\": true, \"tab\": [1, 2, 3, 4, 5], \"obj\": {\"name\" : \"Jack\", \"age\": 27}, \"nb\": 27.8 }");
  10. //Parse
  11. int a = json_parse(&json, str);
  12. printf("Parse : %d\n", a);
  13. //Affiche toutes les clefs : valeurs
  14. for(int i = 0; i < json.elt; i++){
  15. key = key_index(&json, i);
  16. val = get_index(&json, i);
  17. printf("%s : %s\n", key, val);
  18. }
  19. //Recup un nombre
  20. printf("Double : %f %.2f | Int : %d %d\n", get_number(&json, "age"), get_number(&json, "nb"), get_integer(&json, "age"), get_integer(&json, "nb"));
  21. //Recup boolean
  22. printf("Bool : %d %d\n", get_boolean(&json, "test"), get_boolean(&json, "tab"));
  23. //Recup obj
  24. JsonParser* js;
  25. js = get_object(&json, "obj");
  26. if(js != NULL){
  27. int key_i = get_pos(js, "name");
  28. printf("JSON : %s %s, age %d\n", key_index(js, key_i), get_value(js, "name"), get_integer(js, "age"));
  29. } else {
  30. printf("JSON : Error");
  31. }
  32. //Supprime
  33. free(key);
  34. free(val);
  35. clean_json_parser(&json);
  36. return 0;
  37. }
  38. int encode(){
  39. //Encode
  40. JsonEncoder json;
  41. ini_encoder(&json);
  42. add_string(&json, "name", "robert");
  43. add_number(&json, "nb", 25.698, 2);
  44. add_integer(&json, "int", 846);
  45. add_string(&json, "aze", "rty");
  46. add_boolean(&json, "bool", false);
  47. add_value(&json, "\"test\": \"aze\nrty\"");
  48. printf("Json\n");
  49. printf("%s\n", json_encode(&json));
  50. //Encode un JsonEncoder
  51. JsonEncoder json2;
  52. ini_encoder(&json2);
  53. add_integer(&json2, "vie", 42);
  54. add_object(&json2, "obj", json);
  55. printf("\nJson 2\n");
  56. printf("%s\n", json_encode(&json2));
  57. //Decode
  58. JsonParser parser, *parser2;
  59. char* key, *val;
  60. json_parse(&parser, json_encode(&json2));
  61. //Affiche toutes les clefs : valeurs
  62. printf("\nParser\n");
  63. for(int i = 0; i < parser.elt; i++){
  64. key = key_index(&parser, i);
  65. val = get_index(&parser, i);
  66. printf("%s : %s\n", key, val);
  67. }
  68. //Lecture du sous json
  69. parser2 = get_object(&parser, "obj");
  70. //Affiche toutes les clefs : valeurs
  71. printf("\nParser 2\n");
  72. for(int i = 0; i < parser2->elt; i++){
  73. key = key_index(parser2, i);
  74. val = get_index(parser2, i);
  75. printf("%s : %s\n", key, val);
  76. }
  77. //Clean
  78. clean_json_encoder(&json);
  79. clean_json_encoder(&json2);
  80. clean_json_parser(&parser);
  81. return 0;
  82. }
  83. int main(){
  84. //return parse();
  85. return encode();
  86. }