tjson.h 5.0 KB
Newer Older
X
Xiaoyu Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * 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.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TD_UTIL_JSON_H_
#define _TD_UTIL_JSON_H_

S
Shengliang Guan 已提交
19
#include "os.h"
X
Xiaoyu Wang 已提交
20
#include "tarray.h"
S
Shengliang Guan 已提交
21

X
Xiaoyu Wang 已提交
22 23 24 25
#ifdef __cplusplus
extern "C" {
#endif

X
Xiaoyu Wang 已提交
26 27
#define tjsonGetNumberValue(pJson, pName, val, code) \
  do {                                               \
28
    uint64_t _tmp = 0;                               \
wafwerar's avatar
wafwerar 已提交
29
    code = tjsonGetBigIntValue(pJson, pName, &_tmp); \
X
Xiaoyu Wang 已提交
30
    val = _tmp;                                      \
31
  } while (0)
S
Shengliang Guan 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

#define tjsonGetInt32ValueFromDouble(pJson, pName, val, code) \
  do {                                                        \
    double _tmp = 0;                                          \
    code = tjsonGetDoubleValue(pJson, pName, &_tmp);          \
    val = (int32_t)_tmp;                                      \
  } while (0)

#define tjsonGetInt8ValueFromDouble(pJson, pName, val, code) \
  do {                                                       \
    double _tmp = 0;                                         \
    code = tjsonGetDoubleValue(pJson, pName, &_tmp);         \
    val = (int8_t)_tmp;                                      \
  } while (0)

#define tjsonGetUInt16ValueFromDouble(pJson, pName, val, code) \
  do {                                                         \
    double _tmp = 0;                                           \
    code = tjsonGetDoubleValue(pJson, pName, &_tmp);           \
    val = (uint16_t)_tmp;                                      \
  } while (0)
X
Xiaoyu Wang 已提交
53

X
Xiaoyu Wang 已提交
54 55 56
typedef void SJson;

SJson* tjsonCreateObject();
X
Xiaoyu Wang 已提交
57
SJson* tjsonCreateArray();
S
Shengliang Guan 已提交
58
void   tjsonDelete(SJson* pJson);
X
Xiaoyu Wang 已提交
59

S
Shengliang Guan 已提交
60
SJson*  tjsonAddArrayToObject(SJson* pJson, const char* pName);
X
Xiaoyu Wang 已提交
61
int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number);
X
Xiaoyu Wang 已提交
62
int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number);
X
Xiaoyu Wang 已提交
63
int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean);
X
Xiaoyu Wang 已提交
64 65 66 67
int32_t tjsonAddStringToObject(SJson* pJson, const char* pName, const char* pVal);
int32_t tjsonAddItemToObject(SJson* pJson, const char* pName, SJson* pItem);
int32_t tjsonAddItemToArray(SJson* pJson, SJson* pItem);

S
Shengliang Guan 已提交
68
SJson*  tjsonGetObjectItem(const SJson* pJson, const char* pName);
wafwerar's avatar
wafwerar 已提交
69 70
int32_t tjsonGetObjectName(const SJson* pJson, char** pName);
int32_t tjsonGetObjectValueString(const SJson* pJson, char** pStringValue);
X
Xiaoyu Wang 已提交
71 72 73 74 75 76 77
int32_t tjsonGetStringValue(const SJson* pJson, const char* pName, char* pVal);
int32_t tjsonDupStringValue(const SJson* pJson, const char* pName, char** pVal);
int32_t tjsonGetBigIntValue(const SJson* pJson, const char* pName, int64_t* pVal);
int32_t tjsonGetIntValue(const SJson* pJson, const char* pName, int32_t* pVal);
int32_t tjsonGetSmallIntValue(const SJson* pJson, const char* pName, int16_t* pVal);
int32_t tjsonGetTinyIntValue(const SJson* pJson, const char* pName, int8_t* pVal);
int32_t tjsonGetUBigIntValue(const SJson* pJson, const char* pName, uint64_t* pVal);
X
Xiaoyu Wang 已提交
78
int32_t tjsonGetUIntValue(const SJson* pJson, const char* pName, uint32_t* pVal);
X
Xiaoyu Wang 已提交
79 80 81 82 83
int32_t tjsonGetUTinyIntValue(const SJson* pJson, const char* pName, uint8_t* pVal);
int32_t tjsonGetBoolValue(const SJson* pJson, const char* pName, bool* pVal);
int32_t tjsonGetDoubleValue(const SJson* pJson, const char* pName, double* pVal);

int32_t tjsonGetArraySize(const SJson* pJson);
S
Shengliang Guan 已提交
84
SJson*  tjsonGetArrayItem(const SJson* pJson, int32_t index);
X
Xiaoyu Wang 已提交
85

X
Xiaoyu Wang 已提交
86 87 88 89
typedef int32_t (*FToJson)(const void* pObj, SJson* pJson);

int32_t tjsonAddObject(SJson* pJson, const char* pName, FToJson func, const void* pObj);
int32_t tjsonAddItem(SJson* pJson, FToJson func, const void* pObj);
X
Xiaoyu Wang 已提交
90
int32_t tjsonAddArray(SJson* pJson, const char* pName, FToJson func, const void* pArray, int32_t itemSize, int32_t num);
X
Xiaoyu Wang 已提交
91
int32_t tjsonAddTArray(SJson* pJson, const char* pName, FToJson func, const SArray* pArray);
X
Xiaoyu Wang 已提交
92

X
Xiaoyu Wang 已提交
93 94 95
typedef int32_t (*FToObject)(const SJson* pJson, void* pObj);

int32_t tjsonToObject(const SJson* pJson, const char* pName, FToObject func, void* pObj);
X
Xiaoyu Wang 已提交
96
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FToObject func, void** pObj, int32_t objSize);
X
Xiaoyu Wang 已提交
97
int32_t tjsonToArray(const SJson* pJson, const char* pName, FToObject func, void* pArray, int32_t itemSize);
X
Xiaoyu Wang 已提交
98
int32_t tjsonToTArray(const SJson* pJson, const char* pName, FToObject func, SArray** pArray, int32_t itemSize);
X
Xiaoyu Wang 已提交
99 100

char* tjsonToString(const SJson* pJson);
X
Xiaoyu Wang 已提交
101
char* tjsonToUnformattedString(const SJson* pJson);
X
Xiaoyu Wang 已提交
102

X
Xiaoyu Wang 已提交
103 104
SJson*      tjsonParse(const char* pStr);
bool        tjsonValidateJson(const char* pJson);
wafwerar's avatar
wafwerar 已提交
105
const char* tjsonGetError();
X
Xiaoyu Wang 已提交
106

X
Xiaoyu Wang 已提交
107 108 109 110 111
#ifdef __cplusplus
}
#endif

#endif /*_TD_UTIL_JSON_H_*/