PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/mordor/protobuf.h

http://github.com/mozy/mordor
C Header | 109 lines | 27 code | 15 blank | 67 comment | 0 complexity | 6611e618051e07c4e22989b78df8fb8f MD5 | raw file
  1#ifndef __MORDOR_PROTOBUF_H__
  2#define __MORDOR_PROTOBUF_H__
  3// Copyright (c) 2010 - Mozy, Inc.
  4
  5#include "mordor/json.h"
  6
  7namespace google {
  8namespace protobuf {
  9class Message;
 10}}
 11
 12namespace Mordor {
 13struct Buffer;
 14
 15void serializeToBuffer(const google::protobuf::Message &proto,
 16    Mordor::Buffer &buffer);
 17void parseFromBuffer(google::protobuf::Message &proto,
 18    const Mordor::Buffer &buffer);
 19
 20// reflection utils
 21void serializeToJsonObject(const google::protobuf::Message &proto,
 22    Mordor::JSON::Object &object, bool validate=false, bool includeNull=false);
 23
 24void parseFromJsonObject(google::protobuf::Message *proto,
 25    const Mordor::JSON::Object &object, bool validate=false);
 26
 27google::protobuf::Message* forName(const std::string& typeName);
 28
 29std::string toJson(const google::protobuf::Message &proto, bool validate=false, bool includeNull=false);
 30
 31google::protobuf::Message * fromJson(const Mordor::Buffer &buffer,
 32    bool validate=false);
 33
 34void setField(google::protobuf::Message *proto, const std::string &fieldName,
 35    const Mordor::JSON::Value &value);
 36
 37Mordor::JSON::Value getField(google::protobuf::Message *proto,
 38    const std::string &fieldName);
 39
 40}
 41
 42/* sample code
 43
 44    // test reflection
 45    Message *msg = forName("restpb.PartialObject");
 46    setField(msg, "store_dat.len", 100);
 47    Value val = getField(msg, "store_dat.len");
 48
 49    std::cout << "set/getField store_dat.len = " << val << std::endl;
 50
 51
 52    try {
 53        Buffer json =
 54            "{\n"
 55            "    \"restpb.PartialObject\" : {\n"
 56            "        \"container_id\": 12345,\n"
 57            "        \"filename\": \"/tmp/ahaha.txt\",\n"
 58            "        \"store_dat\": {\n"
 59            "            \"len\": 100,\n"
 60            "            \"m_factor\": 100,\n"
 61            "            \"frag\": [\n"
 62            "                {\n"
 63            "                    \"frag_id\": 1001,\n"
 64            "                    \"tdn_id\": 45,\n"
 65            "                    \"tmp_file\": \"/frag-tmp/ajfajfaf.dat\"\n"
 66            "                },\n"
 67            "                {\n"
 68            "                    \"frag_id\": 1001,\n"
 69            "                    \"tmp_file\": \"/frag-tmp/ajfafafafjfaf.dat\"\n"
 70            "                }\n"
 71            "            ]\n"
 72            "        }\n"
 73            "    }\n"
 74            "}\n";
 75
 76        Message * msg = fromJson(json);
 77        std::cout << "from Json\n" << json.toString() << std::endl << "debug string: " << msg->DebugString() << std::endl;
 78
 79        // set indexed field
 80        setField(msg, "store_dat.frag[1].tdn_id", 46);
 81
 82        std::string output = toJson(*msg);
 83        std::cout << "back to Json\n" << output << std::endl;
 84
 85        std::cout << "store_dat.frag[0].tmp_file = " << getField(msg, "store_dat.frag[0].tmp_file") << std::endl;
 86    } catch (std::runtime_error &e) {
 87        std::cerr << "ERROR: " << e.what() << std::endl;
 88    }
 89
 90    // validating prototype
 91    msg = forName("restpb.StoreDat");
 92    Object obj;
 93    try {
 94        parseFromJsonObject(msg, obj, true);
 95    } catch (std::exception &e) {
 96        std::cerr << "validating error : " << e.what() << std::endl;
 97        obj["m_factor"] = 10;
 98        obj["len"] = 100;
 99        try {
100            parseFromJsonObject(msg, obj, true);
101        } catch (std::exception &e) {
102            std::cerr << "validating error : " << e.what() << std::endl;
103        }
104    }
105}
106*/
107
108#endif
109