RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
openrtb/openrtb_parsing.h
00001 /* openrtb_parsing.h                                               -*- C++ -*-
00002    Jeremy Barnes, 22 February 2013
00003    Copyright (c) 2013 Datacratic Inc.  All rights reserved.
00004 
00005    Code to parse OpenRTB bid requests.
00006 */
00007 
00008 #pragma once
00009 
00010 #include "soa/types/value_description.h"
00011 #include "soa/types/basic_value_descriptions.h"
00012 #include "soa/types/json_parsing.h"
00013 #include "openrtb.h"
00014 #include <boost/lexical_cast.hpp>
00015 
00016 namespace Datacratic {
00017 
00018 template<>
00019 struct DefaultDescription<OpenRTB::TaggedBool>
00020     : public ValueDescriptionI<OpenRTB::TaggedBool, ValueKind::BOOLEAN> {
00021   
00022     virtual void parseJsonTyped(OpenRTB::TaggedBool * val,
00023                                 JsonParsingContext & context) const
00024     {
00025         if (context.isBool())
00026             val->val = context.expectBool();
00027         else val->val = context.expectInt();
00028     }
00029 
00030     virtual void printJsonTyped(const OpenRTB::TaggedBool * val,
00031                                 JsonPrintingContext & context) const
00032     {
00033         context.writeInt(val->val);
00034     }
00035 
00036     virtual bool isDefaultTyped(const OpenRTB::TaggedBool * val)
00037         const
00038     {
00039         return val->val == -1;
00040     }
00041 };
00042 
00043 template<int defValue>
00044 struct DefaultDescription<OpenRTB::TaggedBoolDef<defValue> >
00045     : public ValueDescriptionI<OpenRTB::TaggedBoolDef<defValue>,
00046                                ValueKind::BOOLEAN> {
00047   
00048     virtual void parseJsonTyped(OpenRTB::TaggedBoolDef<defValue> * val,
00049                                 JsonParsingContext & context) const
00050     {
00051         if (context.isBool())
00052             val->val = context.expectBool();
00053         else val->val = context.expectInt();
00054     }
00055 
00056     virtual void printJsonTyped(const OpenRTB::TaggedBoolDef<defValue> * val,
00057                                 JsonPrintingContext & context) const
00058     {
00059         context.writeInt(val->val);
00060     }
00061 
00062     virtual bool isDefaultTyped(const OpenRTB::TaggedBoolDef<defValue> * val)
00063         const
00064     {
00065         return val->val == defValue;
00066     }
00067 };
00068 
00069 template<>
00070 struct DefaultDescription<OpenRTB::TaggedInt>
00071     : public ValueDescriptionI<OpenRTB::TaggedInt,
00072                                ValueKind::INTEGER,
00073                                DefaultDescription<OpenRTB::TaggedInt> > {
00074 
00075     virtual void parseJsonTyped(OpenRTB::TaggedInt * val,
00076                                 JsonParsingContext & context) const
00077     {
00078         if (context.isString()) {
00079             std::string s = context.expectStringAscii();
00080             val->val = boost::lexical_cast<int>(s);
00081         }
00082         else val->val = context.expectInt();
00083     }
00084 
00085     virtual void printJsonTyped(const OpenRTB::TaggedInt * val,
00086                                 JsonPrintingContext & context) const
00087     {
00088         context.writeInt(val->val);
00089     }
00090 
00091     virtual bool isDefaultTyped(const OpenRTB::TaggedInt * val)
00092         const
00093     {
00094         return val->val == -1;
00095     }
00096 };
00097 
00098 template<int defValue>
00099 struct DefaultDescription<OpenRTB::TaggedIntDef<defValue> >
00100     : public ValueDescriptionI<OpenRTB::TaggedIntDef<defValue>,
00101                                ValueKind::INTEGER> {
00102 
00103     virtual void parseJsonTyped(OpenRTB::TaggedIntDef<defValue> * val,
00104                                 JsonParsingContext & context) const
00105     {
00106         if (context.isString()) {
00107             std::string s = context.expectStringAscii();
00108             val->val = boost::lexical_cast<int>(s);
00109         }
00110         else val->val = context.expectInt();
00111     }
00112 
00113     virtual void printJsonTyped(const OpenRTB::TaggedIntDef<defValue> * val,
00114                                 JsonPrintingContext & context) const
00115     {
00116         context.writeInt(val->val);
00117     }
00118 
00119     virtual bool isDefaultTyped(const OpenRTB::TaggedIntDef<defValue> * val)
00120         const
00121     {
00122         return val->val == defValue;
00123     }
00124 };
00125 
00126 template<>
00127 struct DefaultDescription<OpenRTB::TaggedFloat>
00128     : public ValueDescriptionI<OpenRTB::TaggedFloat,
00129                                ValueKind::FLOAT> {
00130 
00131     virtual void parseJsonTyped(OpenRTB::TaggedFloat * val,
00132                                 JsonParsingContext & context) const
00133     {
00134         val->val = context.expectFloat();
00135     }
00136 
00137     virtual void printJsonTyped(const OpenRTB::TaggedFloat * val,
00138                                 JsonPrintingContext & context) const
00139     {
00140         context.writeDouble(val->val);
00141     }
00142 
00143     virtual bool isDefaultTyped(const OpenRTB::TaggedFloat * val) const
00144     {
00145         return isnan(val->val);
00146     }
00147 };
00148 
00149 template<int num, int den>
00150 struct DefaultDescription<OpenRTB::TaggedFloatDef<num, den> >
00151     : public ValueDescriptionI<OpenRTB::TaggedFloatDef<num, den>,
00152                                ValueKind::FLOAT> {
00153 
00154     virtual void parseJsonTyped(OpenRTB::TaggedFloatDef<num, den> * val,
00155                                 JsonParsingContext & context) const
00156     {
00157         val->val = context.expectFloat();
00158     }
00159 
00160     virtual void printJsonTyped(const OpenRTB::TaggedFloatDef<num, den> * val,
00161                                 JsonPrintingContext & context) const
00162     {
00163         context.writeFloat(val->val);
00164     }
00165 
00166     virtual bool isDefaultTyped(const OpenRTB::TaggedFloatDef<num, den> * val) const
00167     {
00168         return val->val == (float)num / den;
00169     }
00170 };
00171 
00172 template<class Enum>
00173 struct TaggedEnumDescription
00174     : public ValueDescriptionI<Enum, ValueKind::ENUM,
00175                                TaggedEnumDescription<Enum> > {
00176 
00177     virtual void parseJsonTyped(Enum * val,
00178                                 JsonParsingContext & context) const
00179     {
00180         int index = context.expectInt();
00181         val->val = index;
00182     }
00183 
00184     virtual void printJsonTyped(const Enum * val,
00185                                 JsonPrintingContext & context) const
00186     {
00187         context.writeInt(val->val);
00188     }
00189 
00190     virtual bool isDefaultTyped(const Enum * val) const
00191     {
00192         return val->val == -1;
00193     }
00194 };
00195 
00199 struct FormatListDescription
00200     : public ValueDescriptionI<OpenRTB::List<int> >,
00201       public ListDescriptionBase<int> {
00202 
00203     virtual void parseJsonTyped(OpenRTB::List<int> * val,
00204                                 JsonParsingContext & context) const
00205     {
00206         if (context.isArray()) {
00207             auto onElement = [&] ()
00208                 {
00209                     val->push_back(context.expectInt());
00210                 };
00211             context.forEachElement(onElement);
00212         }
00213         else {
00214             val->push_back(context.expectInt());
00215         }
00216     }
00217 
00218     virtual void printJsonTyped(const OpenRTB::List<int> * val,
00219                                 JsonPrintingContext & context) const
00220     {
00221         if (val->size() == 1) {
00222             this->inner->printJsonTyped(&(*val)[0], context);
00223         }
00224         else
00225             printJsonTypedList(val, context);
00226     }
00227 
00228     virtual bool isDefaultTyped(const OpenRTB::List<int> * val) const
00229     {
00230         return val->empty();
00231     }
00232 
00233 };
00234 
00235 struct CommaSeparatedListDescription
00236     : public ValueDescriptionI<std::string, ValueKind::STRING> {
00237 
00238     virtual void parseJsonTyped(std::string * val,
00239                                 JsonParsingContext & context) const
00240     {
00241         if (context.isArray()) {
00242             std::string res;
00243             auto onElement = [&] ()
00244                 {
00245                     std::string s = context.expectStringAscii();
00246                     if (!res.empty())
00247                         res += ", ";
00248                     res += s;
00249                         
00250                 };
00251             context.forEachElement(onElement);
00252             *val = res;
00253         }
00254         else {
00255             *val = context.expectStringAscii();
00256         }
00257     }
00258 
00259     virtual void printJsonTyped(const std::string * val,
00260                                 JsonPrintingContext & context) const
00261     {
00262         context.writeString(*val);
00263     }
00264 
00265     virtual bool isDefaultTyped(const std::string * val) const
00266     {
00267         return val->empty();
00268     }
00269 
00270 };
00271 
00272 template<typename T>
00273 struct DefaultDescription<OpenRTB::Optional<T> >
00274     : public ValueDescriptionI<OpenRTB::Optional<T>, ValueKind::OPTIONAL> {
00275 
00276     DefaultDescription(ValueDescriptionT<T> * inner
00277                        = getDefaultDescription((T *)0))
00278         : inner(inner)
00279     {
00280     }
00281 
00282     std::unique_ptr<ValueDescriptionT<T> > inner;
00283 
00284     virtual void parseJsonTyped(OpenRTB::Optional<T> * val,
00285                                 JsonParsingContext & context) const
00286     {
00287         if (context.isNull()) {
00288             context.expectNull();
00289             val->reset();
00290         }
00291         val->reset(new T());
00292         inner->parseJsonTyped(val->get(), context);
00293     }
00294 
00295     virtual void printJsonTyped(const OpenRTB::Optional<T> * val,
00296                                 JsonPrintingContext & context) const
00297     {
00298         if (!val->get())
00299             context.skip();
00300         else inner->printJsonTyped(val->get(), context);
00301     }
00302 
00303     virtual bool isDefaultTyped(const OpenRTB::Optional<T> * val) const
00304     {
00305         return !val->get();
00306     }
00307 
00308     virtual void * optionalMakeValueTyped(OpenRTB::Optional<T> * val) const
00309     {
00310         if (!val->get())
00311             val->reset(new T());
00312         return val->get();
00313     }
00314 
00315     virtual const void * optionalGetValueTyped(const OpenRTB::Optional<T> * val) const
00316     {
00317         if (!val->get())
00318             throw ML::Exception("no value in optional field");
00319         return val->get();
00320     }
00321 
00322     virtual const ValueDescription & contained() const
00323     {
00324         return *inner;
00325     }
00326 };
00327 
00328 template<typename T>
00329 struct DefaultDescription<OpenRTB::List<T> >
00330     : public ValueDescriptionI<OpenRTB::List<T>, ValueKind::ARRAY>,
00331       public ListDescriptionBase<T> {
00332 
00333     virtual void parseJsonTyped(OpenRTB::List<T> * val,
00334                                 JsonParsingContext & context) const
00335     {
00336         this->parseJsonTypedList(val, context);
00337     }
00338 
00339     virtual void printJsonTyped(const OpenRTB::List<T> * val,
00340                                 JsonPrintingContext & context) const
00341     {
00342         this->printJsonTypedList(val, context);
00343     }
00344 
00345     virtual bool isDefaultTyped(const OpenRTB::List<T> * val) const
00346     {
00347         return val->empty();
00348     }
00349 
00350     virtual size_t getArrayLength(void * val) const
00351     {
00352         const OpenRTB::List<T> * val2 = reinterpret_cast<const OpenRTB::List<T> *>(val);
00353         return val2->size();
00354     }
00355 
00356     virtual void * getArrayElement(void * val, uint32_t element) const
00357     {
00358         OpenRTB::List<T> * val2 = reinterpret_cast<OpenRTB::List<T> *>(val);
00359         return &val2->at(element);
00360     }
00361 
00362     virtual const void * getArrayElement(const void * val, uint32_t element) const
00363     {
00364         const OpenRTB::List<T> * val2 = reinterpret_cast<const OpenRTB::List<T> *>(val);
00365         return &val2->at(element);
00366     }
00367 
00368     virtual void setArrayLength(void * val, size_t newLength) const
00369     {
00370         OpenRTB::List<T> * val2 = reinterpret_cast<OpenRTB::List<T> *>(val);
00371         val2->resize(newLength);
00372     }
00373     
00374     virtual const ValueDescription & contained() const
00375     {
00376         return *this->inner;
00377     }
00378 
00379 };
00380 
00381 template<>
00382 struct DefaultDescription<OpenRTB::ContentCategory>
00383     : public ValueDescriptionI<OpenRTB::ContentCategory> {
00384 
00385     DefaultDescription()
00386     {
00387     }
00388 
00389     virtual void parseJsonTyped(OpenRTB::ContentCategory * val,
00390                                 JsonParsingContext & context) const
00391     {
00392         val->val = context.expectStringAscii();
00393     }
00394 
00395     virtual void printJsonTyped(const OpenRTB::ContentCategory * val,
00396                                 JsonPrintingContext & context) const
00397     {
00398         context.writeString(val->val);
00399     }
00400 };
00401 
00402 template<>
00403 struct DefaultDescription<OpenRTB::MimeType>
00404     : public ValueDescriptionI<OpenRTB::MimeType, ValueKind::STRING> {
00405 
00406     virtual void parseJsonTyped(OpenRTB::MimeType * val,
00407                                 JsonParsingContext & context) const
00408     {
00409         val->type = context.expectStringAscii();
00410     }
00411 
00412     virtual void printJsonTyped(const OpenRTB::MimeType * val,
00413                                 JsonPrintingContext & context) const
00414     {
00415         context.writeString(val->type);
00416     }
00417 };
00418 
00419 template<>
00420 struct DefaultDescription<OpenRTB::VideoQuality>
00421     : public TaggedEnumDescription<OpenRTB::VideoQuality> {
00422     DefaultDescription()
00423     {
00424     }
00425 };
00426 
00427 template<>
00428 struct DefaultDescription<OpenRTB::AuctionType>
00429     : public TaggedEnumDescription<OpenRTB::AuctionType> {
00430 
00431     DefaultDescription()
00432     {
00433     }
00434 };
00435 
00436 template<>
00437 struct DefaultDescription<OpenRTB::BannerAdType>
00438     : public TaggedEnumDescription<OpenRTB::BannerAdType> {
00439 
00440     DefaultDescription()
00441     {
00442     }
00443 };
00444 
00445 template<>
00446 struct DefaultDescription<OpenRTB::CreativeAttribute>
00447     : public TaggedEnumDescription<OpenRTB::CreativeAttribute> {
00448 
00449     DefaultDescription()
00450     {
00451     }
00452 };
00453 
00454 template<>
00455 struct DefaultDescription<OpenRTB::ApiFramework>
00456     : public TaggedEnumDescription<OpenRTB::ApiFramework> {
00457 
00458     DefaultDescription()
00459     {
00460     }
00461 };
00462 
00463 template<>
00464 struct DefaultDescription<OpenRTB::VideoLinearity>
00465     : public TaggedEnumDescription<OpenRTB::VideoLinearity> {
00466 
00467     DefaultDescription()
00468     {
00469     }
00470 };
00471 
00472 template<>
00473 struct DefaultDescription<OpenRTB::VideoBidResponseProtocol>
00474     : public TaggedEnumDescription<OpenRTB::VideoBidResponseProtocol> {
00475 
00476     DefaultDescription()
00477     {
00478     }
00479 };
00480 
00481 template<>
00482 struct DefaultDescription<OpenRTB::VideoPlaybackMethod>
00483     : public TaggedEnumDescription<OpenRTB::VideoPlaybackMethod> {
00484 
00485     DefaultDescription()
00486     {
00487     }
00488 };
00489 
00490 template<>
00491 struct DefaultDescription<OpenRTB::VideoStartDelay>
00492     : public TaggedEnumDescription<OpenRTB::VideoStartDelay> {
00493 
00494     DefaultDescription()
00495     {
00496     }
00497 };
00498 
00499 template<>
00500 struct DefaultDescription<OpenRTB::ConnectionType>
00501     : public TaggedEnumDescription<OpenRTB::ConnectionType> {
00502 
00503     DefaultDescription()
00504     {
00505     }
00506 };
00507 
00508 template<>
00509 struct DefaultDescription<OpenRTB::ExpandableDirection>
00510     : public TaggedEnumDescription<OpenRTB::ExpandableDirection> {
00511 
00512     DefaultDescription()
00513     {
00514     }
00515 };
00516 
00517 template<>
00518 struct DefaultDescription<OpenRTB::ContentDeliveryMethod>
00519     : public TaggedEnumDescription<OpenRTB::ContentDeliveryMethod> {
00520 
00521     DefaultDescription()
00522     {
00523     }
00524 };
00525 
00526 template<>
00527 struct DefaultDescription<OpenRTB::ContentContext>
00528     : public TaggedEnumDescription<OpenRTB::ContentContext> {
00529 
00530     DefaultDescription()
00531     {
00532     }
00533 };
00534 
00535 template<>
00536 struct DefaultDescription<OpenRTB::LocationType>
00537     : public TaggedEnumDescription<OpenRTB::LocationType> {
00538 
00539     DefaultDescription()
00540     {
00541     }
00542 };
00543 
00544 
00545 template<>
00546 struct DefaultDescription<OpenRTB::DeviceType>
00547     : public TaggedEnumDescription<OpenRTB::DeviceType> {
00548 
00549     DefaultDescription()
00550     {
00551     }
00552 };
00553 
00554 template<>
00555 struct DefaultDescription<OpenRTB::VastCompanionType>
00556     : public TaggedEnumDescription<OpenRTB::VastCompanionType> {
00557 
00558     DefaultDescription()
00559     {
00560     }
00561 };
00562 
00563 template<>
00564 struct DefaultDescription<OpenRTB::MediaRating>
00565     : public TaggedEnumDescription<OpenRTB::MediaRating> {
00566 
00567     DefaultDescription()
00568     {
00569     }
00570 };
00571 
00572 template<>
00573 struct DefaultDescription<OpenRTB::FramePosition>
00574     : public TaggedEnumDescription<OpenRTB::FramePosition> {
00575 
00576     DefaultDescription()
00577     {
00578     }
00579 };
00580 
00581 template<>
00582 struct DefaultDescription<OpenRTB::SourceRelationship>
00583     : public TaggedEnumDescription<OpenRTB::SourceRelationship> {
00584 
00585     DefaultDescription()
00586     {
00587     }
00588 };
00589 
00590 template<>
00591 struct DefaultDescription<OpenRTB::AdPosition>
00592     : public TaggedEnumDescription<OpenRTB::AdPosition> {
00593 
00594     DefaultDescription()
00595     {
00596     }
00597 };
00598 
00599 template<>
00600 struct DefaultDescription<OpenRTB::BidRequest>
00601     : public StructureDescription<OpenRTB::BidRequest> {
00602     DefaultDescription();
00603 };
00604 
00605 template<>
00606 struct DefaultDescription<OpenRTB::Impression>
00607     : public StructureDescription<OpenRTB::Impression> {
00608     DefaultDescription();
00609 };
00610 
00611 template<>
00612 struct DefaultDescription<OpenRTB::Banner>
00613     : public StructureDescription<OpenRTB::Banner> {
00614     DefaultDescription();
00615 };
00616 
00617 template<>
00618 struct DefaultDescription<OpenRTB::Video>
00619     : public StructureDescription<OpenRTB::Video> {
00620     DefaultDescription();
00621 };
00622 
00623 template<>
00624 struct DefaultDescription<OpenRTB::Content>
00625     : public StructureDescription<OpenRTB::Content> {
00626     DefaultDescription();
00627 };
00628 
00629 template<>
00630 struct DefaultDescription<OpenRTB::Context>
00631     : public StructureDescription<OpenRTB::Context> {
00632     DefaultDescription();
00633 };
00634 
00635 template<>
00636 struct DefaultDescription<OpenRTB::Site>
00637     : public StructureDescription<OpenRTB::Site> {
00638     DefaultDescription();
00639 };
00640 
00641 template<>
00642 struct DefaultDescription<OpenRTB::App>
00643     : public StructureDescription<OpenRTB::App> {
00644     DefaultDescription();
00645 };
00646 
00647 template<>
00648 struct DefaultDescription<OpenRTB::Device>
00649     : public StructureDescription<OpenRTB::Device> {
00650     DefaultDescription();
00651 };
00652 
00653 template<>
00654 struct DefaultDescription<OpenRTB::User>
00655     : public StructureDescription<OpenRTB::User> {
00656     DefaultDescription();
00657 };
00658 
00659 template<>
00660 struct DefaultDescription<OpenRTB::Publisher>
00661     : public StructureDescription<OpenRTB::Publisher> {
00662     DefaultDescription();
00663 };
00664 
00665 template<>
00666 struct DefaultDescription<OpenRTB::Geo>
00667     : public StructureDescription<OpenRTB::Geo> {
00668     DefaultDescription();
00669 };
00670 
00671 template<>
00672 struct DefaultDescription<OpenRTB::Data>
00673     : public StructureDescription<OpenRTB::Data> {
00674     DefaultDescription();
00675 };
00676 
00677 template<>
00678 struct DefaultDescription<OpenRTB::Segment>
00679     : public StructureDescription<OpenRTB::Segment> {
00680     DefaultDescription();
00681 };
00682 
00683 template<>
00684 struct DefaultDescription<OpenRTB::Bid>
00685     : public StructureDescription<OpenRTB::Bid> {
00686     DefaultDescription();
00687 };
00688 
00689 template<>
00690 struct DefaultDescription<OpenRTB::SeatBid>
00691     : public StructureDescription<OpenRTB::SeatBid> {
00692     DefaultDescription();
00693 };
00694 
00695 template<>
00696 struct DefaultDescription<OpenRTB::BidResponse>
00697     : public StructureDescription<OpenRTB::BidResponse> {
00698     DefaultDescription();
00699 };
00700 
00701 
00702 
00703 
00704 } // namespace Datacratic
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator