Simple JSON  0.1
A free simple json library for C
simple_json_array.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_list.h"
6 #include "simple_json_array.h"
7 #include "simple_json_error.h"
8 
9 int sj_array_check(SJson *json)
10 {
11  if (!json)return 0;
12  if (json->sjtype != SJVT_Array)
13  {
14  sj_set_error("sj_array: expected type ARRAY");
15  return 0;
16  }
17  return 1;
18 }
19 
21 {
22  SJson *item;
23  SJson *array;
24  int i,count;
25  if (!json)return NULL;
26  array = sj_array_new();
27  if (!array)return NULL;
28  count = sj_list_get_count(json->v.array);
29  for (i = 0; i < count; i++)
30  {
31  item = (SJson *)sj_list_get_nth(json->v.array,i);
32  if (!item)continue;
33  sj_array_append(array,sj_copy(item));
34  }
35  return array;
36 }
37 
39 {
40  SJson *array;
41  array = sj_new();
42  if (!array)return NULL;
43  array->sjtype = SJVT_Array;
44  array->json_free = sj_array_free;
46  array->copy = sj_array_copy;
47  array->v.array = sj_list_new();
48  return array;
49 }
50 
52 {
53  int i,count;
54  SJson *item;
55  if (!sj_array_check(array))return;
56  count = sj_list_get_count(array->v.array);
57  for (i = 0; i < count; i++)
58  {
59  item = (SJson *)sj_list_get_nth(array->v.array,i);
60  if (!item)continue;
61  if (item->json_free)item->json_free(item);
62  }
63  sj_list_delete(array->v.array);
64 }
65 
67 {
68  if (!sj_array_check(array))return 0;
69  return sj_list_get_count(array->v.array);
70 }
71 
73 {
74  if (!sj_array_check(array))return;
75  if (!value)return;
76  sj_list_append(array->v.array,value);
77 }
78 
80 {
81  if (!sj_array_check(array))return NULL;
82  return (SJson *)sj_list_get_nth(array->v.array,n);
83 }
84 
86 {
87  SJson *item;
88  if (!sj_array_check(array))return NULL;
89  item = (SJson *)sj_list_get_nth(array->v.array,n);
90  if (!item)return NULL;
91  if (item->sjtype != SJVT_String)return NULL;
92  return item->v.string;
93 }
94 
96 {
98  SJson *value;
99  int i, count;
100  if (!sj_array_check(array))return NULL;
101  string = sj_string_new_text("[");
102  //for each
103  count = sj_list_get_count(array->v.array);
104  for (i = 0; i < count; i++)
105  {
106  value = sj_list_get_nth(array->v.array,i);
107  if (!value)continue;
109  if (i +1 < count)sj_string_append(string,",");
110  }
111  sj_string_append(string,"]");
112  return string;
113 }
114 
115 /*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
void sj_array_append(SJson *array, SJson *value)
append to a JSON array a new value
int sj_array_get_count(SJson *array)
get the number of elements in the json array
SJValueTypes sjtype
void * sj_list_get_nth(SJList *list, unsigned int n)
get the data stored at the nth element
SJList * sj_list_new()
create a new list
SJString * sj_array_to_json_string(SJson *array)
get the contents of the array as a formatted json string for output
void(* json_free)(struct SJson_S *json)
SJson * sj_array_copy(SJson *json)
union SJson_S::@0 v
void sj_set_error(char *er)
used internally to se the error message
void sj_array_free(SJson *array)
a basic structure that keeps track of a string and its length Automatically grows to accomodate longe...
SJList * array
SJson * sj_array_new()
allocate a new empty json array
SJString * string
SJString * sj_array_get_nth_as_string(SJson *array, int n)
retrieve the nth element in the json array assumed to be a string
void sj_list_delete(SJList *list)
deletes a list that has been previously allocated
SJson * sj_copy(SJson *json)
make a duplicate of a json structure.
Definition: simple_json.c:109
SJson * sj_array_get_nth(SJson *array, int n)
retrieve the nth element in the json array
struct SJson_S *(* copy)(struct SJson_S *json)
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
int sj_array_check(SJson *json)
SJString * sj_string_new_text(char *s)
make a new string with the characters from array s
SJString *(* get_string)(struct SJson_S *json)