tjson.h 3.5 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

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

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

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

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

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

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

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

X
Xiaoyu Wang 已提交
78
SJson* tjsonParse(const char* pStr);
79
bool   tjsonIsObject(const SJson* pJson);
X
Xiaoyu Wang 已提交
80

X
Xiaoyu Wang 已提交
81 82 83 84 85
#ifdef __cplusplus
}
#endif

#endif /*_TD_UTIL_JSON_H_*/