libsheepy

C lib for handling text files, strings and json like data structure with an object oriented system
git clone https://spartatek.se/git/libsheepy.git
Log | Files | Refs | README | LICENSE

json.c (2196B)


      1 //
      2 
      3 #include "../release/libsheepyObject.h"
      4 
      5 #define internal static
      6 
      7 #include <stdlib.h>
      8 #include <stdbool.h>
      9 #include <stdio.h>
     10 
     11 #ifndef unitTest
     12 #endif
     13 int MAIN(int ARGC, char** ARGV);
     14 
     15 int argc; char **argv;
     16 
     17 #ifndef unitTest
     18 // Remove main when running the unit tests
     19 #define MAIN   main
     20 #endif
     21 int MAIN(int ARGC, char** ARGV) {
     22   int dummy UNUSED;
     23 
     24   argc = ARGC; argv = ARGV;;// load file.json and parse
     25   // load file.yml and parse
     26 
     27   // Steps
     28   // create json object
     29   // load text to file array
     30   // parse
     31   // print json
     32   // print in yml format
     33   // print keys and values
     34   // get a bool value and print
     35   // load yml text to file array
     36   // parse yml
     37   // print json
     38   // print in yml format
     39 
     40   // create json object
     41   createAllocateSmallJson(json);
     42 
     43   // parse json
     44 
     45   puts(BLD GRN "Loading JSON file:" RST " file.json");
     46 
     47   // load text to file array
     48   createAllocateSmallArray(file);
     49   readFileG(file, "file.json");
     50   cleanSmallStringP(jsonText) = joinG(file, "\n");
     51 
     52   // parse
     53   parseG(json, ssGet(jsonText));
     54   // print json
     55   logNFree(stringifyG(json,2)); {
     56   // print in yml format
     57   logNFree(toYMLG(json,2));
     58 
     59   // print keys and values
     60   puts(UDL BLD BGBLU "dictionary foreach:" RST);
     61   cast(smallDictt *, d, getTopO(json));
     62   logG(d);
     63   const char *key;
     64   iter(d, value) {
     65     key = iterKeyG(d);
     66     smallStringt *s = allocG(key);;
     67     colorG(s, BGBLU);
     68     char *V = toStringG(value);;
     69     smallStringt *w = allocG(V);;
     70     free(V);
     71     colorG(w, BLD BGMGT);
     72     printf("Key: %s, value: %s", ssGet(s), ssGet(w));
     73     printf("\n");
     74     terminateManyG(s, w);
     75   }
     76   finishO(d);
     77   put
     78 
     79   // get a bool value and print
     80   bool b = getG(json, b, "\"dict\"\"asd\"");
     81   if (b) {
     82     printf("\ndict.asd is true\n\n");
     83     printf("\n");
     84   }
     85 
     86   freeG(json);
     87   freeG(file);
     88 
     89   puts(BLD GRN "Loading YML file:" RST " fileA.yml");
     90 
     91   // load yml text to file array
     92   readFileG(file, "fileA.yml");
     93   smallStringt *yText = joinG(file, "\n");
     94 
     95   // parse yml
     96   parseYMLG(json, ssGet(yText));
     97   // print json
     98   logNFree(stringifyG(json,2)); {
     99   // print in yml format
    100   logNFree(toYMLG(json,2));
    101   putsG(json);
    102 
    103   logVarG(b);
    104   logVarG(file);
    105   terminateManyG(json, file, yText);
    106 }
    107   }
    108   }