forked from scanoss/wayuu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_utils.c
140 lines (127 loc) · 3.9 KB
/
json_utils.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2018-2020 SCANOSS LTD
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "string_utils.h"
#include "log.h"
#include "json_utils.h"
#define JSON_ARRAY_BEGIN "["
#define JSON_ARRAY_STR_BEGIN "[\""
#define JSON_ARRAY_END "]"
#define JSON_ARRAY_STR_END "\"]"
#define JSON_ARRAY_STR_SEP "\",\""
#define MAX_SINGLE_JSON_ENTITY_STR_LEN 512
#define MAX_INT_STRING_LEN 32
char *json_array_of_strings(char **array, int size)
{
if (array == NULL || size < 1)
{
return strdup("[]");
}
// calculate size of string
int string_size = strlen(JSON_ARRAY_STR_BEGIN) + strlen(JSON_ARRAY_STR_END) + size * strlen(JSON_ARRAY_STR_SEP) + 2;
for (int i = 0; i < size; i++)
{
if (array[i] != NULL)
string_size += strlen(array[i]);
}
char *json = calloc(string_size, sizeof(char));
strcpy(json, JSON_ARRAY_STR_BEGIN);
char *joined_str = join(array, size, JSON_ARRAY_STR_SEP);
strcat(json, joined_str);
strcat(json, JSON_ARRAY_STR_END);
free(joined_str);
return json;
}
char *json_array_of_entities(json_list_t list)
{
if (list.size == 0)
{
char *json = malloc(strlen(JSON_ARRAY_BEGIN) + strlen(JSON_ARRAY_END) + 2);
sprintf(json, "%s%s", JSON_ARRAY_BEGIN, JSON_ARRAY_END);
return json;
}
char **json_entities = calloc(list.size, MAX_SINGLE_JSON_ENTITY_STR_LEN);
for (int i = 0; i < list.size; i++)
{
json_entities[i] = list.serializer(list.elements[i]);
// TODO reallocate memory if json_one exceeds length of max single json entity string.
}
char *joined_str = join(json_entities, list.size, ",");
char *json = malloc(strlen(joined_str) + list.size + strlen(JSON_ARRAY_BEGIN) + strlen(JSON_ARRAY_END) + 2);
strcpy(json, JSON_ARRAY_BEGIN);
strcat(json, joined_str);
strcat(json, JSON_ARRAY_END);
if (joined_str && *joined_str!='\0')
free(joined_str);
for (int i = 0; i < list.size; i++)
{
free(json_entities[i]);
}
free(json_entities);
return json;
}
char *json_key_value(char *key, char *value)
{
char *json = calloc(1, strlen(key) + strlen(value) + 15);
sprintf(json, "\"%s\":\"%s\"", key, value? value : "");
return json;
}
char *json_key_value_int(char *key, int value)
{
char *json = calloc(1, strlen(key) + MAX_INT_STR + 15);
sprintf(json, "\"%s\":%d", key, value);
return json;
}
char *json_key_value_bool(char *key, bool value)
{
char *json = calloc(1, strlen(key) + MAX_INT_STR + 15);
sprintf(json, "\"%s\":%s", key, value ? "true": "false");
return json;
}
char *json_key_value_uint32_t(char *key, uint32_t value)
{
char *json = calloc(1, strlen(key) + MAX_INT_STR + 15);
sprintf(json, "\"%s\":%u", key, value);
return json;
}
char *json_array_of_ints(int array[], int size)
{
log_debug("Printing an array of ints with size: %d", size);
if (array == NULL || size < 1)
{
return strdup("[]");
}
char *json = calloc(size, (MAX_INT_STRING_LEN + 2));
strcat(json, JSON_ARRAY_BEGIN);
for (int i = 0; i < size; i++)
{
log_debug("array[%d]: %d", i, array[i]);
char *int_str = malloc(MAX_INT_STRING_LEN + 1);
sprintf(int_str, "%d", array[i]);
strcat(json, int_str);
free(int_str);
if (i < (size - 1))
{
strcat(json, ",");
}
}
strcat(json, JSON_ARRAY_END);
return json;
}