Navigation Menu

Skip to content

Commit

Permalink
C++-ified pb2json.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethho committed Jan 25, 2013
1 parent fcfe0b8 commit d6ecdbb
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 35 deletions.
45 changes: 28 additions & 17 deletions pb2json.cpp
@@ -1,23 +1,22 @@
#include "pb2json.h"

#include <jansson.h>

#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>

#include <string>

using namespace google::protobuf;
using std::string;
char * pb2json(const Message &msg)
{
json_t *root = parse_msg(&msg);
char *json = json_dumps(root,0);
json_decref(root);
return json; // should be freed by caller
}
char * pb2json( Message *msg,const char *buf ,int len)


namespace /*unnamed*/
{
string s (buf,len);
msg->ParseFromString(s);
json_t *root = parse_msg(msg);
char *json = json_dumps(root,0);
json_decref(root);
return json; // should be freed by caller
}
static json_t *parse_repeated_field(const Message *msg,const Reflection * ref,const FieldDescriptor *field)

json_t *parse_msg(const Message *msg);

json_t *parse_repeated_field(const Message *msg,const Reflection * ref,const FieldDescriptor *field)
{
size_t count = ref->FieldSize(*msg,field);
json_t *arr = json_array();
Expand Down Expand Up @@ -99,7 +98,8 @@ static json_t *parse_repeated_field(const Message *msg,const Reflection * ref,co
}
return arr;
}
static json_t *parse_msg(const Message *msg)

json_t *parse_msg(const Message *msg)
{
const Descriptor *d = msg->GetDescriptor();
if(!d)return NULL;
Expand Down Expand Up @@ -181,5 +181,16 @@ static json_t *parse_msg(const Message *msg)

}
return root;
}

} // namespace /*unnamed*/

std::string pb2json(const google::protobuf::Message& msg)
{
json_t *root = parse_msg(&msg);
char *buffer = json_dumps(root,0);
json_decref(root);
string json(buffer);
free(buffer);
return json;
}
32 changes: 24 additions & 8 deletions pb2json.h
@@ -1,9 +1,25 @@
#include <jansson.h>
#ifndef PB2JSON_H
#define PB2JSON_H

#include <string>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
using namespace google::protobuf;
char *pb2json(const Message &msg);
char *pb2json(Message *msg,const char *buf,int len);
static json_t *parse_msg(const Message *msg);
static json_t *parse_repeated_field(const Message *msg,const Reflection * ref,const FieldDescriptor *field);

// Forward declaration
namespace google
{
namespace protobuf
{
class Message;
} // namespace protobuf
} // namespace google

std::string pb2json(const google::protobuf::Message& msg);

template <typename PbMessageType>
std::string pb2json(const std::string& str)
{
PbMessageType msg;
msg.ParseFromString(str);
return pb2json(msg);
}

#endif // PB2JSON_H
20 changes: 10 additions & 10 deletions test/test_json.cpp
@@ -1,10 +1,12 @@
#include <fstream>
#include <iostream>

#include "person.pb.h"
//#include "../pb2json.h"

#include <pb2json.h>

using namespace std;
using namespace google::protobuf;

int main(int argc,char *argv[])
{
Expand All @@ -13,20 +15,18 @@ int main(int argc,char *argv[])
fin.seekg(0,ios_base::end);
size_t len = fin.tellg();
fin.seekg(0,ios_base::beg);
char *buf = new char [len];

fin.read(buf,len);
Message *p = new Person();
char *json = pb2json(p,buf,len);
string buf(len, 0);
fin.read(&buf[0],len);

string json = pb2json<Person>(buf);
cout<<json<<endl;
free(json);
delete p;

// Test 2: convert PB to JSON directly
Person p2;
char *json2 = pb2json(p2);
Person p;
p.ParseFromString(buf);
string json2 = pb2json(p);
cout<<json2<<endl;
free(json2);

return 0;
}
1 change: 1 addition & 0 deletions test/test_person.cpp
@@ -1,5 +1,6 @@
#include <iostream>
#include <fstream>

#include "person.pb.h"

using namespace std;
Expand Down

0 comments on commit d6ecdbb

Please sign in to comment.