Skip to content

Commit

Permalink
Fix files
Browse files Browse the repository at this point in the history
  • Loading branch information
hggq committed Apr 18, 2024
1 parent ea88d0d commit 0cf8049
Show file tree
Hide file tree
Showing 4 changed files with 341 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once
#include <sstream>
#include <string>
#include <vector>
#include <map>

#include "types/techempower_json.h"
#include "unicode.h"

template <typename JSON_REF_OBJ_TEMP>
std::string json_encode([[maybe_unused]] const JSON_REF_OBJ_TEMP &json_reflectobj) { return ""; }

template <typename JSON_REF_OBJ_TEMP>
std::string json_encode([[maybe_unused]] const std::vector<JSON_REF_OBJ_TEMP> &json_reflectobj) { return ""; }

template <typename JSON_REF_OBJ_TEMP>
unsigned int json_decode([[maybe_unused]] JSON_REF_OBJ_TEMP &json_reflectobj, [[maybe_unused]] const std::string &_json_data, [[maybe_unused]] unsigned int _offset) { return 0; }

template <typename JSON_REF_OBJ_TEMP>
unsigned int json_decode([[maybe_unused]] std::vector<JSON_REF_OBJ_TEMP> &json_reflectobj, [[maybe_unused]] const std::string &_json_data, [[maybe_unused]] unsigned int _offset) { return 0; }

namespace http
{

std::string json_encode(const techempower_outjson_t &json_reflectobj);

std::string json_encode(const std::vector<techempower_outjson_t> &json_reflectobj);

unsigned int json_decode(techempower_outjson_t &json_reflectobj, const std::string &_json_data, unsigned int _offset = 0);

unsigned int json_decode(std::vector<techempower_outjson_t> &json_reflectobj, const std::string &_json_data, unsigned int _offset = 0);
}// namespace http
21 changes: 4 additions & 17 deletions frameworks/C++/paozhu/paozhu_benchmark/conf/server.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ cothreadnum=8 ;Coroutines run on thread num
http2_enable=0
debug_enable=1
deamon_enable=0
mainhost=www.869869.com
certificate_chain_file=www.869869.com.pem
private_key_file=www.869869.com.key
mainhost=localhost
certificate_chain_file=localhost.pem
private_key_file=localhost.key
tmp_dh_file=dh4096.pem
reboot_password=e10adc3949ba59abbe56e057f20f883e ;md5(md5("123456")+"rand_char"+md5("123456"))
session_type=1 ;session save type 0.file 1.memory 2.redis 3.memcache 4.reserve
Expand Down Expand Up @@ -38,18 +38,5 @@ method_pre=
method_after=
show_visitinfo=0
upload_max_size=16777216
[www.869869.com]
wwwpath=/root/benchmark/www/default
http2_enable=1
;rewrite_404=1
;rewrite_404_action=index.html|psy/index.html|exam/index.html
;controlsopath=/root/benchmark/docs/controller
static_pre=downloadfileauth|upload
method_pre= ;api/dev/hostcors
method_after=
isuse_php=0
rewrite_php=/root/benchmark/www/thinkphp/public|index.php
fastcgi_host=127.0.0.1
fastcgi_port=9000
upload_max_size=16777216


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef LIBS_TYPES_TECHEMPOWER_TYPE_H
#define LIBS_TYPES_TECHEMPOWER_TYPE_H
#include <string>
#include <sstream>

namespace http
{
//@reflect json to_json from_json
struct techempower_outjson_t
{
std::string message;
};

}// namespace http
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include "types/techempower_json.h"
#include "json_reflect_headers.h"
#include "unicode.h"
#include "func.h"

namespace http
{


std::string json_encode(const techempower_outjson_t &json_reflectobj)
{

std::stringstream _stream;
_stream << "{";
_stream << "\"message\":\"" << http::utf8_to_jsonstring(json_reflectobj.message)<< "\"";

_stream << "}";

return _stream.str();

}


std::string json_encode(const std::vector<techempower_outjson_t> &json_reflectobj)
{
std::stringstream _stream;
_stream << "[";

for(unsigned int i=0;i<json_reflectobj.size();i++)
{
if(i>0)
{
_stream <<",";
}
_stream <<json_encode(json_reflectobj[i]);
}

_stream << "]";

return _stream.str();
}

unsigned int json_decode(techempower_outjson_t &json_reflectobj,const std::string &_json_data,unsigned int _offset)
{
bool _isarray=false;
for(;_offset<_json_data.size();_offset++)
{
if(_json_data[_offset]=='{')
{
break;
}
if(_json_data[_offset]=='[')
{
_isarray=true;
break;
}
}

if(_isarray)
{
for(;_offset<_json_data.size();_offset++)
{
if(_json_data[_offset]=='{')
{
_isarray=false;
break;
}
}
}

if(_isarray==false)
{
if(_json_data[_offset]=='{')
{
_offset++;
}
std::string _json_key_name,_json_value_name;
for(;_offset<_json_data.size();_offset++)
{

//去除空格
_offset=http::json_string_trim(_json_data,_offset);
//如果是右侧括号表示这个对象已经结束
if(_json_data[_offset]==0x7D)
{
return _offset;
}
//直到引号
if(_json_data[_offset]==0x22)
{
unsigned int temp_offset=_offset;
_json_value_name.clear();
_json_key_name=http::jsonstring_to_utf8(&_json_data[_offset],_json_data.size()-_offset,temp_offset);

_offset=temp_offset;
if(_offset < _json_data.size() &&_json_data[_offset]==0x22)
{
_offset+=1;
}
//键名 后就是键值类型 循环去除空格
_offset=http::json_string_trim(_json_data,_offset);
if(_offset < _json_data.size() &&_json_data[_offset]!=':')
{
return _offset;
}
_offset++;
_offset=http::json_string_trim(_json_data,_offset);

if(_offset < _json_data.size() &&_json_data[_offset]=='{')
{ //还是一个对象,表示有嵌套对象
//1 内置 struct map<std::string,*>
//递归代码

_offset++;
for ( ; _offset < _json_data.size(); _offset++)
{
if (_json_data[_offset] == '}')
{
//offset++;
break;
}
if (_json_data[_offset] == '"')
{
_offset++;
for ( ; _offset < _json_data.size(); _offset++)
{
if (_json_data[_offset] == '"'&&_json_data[_offset-1]!=0x5C)
{
break;
}
}
}
}


if(_offset < _json_data.size() && (_json_data[_offset]==']'||_json_data[_offset]=='}'))
{
_offset-=1;
}
//直接下一个,不用处理键值
continue;
}
else if(_json_data[_offset]=='[')
{ //表示有数组
//////////////////////////////////////////////////////////////////////
//begin level1 []
//vector<std::string> vector<std::pair<std::string,*>> vector<vector<int|long|float|double>>
//如果是非内置类型 直接使用json_decode<>

//递归代码


_offset++;
for ( ; _offset < _json_data.size(); _offset++)
{
if (_json_data[_offset] == ']')
{
//offset++;
break;
}
if (_json_data[_offset] == '"')
{
_offset++;
for ( ; _offset < _json_data.size(); _offset++)
{
if (_json_data[_offset] == '"'&&_json_data[_offset-1]!=0x5C)
{
break;
}
}
}
}

//直接下一个,不用处理键值
if(_offset < _json_data.size() && (_json_data[_offset]==']'||_json_data[_offset]=='}'))
{
_offset-=1;
}
continue;
//end level1[]
////////////////////////////////////////////////////////////////////
}
else if(_json_data[_offset]==0x22)
{
//如果键值也是字符串
temp_offset=_offset;
_json_value_name=http::jsonstring_to_utf8(&_json_data[_offset],_json_data.size()-_offset,temp_offset);
_offset=temp_offset;
if(_json_data[_offset]==0x22)
{
if((_offset+1)<_json_data.size())
{
if(_json_data[_offset+1]!=']'&&_json_data[_offset+1]!='}')
{
_offset+=1;
}
}
}
}
else
{
//表示是数字 bool NULL
for(;_offset<_json_data.size();_offset++)
{
//结束条件
if(_json_data[_offset]==','||_json_data[_offset]==']'||_json_data[_offset]=='}'||_json_data[_offset]==0x20||_json_data[_offset]==0x0A||_json_data[_offset]==0x0D||_json_data[_offset]=='\t')
{
break;
}
_json_value_name.push_back(_json_data[_offset]);
}
//让前面循环退出或返回
if(_offset < _json_data.size() && _json_data[_offset]=='}')
{
_offset-=1;
}
}
////////////////////////////////////////////////////////
// level1
//处理对象赋值
if (http::str_casecmp(_json_key_name, "message"))
{

json_reflectobj.message=_json_value_name;
}

////////////////////////////////////////////////////////
//继续循环下一个键值
continue;
}
}
}
return _offset;
}

unsigned int json_decode(std::vector<techempower_outjson_t> &json_reflectobj,const std::string &_json_data,unsigned int _offset)
{
bool _isarray=false;
for(;_offset<_json_data.size();_offset++)
{
if(_json_data[_offset]=='{')
{
break;
}
if(_json_data[_offset]=='[')
{
_isarray=true;
break;
}
}

if(_isarray)
{
if(_json_data[_offset]=='[')
{
_offset+=1;
}
for(;_offset<_json_data.size();_offset++)
{
_offset=http::json_string_trim(_json_data,_offset);
//直接返回,这样可以防插入空的对象
if(_json_data[_offset]==0x5D)
{
return _offset;
}else if(_json_data[_offset]=='{')
{
techempower_outjson_t temp;
_offset=json_decode(temp,_json_data,_offset);
json_reflectobj.push_back(temp);
}

}

}
else
{
techempower_outjson_t temp;
_offset=json_decode(temp,_json_data,_offset);
json_reflectobj.push_back(temp);

}

return _offset;
}

}

0 comments on commit 0cf8049

Please sign in to comment.