Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why #348

Closed
hushanjiang opened this issue Aug 10, 2017 · 1 comment
Closed

why #348

hushanjiang opened this issue Aug 10, 2017 · 1 comment

Comments

@hushanjiang
Copy link

No description provided.

@hushanjiang
Copy link
Author

there is the code:
#include
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

#include "json-c/json.h"

void* parse_json(void *p)
{
printf("start parse\n");
std::string req = "{"method": "fm_get_member_info", "msg_tag":"xx", "msg_uuid": "uuid", "msg_encry": "true", "session_id":"session_id", "req_id": 123, "timestamp":123456890, "real_id":2003, "params": {"family_id":2013, "target_user_id": 2005} }";

for(int i=0; i<= 10000; i++)
{
    enum json_tokener_error jerr;
    json_object *root = json_tokener_parse_verbose(req.c_str(), &jerr);
    if(jerr != json_tokener_success)
    {
        printf("invalid json1\n");
        return NULL;
    }

    json_object *params = NULL;
    if(json_object_object_get_ex(root, "params", &params)) 
    { 
        if(!json_object_is_type(params, json_type_object)) 
        { 
            printf("invalid json2\n");
            json_object_put(root);
            return NULL; 
        } 
    }
    std::string result = json_object_to_json_string(params);
    if(result.size() != 45)
    {
        printf("length: %u, data: %s\n", result.size(), result.c_str());
    }
    
    json_object_put(root);	

    usleep(1);
}

}

int main (int argc, char **argv)
{
int i,ret;
for(int i=0; i<5; i++)
{
pthread_t id;
ret=pthread_create(&id,NULL,parse_json,NULL);
if(ret!=0)
{
printf("Create pthread error!\n");
return -1;
}
}

sleep(5);
return 0;

}

make flag:
g++ -DLINUX -W -g -I./ -I../ -I../../../../../3rd_party/sdk/json-c/include/ -L../../../../../3rd_party/sdk/json-c/lib/ -ljson-c -o test-json test-json.cpp

result:
start parse
start parse
start parse
start parse
start parse
length: 41, data: { "family_id": 2013, "target_user_id": }
length: 41, data: { "family_id": , "target_user_id": 2005 }
length: 41, data: { "family_id": , "target_user_id": 2005 }
length: 15, data: { "family_id":
length: 39, data: { "family_id": 2013, "target_user_id":

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant