Simple JSON  0.1
A free simple json library for C
simple_json_object.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "simple_json.h"
5 #include "simple_json_object.h"
6 #include "simple_json_list.h"
7 #include "simple_json_string.h"
8 #include "simple_json_error.h"
9 
11 {
12  if (!json)return 0;
13  if (json->sjtype != SJVT_Object)
14  {
15  sj_set_error("sj_object: expected type OBJECT");
16  return 0;
17  }
18  return 1;
19 }
20 
24 typedef struct
25 {
28 }SJPair;
29 
30 void sj_pair_free(SJPair *pair)
31 {
32  if (!pair)return;
33  sj_string_free(pair->key);
34  sj_free(pair->value);
35  free(pair);
36 }
37 
38 SJPair *sj_pair_new(char *key,SJson *value)
39 {
40  SJPair *pair;
41  pair = (SJPair *)malloc(sizeof(SJPair));
42  if (!pair)
43  {
44  sj_set_error("failed to allocate data for new data pair in object");
45  return NULL;
46  }
47  pair->key = sj_string_new_text(key);
48  if (!pair->key)
49  {
50  sj_pair_free(pair);
51  sj_set_error("failed to set key for object pair");
52  return NULL;
53  }
54  pair->value = value;
55  return pair;
56 }
57 
58 void sj_object_insert(SJson *object,char *key,SJson *value)
59 {
60  SJPair *pair;
61  if (!sj_object_check(object))return;
62  if (!key)return;
63  if (!value)return;
64  pair = sj_pair_new(key,value);
65  if (!pair)return;
66  sj_list_append(object->v.array,pair);
67 }
68 
70 {
71  SJPair *pair;
72  SJson *object;
73  int i,count;
74  if (!json)return NULL;
75  object = sj_object_new();
76  if (!object)return NULL;
77  count = sj_list_get_count(json->v.array);
78  for (i = 0; i < count; i++)
79  {
80  pair = sj_list_get_nth(json->v.array,i);
81  if (!pair)continue;
82  sj_object_insert(object,pair->key->text,sj_copy(pair->value));
83  }
84  return object;
85 }
86 
88 {
89  SJson *object;
90  object = sj_new();
91  if (!object)return NULL;
92  object->sjtype = SJVT_Object;
93  object->v.array = sj_list_new();
94  object->json_free = sj_object_free;
95  object->get_string = sj_object_to_json_string;
96  object->copy = sj_object_copy;
97  return object;
98 }
99 
100 void sj_object_free(SJson *object)
101 {
102  int i,count;
103  SJPair *pair;
104  if (!sj_object_check(object))return;
105  count = sj_list_get_count(object->v.array);
106  for (i = 0; i < count; i++)
107  {
108  pair = sj_list_get_nth(object->v.array,i);
109  if (!pair)continue;
110  sj_pair_free(pair);
111  }
112  sj_list_delete(object->v.array);
113  free(object);
114 }
115 
116 SJson *sj_object_get_value(SJson *object,char *key)
117 {
118  int i,count;
119  SJPair *pair;
120  if (!sj_object_check(object))return NULL;
121  count = sj_list_get_count(object->v.array);
122  for (i = 0; i < count; i++)
123  {
124  pair = sj_list_get_nth(object->v.array,i);
125  if (!pair)continue;
126  if (sj_string_cmp(pair->key,key) == 0)
127  {
128  return pair->value;
129  }
130  }
131  return NULL;
132 }
133 
134 char *sj_object_get_value_as_string(SJson *object,char *key)
135 {
136  SJson *value;
137  value = sj_object_get_value(object,key);
138  if (!value)return NULL;
139  if (value->sjtype != SJVT_String)return NULL;
140  return sj_string_get_text(value->v.string);
141 }
142 
144 {
145  SJString *string;
146  SJPair *pair;
147  int i, count;
148  string = sj_string_new_text("{");
149  //for each
150  count = sj_list_get_count(object->v.array);
151  for (i = 0; i < count; i++)
152  {
153  pair = sj_list_get_nth(object->v.array,i);
154  if (!pair)continue;
155  sj_string_append(string,"\"");
156  sj_string_concat(string,pair->key);
157  sj_string_append(string,"\":");
159  if (i +1 < count)sj_string_append(string,",");
160  }
161  sj_string_append(string,"}");
162  return string;
163 }
164 
165 /*eol@eof*/
void sj_string_concat(SJString *string1, SJString *string2)
append the contents of string2 to string1
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
void sj_list_append(SJList *list, void *data)
add an element to the end of the list
SJson * sj_object_get_value(SJson *object, char *key)
get the json value from an object given a key
void sj_object_insert(SJson *object, char *key, SJson *value)
insert data into a json object
SJson * value
char * sj_string_get_text(SJString *string)
get the text back from the string
SJson * sj_object_new()
allocate a new empty json object
SJValueTypes sjtype
char * sj_object_get_value_as_string(SJson *object, char *key)
void sj_pair_free(SJPair *pair)
void * sj_list_get_nth(SJList *list, unsigned int n)
get the data stored at the nth element
SJson * sj_object_copy(SJson *json)
an object is a list of key/value pairs
SJList * sj_list_new()
create a new list
SJPair * sj_pair_new(char *key, SJson *value)
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...
SJList * array
void sj_object_free(SJson *object)
free a previously allocated json object
SJString * sj_object_to_json_string(SJson *object)
get the json object back as a formatted json string
SJString * string
void sj_list_delete(SJList *list)
deletes a list that has been previously allocated
void sj_string_free(SJString *string)
free an SJString
SJson * sj_copy(SJson *json)
make a duplicate of a json structure.
Definition: simple_json.c:109
int sj_object_check(SJson *json)
void sj_free(SJson *json)
frees a previously loaded json struct
Definition: simple_json.c:116
int sj_string_cmp(SJString *string, char *s)
performs a strcmp on the string with the provided character array
SJString * key
unsigned int sj_list_get_count(SJList *list)
get the number of tracked elements in the list
void sj_string_append(SJString *string, char *buffer)
append the contents of buffer to the string
SJString * sj_string_new_text(char *s)
make a new string with the characters from array s