![]() |
RTBKit
0.9
Open-source framework to create real-time ad bidding systems.
|
00001 /* json_holder.cc -*- C++ -*- 00002 Jeremy Barnes, 1 June 2012 00003 Copyright (c) 2012 Datacratic. All rights reserved. 00004 00005 Hold JSON in its natural format. 00006 */ 00007 00008 #include "rtbkit/common/json_holder.h" 00009 #include <boost/algorithm/string.hpp> 00010 #include "jml/db/persistent.h" 00011 00012 using namespace std; 00013 using namespace ML; 00014 00015 00016 namespace RTBKIT { 00017 00018 00019 void 00020 JsonHolder:: 00021 serialize(ML::DB::Store_Writer & store) const 00022 { 00023 unsigned char version = 0; 00024 makeString(); 00025 store << version << str; 00026 } 00027 00028 void 00029 JsonHolder:: 00030 reconstitute(ML::DB::Store_Reader & store) 00031 { 00032 unsigned char version; 00033 store >> version; 00034 if (version != 0) 00035 throw ML::Exception("invalid JsonHolder serialization version"); 00036 string s; 00037 store >> s; 00038 *this = s; 00039 } 00040 00041 void 00042 JsonHolder:: 00043 makeString() const 00044 { 00045 if (!str.empty()) return; 00046 if (!parsed) return; 00047 if (parsed->isNull()) return; 00048 str = parsed->toString(); 00049 boost::trim(str); 00050 } 00051 00052 void 00053 JsonHolder:: 00054 makeJson() const 00055 { 00056 if (parsed) return; 00057 if (str.empty()) 00058 parsed.reset(new Json::Value()); 00059 else parsed.reset(new Json::Value(Json::parse(str))); 00060 } 00061 00062 const std::string JsonHolder::nullStr("null"); 00063 00064 std::ostream & operator << (std::ostream & stream, const JsonHolder & json) 00065 { 00066 return stream << json.toString(); 00067 } 00068 00069 } // namespace RTBKIT