Skip to content

Commit 884e02f

Browse files
committed
remove sstream
1 parent 9312017 commit 884e02f

File tree

3 files changed

+78
-9
lines changed

3 files changed

+78
-9
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@
66
.project
77
*.swp
88
*.exe
9+
*.txt
10+
*.log
11+
*.json
12+
CJsonObjectTest

CJsonObject.cpp

+48-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
******************************************************************************/
1010

1111
#include "CJsonObject.hpp"
12-
#include <sstream>
1312

1413
#ifdef _WIN32
1514
#define snprintf _snprintf_s
@@ -308,9 +307,30 @@ std::string CJsonObject::operator()(const std::string& strKey) const
308307
}
309308
else if (pJsonStruct->type == cJSON_Int)
310309
{
311-
std::ostringstream ossNumber;
312-
ossNumber << pJsonStruct->valueint;
313-
return(ossNumber.str());
310+
char szNumber[128] = {0};
311+
if (pJsonStruct->sign == -1)
312+
{
313+
if (pJsonStruct->valueint <= (int64)INT_MAX && (int64)pJsonStruct->valueint >= (int64)INT_MIN)
314+
{
315+
snprintf(szNumber, sizeof(szNumber), "%d", (int32)pJsonStruct->valueint);
316+
}
317+
else
318+
{
319+
snprintf(szNumber, sizeof(szNumber), "%ld", (int64)pJsonStruct->valueint);
320+
}
321+
}
322+
else
323+
{
324+
if ((uint64)pJsonStruct->valueint <= (uint64)UINT_MAX)
325+
{
326+
snprintf(szNumber, sizeof(szNumber), "%u", (uint32)pJsonStruct->valueint);
327+
}
328+
else
329+
{
330+
snprintf(szNumber, sizeof(szNumber), "%lu", pJsonStruct->valueint);
331+
}
332+
}
333+
return(std::string(szNumber));
314334
}
315335
else if (pJsonStruct->type == cJSON_Double)
316336
{
@@ -363,9 +383,30 @@ std::string CJsonObject::operator()(unsigned int uiWhich) const
363383
}
364384
else if (pJsonStruct->type == cJSON_Int)
365385
{
366-
std::ostringstream ossNumber;
367-
ossNumber << pJsonStruct->valueint;
368-
return(ossNumber.str());
386+
char szNumber[128] = {0};
387+
if (pJsonStruct->sign == -1)
388+
{
389+
if (pJsonStruct->valueint <= (int64)INT_MAX && (int64)pJsonStruct->valueint >= (int64)INT_MIN)
390+
{
391+
snprintf(szNumber, sizeof(szNumber), "%d", (int32)pJsonStruct->valueint);
392+
}
393+
else
394+
{
395+
snprintf(szNumber, sizeof(szNumber), "%ld", (int64)pJsonStruct->valueint);
396+
}
397+
}
398+
else
399+
{
400+
if ((uint64)pJsonStruct->valueint <= (uint64)UINT_MAX)
401+
{
402+
snprintf(szNumber, sizeof(szNumber), "%u", (uint32)pJsonStruct->valueint);
403+
}
404+
else
405+
{
406+
snprintf(szNumber, sizeof(szNumber), "%lu", pJsonStruct->valueint);
407+
}
408+
}
409+
return(std::string(szNumber));
369410
}
370411
else if (pJsonStruct->type == cJSON_Double)
371412
{

demo/demo.cpp

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
#include <string>
22
#include <iostream>
3+
#include <fstream>
4+
#include <sstream>
35
#include "../CJsonObject.hpp"
46

5-
int main()
7+
//int main()
8+
int main(int argc, char* argv[])
69
{
10+
std::ifstream fin(argv[1]);
11+
if (fin.good())
12+
{
13+
neb::CJsonObject oJson;
14+
std::stringstream ssContent;
15+
ssContent << fin.rdbuf();
16+
if (oJson.Parse(ssContent.str()))
17+
{
18+
std::cout << oJson.ToString() << std::endl;
19+
}
20+
else
21+
{
22+
std::cerr << "parse json error" << "\n";// << ssContent.str() << std::endl;
23+
}
24+
fin.close();
25+
}
726
int iValue;
827
double fTimeout;
928
std::string strValue;
1029
neb::CJsonObject oJson("{\"refresh_interval\":60,"
1130
"\"test_float\":[18.0, 10.0, 5.0],"
31+
"\"test_int\":[135355, -1844674407370955161, -935375],"
1232
"\"timeout\":12.5,"
1333
"\"dynamic_loading\":["
1434
"{"
@@ -83,7 +103,11 @@ int main()
83103
for (int i = 0; i < oJson["test_float"].GetArraySize(); ++i)
84104
{
85105
oJson["test_float"].Get(i, fTestValue);
86-
std::cout << fTestValue << std::endl;
106+
std::cout << fTestValue << "\t in string: " << oJson["test_float"](i) << std::endl;
107+
}
108+
for (int i = 0; i < oJson["test_int"].GetArraySize(); ++i)
109+
{
110+
std::cout << "in string: " << oJson["test_int"](i) << std::endl;
87111
}
88112
oJson.AddNull("null_value");
89113
std::cout << oJson.IsNull("test_float") << "\t" << oJson.IsNull("null_value") << std::endl;

0 commit comments

Comments
 (0)