Simple JSON  0.1
A free simple json library for C
simple_json.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "simple_json.h"
5 #include "simple_json_string.h"
6 #include "simple_json_parse.h"
7 #include "simple_json_error.h"
8 
9 SJson *sj_new_str(char *str)
10 {
12 }
13 
15 {
17 }
18 
20 {
22 }
23 
25 {
27 }
28 
29 long get_file_Size(FILE *file)
30 {
31  long size;
32 
33  if(file != NULL){
34  if( fseek(file, 0, SEEK_END) ){
35  fclose(file);
36  return -1;
37  }
38 
39  size = ftell(file);
40  rewind(file);
41  return size;
42  }
43 
44  return -1;
45 }
46 
47 SJson *sj_load(const char *filename)
48 {
49  FILE *file;
50  SJson *json;
51  long size,read;
52  char *buffer = NULL;
53  file = fopen(filename,"r");
54  if (!file)
55  {
56  sj_set_error("sj_load: failed to open file");
57  return NULL;
58  }
59  size = get_file_Size(file);
60  if (size <= 0)
61  {
62  sj_set_error("sj_load: error with file size");
63  fclose(file);
64  return NULL;
65  }
66  printf("loaded file %s with a size of %li characters\n",filename,size);
67 
68  buffer = (char *)malloc(sizeof(char)*(size + 1));
69 
70  if (buffer == NULL)
71  {
72  sj_set_error("sj_load: failed to allocate character buffer for json file");
73  fclose(file);
74  return NULL;
75  }
76  memset(buffer,0,sizeof(char)*(size+1));
77 
78  if ((read = fread(buffer, sizeof(char), size, file)) != size)
79  {
80  printf("expected to read %li characters, but read %li instead\n",size,read);
81  }
82  else
83  {
84  printf("read %li characters of %li available",read,size);
85  }
86  printf("file contents:\n%s\n",buffer);
87  fclose(file);
88 
89  json = sj_parse_buffer(buffer,read);
90 
91  free(buffer);
92 
93  return json;
94 }
95 
97 {
98  SJson *json;
99  json = (SJson *)malloc(sizeof(SJson));
100  if (!json)
101  {
102  sj_set_error("sj_new: failed to allocate space for new json object");
103  return NULL;
104  }
105  memset(json,0,sizeof(SJson));
106  return json;
107 }
108 
110 {
111  if (!json)return NULL;
112  if (!json->copy)return NULL;
113  return json->copy(json);
114 }
115 
116 void sj_free(SJson *json)
117 {
118  if (!json)return;
119 
120  if (json->json_free)json->json_free(json);
121 }
122 
123 void sj_save(SJson *json,char *filename)
124 {
125  FILE *file;
126  SJString *string;
127  if ((!json) || (!json->get_string))return;
128  string = json->get_string(json);
129  if (!string)return;
130  file = fopen(filename,"w");
131  if (!file)
132  {
133  sj_string_free(string);
134  return;
135  }
136  fputs(string->text,file);
137  fclose(file);
138 }
139 
140 void sj_echo(SJson *json)
141 {
142  SJString *output;
143  if ((!json) || (!json->get_string))return;
144  output = json->get_string(json);
145  if (!output)return;
146  printf("%s\n",output->text);
147  sj_string_free(output);
148 }
149 
150 void sj_null_free(SJson *json)
151 {
152  if (!json)return;
153  free(json);
154 }
155 
157 {
158  if (!json)return NULL;
159  if (!json->get_string)return NULL;
160  return json->get_string(json);
161 }
162 
164 {
165  return sj_string_new_text("null");
166 }
167 
169 {
170  return sj_null_new();
171 }
172 
174 {
175  SJson *json;
176  json = sj_new();
177  if (!json)return NULL;
178  json->sjtype = SJVT_NULL;
179  json->json_free = sj_null_free;
181  return json;
182 }
183 
184 const char *sj_get_string_value(SJson *json)
185 {
186  if ((!json)||(json->sjtype != SJVT_String))return NULL;
187  return json->v.string->text;
188 }
189 
190 int sj_get_integer_value(SJson *json,int *i)
191 {
192  if ((!json)||(json->sjtype != SJVT_String))return 0;
193  return sj_string_as_integer(json->v.string,i);
194 }
195 
196 int sj_get_float_value(SJson *json,float *f)
197 {
198  if ((!json)||(json->sjtype != SJVT_String))return 0;
199  return sj_string_as_float(json->v.string,f);
200 }
201 
202 int sj_get_bool_value(SJson *json,short int *b)
203 {
204  if ((!json)||(json->sjtype != SJVT_String))return 0;
205  return sj_string_as_bool(json->v.string,b);
206 }
207 
208 int sj_is_array(SJson *json)
209 {
210  if (!json)return 0;
211  if (json->sjtype != SJVT_Object)return 0;
212  return 1;
213 }
214 
215 int sj_is_object(SJson *json)
216 {
217  if (!json)return 0;
218  if (json->sjtype != SJVT_Array)return 0;
219  return 1;
220 }
221 
222 int sj_is_string(SJson *json)
223 {
224  if (!json)return 0;
225  if (json->sjtype != SJVT_String)return 0;
226  return 1;
227 }
228 
229 int sj_is_number(SJson *json)
230 {
231  if (!json)return 0;
232  if (json->sjtype != SJVT_String)return 0;
233  return sj_string_as_float(json->v.string,NULL);
234 }
235 
236 int sj_is_bool(SJson *json)
237 {
238  if (!json)return 0;
239  if (json->sjtype != SJVT_String)return 0;
240  return sj_string_as_bool(json->v.string,NULL);
241 }
242 
243 int sj_is_null(SJson *json)
244 {
245  if (!json)return 0;
246  if (json->sjtype != SJVT_NULL)return 0;
247  return 1;
248 }
249 
250 
251 
252 /*eol@eof*/
SJString * sj_string_new_float(float f)
SJson * sj_null_copy(SJson *json)
Definition: simple_json.c:168
SJString * sj_value_to_json_string(SJson *json)
convert the json value into a json string
Definition: simple_json.c:156
this is the abstract container structure for all json data This structure may be an object...
SJson * sj_new()
allocate a new empty json object
Definition: simple_json.c:96
int sj_string_as_integer(SJString *string, int *output)
get the integer value if the string is a numer
int sj_string_as_float(SJString *string, float *output)
get the float value if the string is a numer
int sj_is_number(SJson *json)
check if the json is a number
Definition: simple_json.c:229
int sj_get_bool_value(SJson *json, short int *b)
get the json value as a bool Can be used to check for existence
Definition: simple_json.c:202
void sj_echo(SJson *json)
print the contents of the json file to stdout
Definition: simple_json.c:140
SJString * sj_string_new_bool(int i)
void sj_null_free(SJson *json)
Definition: simple_json.c:150
SJValueTypes sjtype
SJson * sj_new_float(float f)
make a new json value that is a float
Definition: simple_json.c:19
SJson * sj_parse_buffer(char *string, unsigned long length)
given a string, create a JSON object from it
int sj_is_object(SJson *json)
check if the json is an object
Definition: simple_json.c:215
SJson * sj_null_new()
make a new json value that is NULL
Definition: simple_json.c:173
long get_file_Size(FILE *file)
Definition: simple_json.c:29
int sj_is_array(SJson *json)
check if the json is an array
Definition: simple_json.c:208
SJson * sj_load(const char *filename)
loads and parses a json file
Definition: simple_json.c:47
void(* json_free)(struct SJson_S *json)
int sj_is_string(SJson *json)
check if the json is a string
Definition: simple_json.c:222
SJson * sj_string_to_value(SJString *string)
make a jason value object out of a string
union SJson_S::@0 v
void sj_set_error(char *er)
used internally to se the error message
a basic structure that keeps track of a string and its length Automatically grows to accomodate longe...
SJString * string
SJString * sj_string_new_integer(int i)
int sj_get_float_value(SJson *json, float *f)
get the json value as a float Can be used to check for existence
Definition: simple_json.c:196
int sj_string_as_bool(SJString *string, short int *output)
get the bool value if the string is a bool
SJson * sj_new_bool(int b)
make a new json value that is a bool
Definition: simple_json.c:24
int sj_get_integer_value(SJson *json, int *i)
get the json value as an integer Can be used to check for existence
Definition: simple_json.c:190
const char * sj_get_string_value(SJson *json)
get the JSON value as a string
Definition: simple_json.c:184
SJson * sj_new_str(char *str)
make a new json value that is a string
Definition: simple_json.c:9
SJson * sj_new_int(int i)
make a new json value that is an integer
Definition: simple_json.c:14
void sj_string_free(SJString *string)
free an SJString
int sj_is_null(SJson *json)
check if the json is NULL
Definition: simple_json.c:243
SJson * sj_copy(SJson *json)
make a duplicate of a json structure.
Definition: simple_json.c:109
void sj_free(SJson *json)
frees a previously loaded json struct
Definition: simple_json.c:116
struct SJson_S *(* copy)(struct SJson_S *json)
void sj_save(SJson *json, char *filename)
write a json value as a formatted json string to file
Definition: simple_json.c:123
int sj_is_bool(SJson *json)
check if the json is a bool
Definition: simple_json.c:236
SJString * sj_string_new_text(char *s)
make a new string with the characters from array s
SJString * sj_null_to_json_string(SJson *json)
Definition: simple_json.c:163
SJString *(* get_string)(struct SJson_S *json)