Belle II Software  release-08-01-10
Belle2::Stream Namespace Reference

Define (de)serialization methods for TObject. More...

Functions

std::string serializeAndEncode (const TObject *obj)
 Convert given TObject into encoded byte stream (for storing in XML). More...
 
std::string escapeXML (const std::string &xmlString)
 Escape given XML string as CDATA sequence. More...
 
TObject * deserializeEncodedRawData (const std::string &base64Data)
 Convert given serialized raw data back into TObject. More...
 

Detailed Description

Define (de)serialization methods for TObject.

This code is also exported to Python, after 'from ROOT import Belle2' it is available as Belle2.Stream.

Function Documentation

◆ deserializeEncodedRawData()

TObject * deserializeEncodedRawData ( const std::string &  base64Data)

Convert given serialized raw data back into TObject.

Returns a pointer to the deserialized object, might be NULL if conversion was impossible. User is responsible for deletion.

If input is not well-formed base64-encoded data, this might crash.

Definition at line 72 of file Stream.cc.

73 {
74  if (base64Data.empty())
75  return nullptr;
76 
77  //convert data back into raw byte stream
78  const TString& data(TBase64::Decode(base64Data.c_str()));
79 
80  StreamMsg msg(data.Data(), data.Length());
81  //some checking
82  if (msg.What() != kMESS_OBJECT or msg.GetClass() == nullptr) {
83  return nullptr;
84  }
85  auto* obj = static_cast<TObject*>(msg.ReadObjectAny(msg.GetClass()));
86 
87  return obj;
88 }

◆ escapeXML()

std::string escapeXML ( const std::string &  xmlString)

Escape given XML string as CDATA sequence.

This format is suitable for storing in an XML file, wrap it in a tag and use Gearbox::getInstance().getTObject(".../MyTag") to retrieve the object again.

Definition at line 64 of file Stream.cc.

◆ serializeAndEncode()

std::string serializeAndEncode ( const TObject *  obj)

Convert given TObject into encoded byte stream (for storing in XML).

Returns base64-encoded TMessage. Please pass it through escapeXML() first.

Definition at line 32 of file Stream.cc.