Coverage Report

Created: 2026-08-25 07:15

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
118k
{
32
118k
  cJSON *jtmp;
33
34
118k
  if(optional == true){
35
61.8k
    *value = default_value;
36
61.8k
  }
37
38
118k
  jtmp = cJSON_GetObjectItem(json, name);
39
118k
  if(jtmp){
40
1.74k
    if(cJSON_IsBool(jtmp) == false){
41
308
      return MOSQ_ERR_INVAL;
42
308
    }
43
1.43k
    *value = cJSON_IsTrue(jtmp);
44
116k
  }else{
45
116k
    if(optional == false){
46
56.0k
      return MOSQ_ERR_INVAL;
47
56.0k
    }
48
116k
  }
49
62.3k
  return MOSQ_ERR_SUCCESS;
50
118k
}
51
52
53
int json_get_int(cJSON *json, const char *name, int *value, bool optional, int default_value)
54
139k
{
55
139k
  cJSON *jtmp;
56
57
139k
  if(optional == true){
58
139k
    *value = default_value;
59
139k
  }
60
61
139k
  jtmp = cJSON_GetObjectItem(json, name);
62
139k
  if(jtmp){
63
4.60k
    if(cJSON_IsNumber(jtmp) == false){
64
196
      return MOSQ_ERR_INVAL;
65
196
    }
66
4.41k
    *value  = jtmp->valueint;
67
135k
  }else{
68
135k
    if(optional == false){
69
0
      return MOSQ_ERR_INVAL;
70
0
    }
71
135k
  }
72
139k
  return MOSQ_ERR_SUCCESS;
73
139k
}
74
75
76
int json_get_int64(cJSON *json, const char *name, int64_t *value, bool optional, int64_t default_value)
77
5.68k
{
78
5.68k
  cJSON *jtmp;
79
80
5.68k
  if(optional == true){
81
5.68k
    *value = default_value;
82
5.68k
  }
83
84
5.68k
  jtmp = cJSON_GetObjectItem(json, name);
85
5.68k
  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.68k
  }else{
91
5.68k
    if(optional == false){
92
0
      return MOSQ_ERR_INVAL;
93
0
    }
94
5.68k
  }
95
5.68k
  return MOSQ_ERR_SUCCESS;
96
5.68k
}
97
98
99
int json_get_string(cJSON *json, const char *name, const char **value, bool optional)
100
807k
{
101
807k
  cJSON *jtmp;
102
103
807k
  *value = NULL;
104
105
807k
  jtmp = cJSON_GetObjectItem(json, name);
106
807k
  if(jtmp){
107
411k
    if(cJSON_IsString(jtmp) == false){
108
316
      return MOSQ_ERR_INVAL;
109
316
    }
110
410k
    if(jtmp->valuestring){
111
410k
      *value = jtmp->valuestring;
112
410k
    }else{
113
0
      *value = "";
114
0
    }
115
410k
  }else{
116
396k
    if(optional == false){
117
396k
      return MOSQ_ERR_INVAL;
118
396k
    }
119
396k
  }
120
410k
  return MOSQ_ERR_SUCCESS;
121
807k
}
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
}