httpJson.h 4.0 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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 TDENGINE_HTTP_JSON_H
#define TDENGINE_HTTP_JSON_H

#include <stdint.h>
S
slguan 已提交
20
#include <stdbool.h>
H
hzcheng 已提交
21

S
TD-1451  
Shengliang Guan 已提交
22
#define JSON_BUFFER_SIZE 16384
H
hzcheng 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
struct HttpContext;

enum { JsonNumber, JsonString, JsonBoolean, JsonArray, JsonObject, JsonNull };

extern char JsonItmTkn;
extern char JsonObjStt;
extern char JsonObjEnd;
extern char JsonArrStt;
extern char JsonArrEnd;
extern char JsonStrStt;
extern char JsonStrEnd;
extern char JsonPairTkn;
extern char JsonNulTkn[];
extern char JsonTrueTkn[];
extern char JsonFalseTkn[];

typedef struct {
S
TD-1311  
Shengliang Guan 已提交
40 41 42 43 44
  int32_t size;
  int32_t total;
  char*   lst;
  char    buf[JSON_BUFFER_SIZE];
  struct  HttpContext* pContext;
H
hzcheng 已提交
45 46 47
} JsonBuf;

// http response
S
TD-1311  
Shengliang Guan 已提交
48 49 50
int32_t httpWriteBuf(struct HttpContext* pContext, const char* buf, int32_t sz);
int32_t httpWriteBufNoTrace(struct HttpContext* pContext, const char* buf, int32_t sz);
int32_t httpWriteBufByFd(struct HttpContext* pContext, const char* buf, int32_t sz);
H
hzcheng 已提交
51 52 53 54 55

// builder callback
typedef void (*httpJsonBuilder)(JsonBuf* buf, void* jsnHandle);

// buffer
S
TD-1311  
Shengliang Guan 已提交
56 57 58 59
void    httpInitJsonBuf(JsonBuf* buf, struct HttpContext* pContext);
void    httpWriteJsonBufHead(JsonBuf* buf);
int32_t httpWriteJsonBufBody(JsonBuf* buf, bool isTheLast);
void    httpWriteJsonBufEnd(JsonBuf* buf);
H
hzcheng 已提交
60 61

// value
S
TD-1311  
Shengliang Guan 已提交
62 63 64
void httpJsonString(JsonBuf* buf, char* sVal, int32_t len);
void httpJsonOriginString(JsonBuf* buf, char* sVal, int32_t len);
void httpJsonStringForTransMean(JsonBuf* buf, char* SVal, int32_t maxLen);
H
hzcheng 已提交
65
void httpJsonInt64(JsonBuf* buf, int64_t num);
66
void httpJsonUInt64(JsonBuf* buf, uint64_t num);
67 68
void httpJsonTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision);
void httpJsonUtcTimestamp(JsonBuf* buf, int64_t t, int32_t timePrecision);
S
TD-1311  
Shengliang Guan 已提交
69
void httpJsonInt(JsonBuf* buf, int32_t num);
70
void httpJsonUInt(JsonBuf* buf, uint32_t num);
H
hzcheng 已提交
71 72 73
void httpJsonFloat(JsonBuf* buf, float num);
void httpJsonDouble(JsonBuf* buf, double num);
void httpJsonNull(JsonBuf* buf);
S
TD-1311  
Shengliang Guan 已提交
74
void httpJsonBool(JsonBuf* buf, int32_t val);
H
hzcheng 已提交
75 76

// pair
S
TD-1311  
Shengliang Guan 已提交
77 78 79 80 81 82 83 84 85
void httpJsonPair(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen);
void httpJsonPairOriginString(JsonBuf* buf, char* name, int32_t nameLen, char* sVal, int32_t valLen);
void httpJsonPairHead(JsonBuf* buf, char* name, int32_t len);
void httpJsonPairIntVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num);
void httpJsonPairInt64Val(JsonBuf* buf, char* name, int32_t nNameLen, int64_t num);
void httpJsonPairBoolVal(JsonBuf* buf, char* name, int32_t nNameLen, int32_t num);
void httpJsonPairFloatVal(JsonBuf* buf, char* name, int32_t nNameLen, float num);
void httpJsonPairDoubleVal(JsonBuf* buf, char* name, int32_t nNameLen, double num);
void httpJsonPairNullVal(JsonBuf* buf, char* name, int32_t nNameLen);
H
hzcheng 已提交
86 87

// object
S
TD-1311  
Shengliang Guan 已提交
88 89
void httpJsonPairArray(JsonBuf* buf, char* name, int32_t nLen, httpJsonBuilder builder, void* dsHandle);
void httpJsonPairObject(JsonBuf* buf, char* name, int32_t nLen, httpJsonBuilder builder, void* dsHandle);
H
hzcheng 已提交
90 91 92 93
void httpJsonObject(JsonBuf* buf, httpJsonBuilder fnBuilder, void* dsHandle);
void httpJsonArray(JsonBuf* buf, httpJsonBuilder fnBuidler, void* jsonHandle);

// print
S
TD-1311  
Shengliang Guan 已提交
94
void httpJsonTestBuf(JsonBuf* buf, int32_t safety);
H
hzcheng 已提交
95 96
void httpJsonToken(JsonBuf* buf, char c);
void httpJsonItemToken(JsonBuf* buf);
S
TD-1311  
Shengliang Guan 已提交
97
void httpJsonPrint(JsonBuf* buf, const char* json, int32_t len);
H
hzcheng 已提交
98 99

// quick
S
TD-1311  
Shengliang Guan 已提交
100
void httpJsonPairStatus(JsonBuf* buf, int32_t code);
H
hzcheng 已提交
101

102 103 104 105
// http json printer
JsonBuf* httpMallocJsonBuf(struct HttpContext* pContext);
void     httpFreeJsonBuf(struct HttpContext* pContext);

H
hzcheng 已提交
106
#endif