Belle II Software light-2406-ragdoll
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).
 
std::string escapeXML (const std::string &xmlString)
 Escape given XML string as CDATA sequence.
 
TObject * deserializeEncodedRawData (const std::string &base64Data)
 Convert given serialized raw data back into TObject.
 

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.

65{
66 //avoid nesting CDATA sections...
67 std::string newString(xmlString);
68 boost::replace_all(newString, "]]>", "]]]]><![CDATA[>");
69 return "<![CDATA[" + newString + "]]>";
70}

◆ 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.

33{
34 TMessage::EnableSchemaEvolutionForAll();
35
36 TMessage msg(kMESS_OBJECT);
37 msg.SetWriteMode();
38
39 // Currently disabled, but can be made to work by copying the input buffer in deserializeEncodedRawData
40 // However, this is a workaround and thus doesn't seem like a good idea.
41 // Once https://sft.its.cern.ch/jira/browse/ROOT-4550 is fixed, this can simply be turned on,
42 // but make sure to check that old (uncompressed) data can still be deserialized.
43 msg.SetCompressionLevel(0);
44
45 msg.WriteObject(obj);
46 msg.Compress(); //only does something if compression active
47
48 //convert TMessage into base64-encoded string
49 char* buf = msg.Buffer();
50 UInt_t len = msg.Length();
51
52 if (msg.CompBuffer()) {
53 B2FATAL("compression used, but broken thanks to ROOT");
54 /* for future use?
55 buf = msg.CompBuffer();
56 len = msg.CompLength();
57 */
58 }
59
60 const std::string& encodedStr(TBase64::Encode(buf, len).Data());
61 return encodedStr;
62}