RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
common/json_holder.h
00001 /* json_holder.h                                                   -*- C++ -*-
00002    Jeremy Barnes, 1 June 2012
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Container to allow JSON to be accessed, both as a string and as a
00006    structured object if required.
00007 */
00008 
00009 #pragma once
00010 
00011 #include "soa/jsoncpp/json.h"
00012 #include "jml/db/persistent_fwd.h"
00013 #include "jml/utils/unnamed_bool.h"
00014 #include <memory>
00015 
00016 
00017 namespace RTBKIT {
00018 
00019 
00020 /*****************************************************************************/
00021 /* JSON HOLDER                                                               */
00022 /*****************************************************************************/
00023 
00028 struct JsonHolder {
00029     JsonHolder()
00030     {
00031     }
00032 
00033     JsonHolder(Json::Value && val)
00034         : parsed(new Json::Value(val))
00035     {
00036     }
00037 
00038     JsonHolder(const Json::Value & val)
00039         : parsed(new Json::Value(val))
00040     {
00041     }
00042 
00043     JsonHolder(const std::string & str)
00044         : str()
00045     {
00046     }
00047 
00048     ~JsonHolder()
00049     {
00050     }
00051 
00052     JsonHolder & operator = (Json::Value && val)
00053     {
00054         clear();
00055         parsed.reset(new Json::Value(val));
00056         return *this;
00057     }
00058 
00059     JsonHolder & operator = (const Json::Value & val)
00060     {
00061         clear();
00062         parsed.reset(new Json::Value(val));
00063         return *this;
00064     }
00065 
00066     JsonHolder & operator = (const std::string & val)
00067     {
00068         clear();
00069         str = val;
00070         return *this;
00071     }
00072 
00073     void clear()
00074     {
00075         str.clear();
00076         parsed.reset();
00077     }
00078 
00079     static const std::string nullStr;
00080     
00081     const std::string & toString() const
00082     {
00083         if (!parsed) {
00084             if (str.empty()) return nullStr;
00085             return str;
00086         }
00087         if (parsed->isNull()) return nullStr;
00088         makeString();
00089         return str;
00090     }
00091 
00092     const Json::Value & toJson() const
00093     {
00094         if (parsed) return *parsed;
00095         makeJson();
00096         return *parsed;
00097     }
00098 
00099     void serialize(ML::DB::Store_Writer & store) const;
00100     void reconstitute(ML::DB::Store_Reader & store);
00101     
00102     bool isNonNull() const
00103     {
00104         if (parsed) return !parsed->isNull();
00105         return !str.empty() && str != "null";
00106     }
00107 
00108 #if 0
00109     bool valid() const
00110     {
00111         return parsed || !str.empty();
00112     }
00113 
00114     JML_IMPLEMENT_OPERATOR_BOOL(valid());
00115 #endif
00116 
00117 private:    
00118     void makeString() const;
00119     void makeJson() const;
00120 
00121     mutable std::string str;
00122     mutable std::shared_ptr<const Json::Value> parsed;
00123 };
00124 
00125 IMPL_SERIALIZE_RECONSTITUTE(JsonHolder);
00126 
00127 std::ostream & operator << (std::ostream & stream, const JsonHolder & json);
00128 
00129 } // namespace RTBKIT
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator