tjson.h 3.8 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 20
#include "os.h"

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

25 26 27 28 29 30
#define tjsonGetNumberValue(pJson, pName, val, code)  \
  do {                                                \
    uint64_t _tmp = 0;                                \
    code = tjsonGetUBigIntValue(pJson, pName, &_tmp); \
    val = _tmp;                                       \
  } while (0)
X
Xiaoyu Wang 已提交
31

X
Xiaoyu Wang 已提交
32 33 34
typedef void SJson;

SJson* tjsonCreateObject();
X
Xiaoyu Wang 已提交
35
SJson* tjsonCreateArray();
S
Shengliang Guan 已提交
36
void   tjsonDelete(SJson* pJson);
X
Xiaoyu Wang 已提交
37

S
Shengliang Guan 已提交
38
SJson*  tjsonAddArrayToObject(SJson* pJson, const char* pName);
X
Xiaoyu Wang 已提交
39
int32_t tjsonAddIntegerToObject(SJson* pJson, const char* pName, const uint64_t number);
X
Xiaoyu Wang 已提交
40
int32_t tjsonAddDoubleToObject(SJson* pJson, const char* pName, const double number);
X
Xiaoyu Wang 已提交
41
int32_t tjsonAddBoolToObject(SJson* pJson, const char* pName, const bool boolean);
X
Xiaoyu Wang 已提交
42 43 44 45
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 已提交
46
SJson*  tjsonGetObjectItem(const SJson* pJson, const char* pName);
wafwerar's avatar
wafwerar 已提交
47 48
int32_t tjsonGetObjectName(const SJson* pJson, char** pName);
int32_t tjsonGetObjectValueString(const SJson* pJson, char** pStringValue);
X
Xiaoyu Wang 已提交
49 50 51 52 53 54 55
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 已提交
56
int32_t tjsonGetUIntValue(const SJson* pJson, const char* pName, uint32_t* pVal);
X
Xiaoyu Wang 已提交
57 58 59 60 61
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 已提交
62
SJson*  tjsonGetArrayItem(const SJson* pJson, int32_t index);
X
Xiaoyu Wang 已提交
63

X
Xiaoyu Wang 已提交
64 65 66 67
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 已提交
68
int32_t tjsonAddArray(SJson* pJson, const char* pName, FToJson func, const void* pArray, int32_t itemSize, int32_t num);
X
Xiaoyu Wang 已提交
69

X
Xiaoyu Wang 已提交
70 71 72
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 已提交
73
int32_t tjsonMakeObject(const SJson* pJson, const char* pName, FToObject func, void** pObj, int32_t objSize);
X
Xiaoyu Wang 已提交
74
int32_t tjsonToArray(const SJson* pJson, const char* pName, FToObject func, void* pArray, int32_t itemSize);
X
Xiaoyu Wang 已提交
75 76

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

X
Xiaoyu Wang 已提交
79
SJson* tjsonParse(const char* pStr);
wmmhello's avatar
wmmhello 已提交
80
bool   tjsonValidateJson(const char* pJson);
wafwerar's avatar
wafwerar 已提交
81
const char* tjsonGetError();
X
Xiaoyu Wang 已提交
82

X
Xiaoyu Wang 已提交
83 84 85 86 87
#ifdef __cplusplus
}
#endif

#endif /*_TD_UTIL_JSON_H_*/