Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/root/TBufferJSON.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // $Id$
0002 // Author: Sergey Linev  4.03.2014
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TBufferJSON
0013 #define ROOT_TBufferJSON
0014 
0015 #include "TBufferText.h"
0016 #include "TString.h"
0017 
0018 #include <deque>
0019 #include <memory>
0020 #include <string>
0021 #include <vector>
0022 
0023 class TVirtualStreamerInfo;
0024 class TStreamerInfo;
0025 class TStreamerElement;
0026 class TMemberStreamer;
0027 class TDataMember;
0028 class TJSONStackObj;
0029 
0030 class TBufferJSON final : public TBufferText {
0031 
0032 public:
0033 
0034    enum {
0035      // values 0..3 are exclusive, define text formating, JSON data are same
0036      kNoCompress    = 0,             ///< no any compression, maximal size of JSON (default)
0037      kNoIndent      = 1,             ///< remove spaces in the beginning showing JSON indentation level
0038      kNoNewLine     = 2,             ///< no indent plus skip newline symbols
0039      kNoSpaces      = 3,             ///< no new lines plus remove all spaces around "," and ":" symbols
0040 
0041      kMapAsObject   = 5,             ///< store std::map, std::unordered_map as JSON object
0042 
0043      // algorithms for array compression - exclusive
0044      kZeroSuppression = 10,          ///< if array has much zeros in begin and/or end, they will be removed
0045      kSameSuppression = 20,          ///< zero suppression plus compress many similar values together
0046      kBase64          = 30,          ///< all binary arrays will be compressed with base64 coding, supported by JSROOT
0047 
0048      kSkipTypeInfo  = 100,           ///< do not store typenames in JSON
0049 
0050      kStoreInfNaN = 1000             ///< explicitly store special float numbers as strings ("inf", "nan")
0051    };
0052 
0053    TBufferJSON(TBuffer::EMode mode = TBuffer::kWrite);
0054    ~TBufferJSON() override;
0055 
0056    void SetCompact(int level);
0057    void SetTypenameTag(const char *tag = "_typename");
0058    void SetTypeversionTag(const char *tag = nullptr);
0059    void SetSkipClassInfo(const TClass *cl);
0060    Bool_t IsSkipClassInfo(const TClass *cl) const;
0061 
0062    TString StoreObject(const void *obj, const TClass *cl);
0063    void *RestoreObject(const char *str, TClass **cl);
0064 
0065    static TString ConvertToJSON(const TObject *obj, Int_t compact = 0, const char *member_name = nullptr);
0066    static TString
0067    ConvertToJSON(const void *obj, const TClass *cl, Int_t compact = 0, const char *member_name = nullptr);
0068    static TString ConvertToJSON(const void *obj, TDataMember *member, Int_t compact = 0, Int_t arraylen = -1);
0069 
0070    static Int_t ExportToFile(const char *filename, const TObject *obj, const char *option = nullptr);
0071    static Int_t ExportToFile(const char *filename, const void *obj, const TClass *cl, const char *option = nullptr);
0072 
0073    static TObject *ConvertFromJSON(const char *str);
0074    static void *ConvertFromJSONAny(const char *str, TClass **cl = nullptr);
0075 
0076    template <class T>
0077    static TString ToJSON(const T *obj, Int_t compact = 0, const char *member_name = nullptr)
0078    {
0079       return ConvertToJSON(obj, TClass::GetClass<T>(), compact, member_name);
0080    }
0081 
0082    static TString zipJSON(const char *json);
0083 
0084    template <class T>
0085    static Bool_t FromJSON(T *&obj, const char *json)
0086    {
0087       if (obj)
0088          return kFALSE;
0089       obj = (T *)ConvertFromJSONChecked(json, TClass::GetClass<T>());
0090       return obj != nullptr;
0091    }
0092 
0093    template <class T>
0094    static std::unique_ptr<T> FromJSON(const std::string &json)
0095    {
0096       T *obj = (T *)ConvertFromJSONChecked(json.c_str(), TClass::GetClass<T>());
0097       return std::unique_ptr<T>(obj);
0098    }
0099 
0100    // suppress class writing/reading
0101 
0102    TClass *ReadClass(const TClass *cl = nullptr, UInt_t *objTag = nullptr) final;
0103    void WriteClass(const TClass *cl) final;
0104 
0105    // redefined virtual functions of TBuffer
0106 
0107    Version_t ReadVersion(UInt_t *start = nullptr, UInt_t *bcnt = nullptr, const TClass *cl = nullptr) final;
0108    UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt = kFALSE) final;
0109 
0110    void *ReadObjectAny(const TClass *clCast) final;
0111    void SkipObjectAny() final;
0112 
0113    // these methods used in streamer info to indicate currently streamed element,
0114    void IncrementLevel(TVirtualStreamerInfo *) final;
0115    void SetStreamerElementNumber(TStreamerElement *elem, Int_t comp_type) final;
0116    void DecrementLevel(TVirtualStreamerInfo *) final;
0117 
0118    void ClassBegin(const TClass *, Version_t = -1) final;
0119    void ClassEnd(const TClass *) final;
0120    void ClassMember(const char *name, const char *typeName = nullptr, Int_t arrsize1 = -1, Int_t arrsize2 = -1) final;
0121 
0122    Int_t ReadArray(Bool_t *&b) final;
0123    Int_t ReadArray(Char_t *&c) final;
0124    Int_t ReadArray(UChar_t *&c) final;
0125    Int_t ReadArray(Short_t *&h) final;
0126    Int_t ReadArray(UShort_t *&h) final;
0127    Int_t ReadArray(Int_t *&i) final;
0128    Int_t ReadArray(UInt_t *&i) final;
0129    Int_t ReadArray(Long_t *&l) final;
0130    Int_t ReadArray(ULong_t *&l) final;
0131    Int_t ReadArray(Long64_t *&l) final;
0132    Int_t ReadArray(ULong64_t *&l) final;
0133    Int_t ReadArray(Float_t *&f) final;
0134    Int_t ReadArray(Double_t *&d) final;
0135 
0136    Int_t ReadStaticArray(Bool_t *b) final;
0137    Int_t ReadStaticArray(Char_t *c) final;
0138    Int_t ReadStaticArray(UChar_t *c) final;
0139    Int_t ReadStaticArray(Short_t *h) final;
0140    Int_t ReadStaticArray(UShort_t *h) final;
0141    Int_t ReadStaticArray(Int_t *i) final;
0142    Int_t ReadStaticArray(UInt_t *i) final;
0143    Int_t ReadStaticArray(Long_t *l) final;
0144    Int_t ReadStaticArray(ULong_t *l) final;
0145    Int_t ReadStaticArray(Long64_t *l) final;
0146    Int_t ReadStaticArray(ULong64_t *l) final;
0147    Int_t ReadStaticArray(Float_t *f) final;
0148    Int_t ReadStaticArray(Double_t *d) final;
0149 
0150    void ReadFastArray(Bool_t *b, Int_t n) final;
0151    void ReadFastArray(Char_t *c, Int_t n) final;
0152    void ReadFastArrayString(Char_t *c, Int_t n) final;
0153    void ReadFastArray(UChar_t *c, Int_t n) final;
0154    void ReadFastArray(Short_t *h, Int_t n) final;
0155    void ReadFastArray(UShort_t *h, Int_t n) final;
0156    void ReadFastArray(Int_t *i, Int_t n) final;
0157    void ReadFastArray(UInt_t *i, Int_t n) final;
0158    void ReadFastArray(Long_t *l, Int_t n) final;
0159    void ReadFastArray(ULong_t *l, Int_t n) final;
0160    void ReadFastArray(Long64_t *l, Int_t n) final;
0161    void ReadFastArray(ULong64_t *l, Int_t n) final;
0162    void ReadFastArray(Float_t *f, Int_t n) final;
0163    void ReadFastArray(Double_t *d, Int_t n) final;
0164    void ReadFastArray(void *start, const TClass *cl, Int_t n = 1, TMemberStreamer *s = nullptr,
0165                       const TClass *onFileClass = nullptr) final;
0166    void ReadFastArray(void **startp, const TClass *cl, Int_t n = 1, Bool_t isPreAlloc = kFALSE,
0167                       TMemberStreamer *s = nullptr, const TClass *onFileClass = nullptr) final;
0168 
0169    void WriteArray(const Bool_t *b, Int_t n) final;
0170    void WriteArray(const Char_t *c, Int_t n) final;
0171    void WriteArray(const UChar_t *c, Int_t n) final;
0172    void WriteArray(const Short_t *h, Int_t n) final;
0173    void WriteArray(const UShort_t *h, Int_t n) final;
0174    void WriteArray(const Int_t *i, Int_t n) final;
0175    void WriteArray(const UInt_t *i, Int_t n) final;
0176    void WriteArray(const Long_t *l, Int_t n) final;
0177    void WriteArray(const ULong_t *l, Int_t n) final;
0178    void WriteArray(const Long64_t *l, Int_t n) final;
0179    void WriteArray(const ULong64_t *l, Int_t n) final;
0180    void WriteArray(const Float_t *f, Int_t n) final;
0181    void WriteArray(const Double_t *d, Int_t n) final;
0182 
0183    void WriteFastArray(const Bool_t *b, Long64_t n) final;
0184    void WriteFastArray(const Char_t *c, Long64_t n) final;
0185    void WriteFastArrayString(const Char_t *c, Long64_t n) final;
0186    void WriteFastArray(const UChar_t *c, Long64_t n) final;
0187    void WriteFastArray(const Short_t *h, Long64_t n) final;
0188    void WriteFastArray(const UShort_t *h, Long64_t n) final;
0189    void WriteFastArray(const Int_t *i, Long64_t n) final;
0190    void WriteFastArray(const UInt_t *i, Long64_t n) final;
0191    void WriteFastArray(const Long_t *l, Long64_t n) final;
0192    void WriteFastArray(const ULong_t *l, Long64_t n) final;
0193    void WriteFastArray(const Long64_t *l, Long64_t n) final;
0194    void WriteFastArray(const ULong64_t *l, Long64_t n) final;
0195    void WriteFastArray(const Float_t *f, Long64_t n) final;
0196    void WriteFastArray(const Double_t *d, Long64_t n) final;
0197    void WriteFastArray(void *start, const TClass *cl, Long64_t n = 1, TMemberStreamer *s = nullptr) final;
0198    Int_t WriteFastArray(void **startp, const TClass *cl, Long64_t n = 1, Bool_t isPreAlloc = kFALSE,
0199                         TMemberStreamer *s = nullptr) final;
0200 
0201    void StreamObject(void *obj, const TClass *cl, const TClass *onFileClass = nullptr) final;
0202    using TBufferText::StreamObject;
0203 
0204    void ReadBool(Bool_t &b) final;
0205    void ReadChar(Char_t &c) final;
0206    void ReadUChar(UChar_t &c) final;
0207    void ReadShort(Short_t &s) final;
0208    void ReadUShort(UShort_t &s) final;
0209    void ReadInt(Int_t &i) final;
0210    void ReadUInt(UInt_t &i) final;
0211    void ReadLong(Long_t &l) final;
0212    void ReadULong(ULong_t &l) final;
0213    void ReadLong64(Long64_t &l) final;
0214    void ReadULong64(ULong64_t &l) final;
0215    void ReadFloat(Float_t &f) final;
0216    void ReadDouble(Double_t &d) final;
0217    void ReadCharP(Char_t *c) final;
0218    void ReadTString(TString &s) final;
0219    void ReadStdString(std::string *s) final;
0220    using TBuffer::ReadStdString;
0221    void ReadCharStar(char *&s) final;
0222 
0223    void WriteBool(Bool_t b) final;
0224    void WriteChar(Char_t c) final;
0225    void WriteUChar(UChar_t c) final;
0226    void WriteShort(Short_t s) final;
0227    void WriteUShort(UShort_t s) final;
0228    void WriteInt(Int_t i) final;
0229    void WriteUInt(UInt_t i) final;
0230    void WriteLong(Long_t l) final;
0231    void WriteULong(ULong_t l) final;
0232    void WriteLong64(Long64_t l) final;
0233    void WriteULong64(ULong64_t l) final;
0234    void WriteFloat(Float_t f) final;
0235    void WriteDouble(Double_t d) final;
0236    void WriteCharP(const Char_t *c) final;
0237    void WriteTString(const TString &s) final;
0238    void WriteStdString(const std::string *s) final;
0239    using TBuffer::WriteStdString;
0240    void WriteCharStar(char *s) final;
0241 
0242    TVirtualStreamerInfo *GetInfo() final;
0243 
0244    // end of redefined virtual functions
0245 
0246    void ReadBaseClass(void *start, TStreamerBase *elem) final;
0247 
0248 protected:
0249    // redefined protected virtual functions
0250 
0251    void WriteObjectClass(const void *actualObjStart, const TClass *actualClass, Bool_t cacheReuse) final;
0252 
0253    // end redefined protected virtual functions
0254 
0255    static void *ConvertFromJSONChecked(const char *str, const TClass *expectedClass);
0256 
0257    TString JsonWriteMember(const void *ptr, TDataMember *member, TClass *memberClass, Int_t arraylen);
0258 
0259    TJSONStackObj *PushStack(Int_t inclevel = 0, void *readnode = nullptr);
0260    TJSONStackObj *PopStack();
0261    TJSONStackObj *Stack() { return fStack.back().get(); }
0262 
0263    void WorkWithClass(TStreamerInfo *info, const TClass *cl = nullptr);
0264    void WorkWithElement(TStreamerElement *elem, Int_t);
0265 
0266    void JsonDisablePostprocessing();
0267    Int_t JsonSpecialClass(const TClass *cl) const;
0268 
0269    TJSONStackObj *JsonStartObjectWrite(const TClass *obj_class, TStreamerInfo *info = nullptr);
0270 
0271    void JsonStartElement(const TStreamerElement *elem, const TClass *base_class);
0272 
0273    void PerformPostProcessing(TJSONStackObj *stack, const TClass *obj_cl = nullptr);
0274 
0275    void JsonWriteBasic(Char_t value);
0276    void JsonWriteBasic(Short_t value);
0277    void JsonWriteBasic(Int_t value);
0278    void JsonWriteBasic(Long_t value);
0279    void JsonWriteBasic(Long64_t value);
0280    void JsonWriteBasic(Float_t value);
0281    void JsonWriteBasic(Double_t value);
0282    void JsonWriteBasic(Bool_t value);
0283    void JsonWriteBasic(UChar_t value);
0284    void JsonWriteBasic(UShort_t value);
0285    void JsonWriteBasic(UInt_t value);
0286    void JsonWriteBasic(ULong_t value);
0287    void JsonWriteBasic(ULong64_t value);
0288 
0289    void JsonWriteConstChar(const char *value, Int_t len = -1, const char * /*typname*/ = nullptr);
0290 
0291    void JsonWriteObject(const void *obj, const TClass *objClass, Bool_t check_map = kTRUE);
0292 
0293    void JsonWriteCollection(TCollection *obj, const TClass *objClass);
0294 
0295    void JsonReadCollection(TCollection *obj, const TClass *objClass);
0296 
0297    void JsonReadTObjectMembers(TObject *obj, void *node = nullptr);
0298 
0299    void *JsonReadObject(void *obj, const TClass *objClass = nullptr, TClass **readClass = nullptr);
0300 
0301    void AppendOutput(const char *line0, const char *line1 = nullptr);
0302 
0303    void JsonPushValue();
0304 
0305    template <typename T>
0306    void JsonWriteArrayCompress(const T *vname, Int_t arrsize, const char *typname);
0307 
0308    template <typename T>
0309    void JsonReadBasic(T &value);
0310 
0311    template <typename T>
0312    Int_t JsonReadArray(T *value);
0313 
0314    template <typename T>
0315    void JsonReadFastArray(T *arr, Int_t arrsize, bool asstring = false);
0316 
0317    template <typename T>
0318    void JsonWriteFastArray(const T *arr, Long64_t arrsize, const char *typname,
0319                                             void (TBufferJSON::*method)(const T *, Int_t, const char *));
0320 
0321    TString fOutBuffer;                 ///<!  main output buffer for json code
0322    TString *fOutput{nullptr};          ///<!  current output buffer for json code
0323    TString fValue;                     ///<!  buffer for current value
0324    unsigned fJsonrCnt{0};              ///<!  counter for all objects, used for referencing
0325    std::deque<std::unique_ptr<TJSONStackObj>> fStack; ///<!  hierarchy of currently streamed element
0326    Int_t fCompact{0};                  ///<!  0 - no any compression, 1 - no spaces in the begin, 2 - no new lines, 3 - no spaces at all
0327    Bool_t fMapAsObject{kFALSE};        ///<! when true, std::map will be converted into JSON object
0328    TString fSemicolon;                 ///<!  depending from compression level, " : " or ":"
0329    Int_t fArrayCompact{0};             ///<!  0 - no array compression, 1 - exclude leading/trailing zeros, 2 - check value repetition
0330    TString fArraySepar;                ///<!  depending from compression level, ", " or ","
0331    TString fNumericLocale;             ///<!  stored value of setlocale(LC_NUMERIC), which should be recovered at the end
0332    Bool_t fStoreInfNaN{kFALSE};        ///<! when true, store inf and nan as string, this is not portable for other JSON readers
0333    TString fTypeNameTag;               ///<! JSON member used for storing class name, when empty - no class name will be stored
0334    TString fTypeVersionTag;            ///<! JSON member used to store class version, default empty
0335    std::vector<const TClass *> fSkipClasses; ///<! list of classes, which class info is not stored
0336 
0337    ClassDefOverride(TBufferJSON, 0) // a specialized TBuffer to only write objects into JSON format
0338 };
0339 
0340 #endif