Coverage Report

Created: 2025-11-07 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mosquitto/common/json_help.c
Line
Count
Source
1
/*
2
Copyright (c) 2020-2021 Roger Light <roger@atchoo.org>
3
4
All rights reserved. This program and the accompanying materials
5
are made available under the terms of the Eclipse Public License 2.0
6
and Eclipse Distribution License v1.0 which accompany this distribution.
7
8
The Eclipse Public License is available at
9
   https://www.eclipse.org/legal/epl-2.0/
10
and the Eclipse Distribution License is available at
11
  http://www.eclipse.org/org/documents/edl-v10.php.
12
13
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14
15
Contributors:
16
   Roger Light - initial implementation and documentation.
17
*/
18
19
#include "config.h"
20
21
#include <cjson/cJSON.h>
22
#include <stdbool.h>
23
#include <stdlib.h>
24
#include <stdio.h>
25
26
#include "json_help.h"
27
#include "mosquitto.h"
28
29
30
int json_get_bool(cJSON *json, const char *name, bool *value, bool optional, bool default_value)
31
122k
{
32
122k
  cJSON *jtmp;
33
34
122k
  if(optional == true){
35
62.6k
    *value = default_value;
36
62.6k
  }
37
38
122k
  jtmp = cJSON_GetObjectItem(json, name);
39
122k
  if(jtmp){
40
1.91k
    if(cJSON_IsBool(jtmp) == false){
41
200
      return MOSQ_ERR_INVAL;
42
200
    }
43
1.71k
    *value = cJSON_IsTrue(jtmp);
44
120k
  }else{
45
120k
    if(optional == false){
46
58.7k
      return MOSQ_ERR_INVAL;
47
58.7k
    }
48
120k
  }
49
63.4k
  return MOSQ_ERR_SUCCESS;
50
122k
}
51
52
53
int json_get_int(cJSON *json, const char *name, int *value, bool optional, int default_value)
54
149k
{
55
149k
  cJSON *jtmp;
56
57
149k
  if(optional == true){
58
149k
    *value = default_value;
59
149k
  }
60
61
149k
  jtmp = cJSON_GetObjectItem(json, name);
62
149k
  if(jtmp){
63
4.58k
    if(cJSON_IsNumber(jtmp) == false){
64
195
      return MOSQ_ERR_INVAL;
65
195
    }
66
4.39k
    *value  = jtmp->valueint;
67
144k
  }else{
68
144k
    if(optional == false){
69
0
      return MOSQ_ERR_INVAL;
70
0
    }
71
144k
  }
72
148k
  return MOSQ_ERR_SUCCESS;
73
149k
}
74
75
76
int json_get_int64(cJSON *json, const char *name, int64_t *value, bool optional, int64_t default_value)
77
5.64k
{
78
5.64k
  cJSON *jtmp;
79
80
5.64k
  if(optional == true){
81
5.64k
    *value = default_value;
82
5.64k
  }
83
84
5.64k
  jtmp = cJSON_GetObjectItem(json, name);
85
5.64k
  if(jtmp){
86
0
    if(cJSON_IsNumber(jtmp) == false){
87
0
      return MOSQ_ERR_INVAL;
88
0
    }
89
0
    *value  = (int64_t)jtmp->valuedouble;
90
5.64k
  }else{
91
5.64k
    if(optional == false){
92
0
      return MOSQ_ERR_INVAL;
93
0
    }
94
5.64k
  }
95
5.64k
  return MOSQ_ERR_SUCCESS;
96
5.64k
}
97
98
99
int json_get_string(cJSON *json, const char *name, const char **value, bool optional)
100
819k
{
101
819k
  cJSON *jtmp;
102
103
819k
  *value = NULL;
104
105
819k
  jtmp = cJSON_GetObjectItem(json, name);
106
819k
  if(jtmp){
107
417k
    if(cJSON_IsString(jtmp) == false){
108
381
      return MOSQ_ERR_INVAL;
109
381
    }
110
417k
    if(jtmp->valuestring){
111
417k
      *value = jtmp->valuestring;
112
417k
    }else{
113
0
      *value = "";
114
0
    }
115
417k
  }else{
116
402k
    if(optional == false){
117
402k
      return MOSQ_ERR_INVAL;
118
402k
    }
119
402k
  }
120
417k
  return MOSQ_ERR_SUCCESS;
121
819k
}
122
123
124
cJSON *cJSON_AddIntToObject(cJSON * const object, const char * const name, long long number)
125
0
{
126
0
  char buf[30];
127
128
0
  snprintf(buf, sizeof(buf), "%lld", number);
129
0
  return cJSON_AddRawToObject(object, name, buf);
130
0
}
131
132
133
cJSON *cJSON_AddUIntToObject(cJSON * const object, const char * const name, unsigned long long number)
134
0
{
135
0
  char buf[30];
136
137
0
  snprintf(buf, sizeof(buf), "%llu", number);
138
0
  return cJSON_AddRawToObject(object, name, buf);
139
0
}