Skip to content

Commit ef43f93

Browse files
committed
[json] Introduce TBufferJSON::zipJSON method
It idea to provide string which can be embed in any JavaScript code and then unzipped whith JSROOT.unzipJSON function. Primary aim is to improve json handling in jupyter
1 parent 05af3c7 commit ef43f93

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

io/io/inc/TBufferJSON.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class TBufferJSON final : public TBufferText {
7777
return ConvertToJSON(obj, TClass::GetClass<T>(), compact, member_name);
7878
}
7979

80+
static TString zipJSON(const char *json);
81+
8082
template <class T>
8183
static Bool_t FromJSON(T *&obj, const char *json)
8284
{

io/io/src/TBufferJSON.cxx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,28 @@ TString TBufferJSON::ConvertToJSON(const TObject *obj, Int_t compact, const char
541541
return ConvertToJSON(ptr, clActual, compact, member_name);
542542
}
543543

544+
////////////////////////////////////////////////////////////////////////////////
545+
/// zip JSON string and convert into base64 string
546+
/// to be used with JSROOT unzipJSON() function
547+
/// Main application - embed large JSON code into jupyter notebooks
548+
549+
TString TBufferJSON::zipJSON(const char *json)
550+
{
551+
std::string buf;
552+
553+
int srcsize = (int) strlen(json);
554+
555+
buf.resize(srcsize + 500);
556+
557+
int tgtsize = buf.length();
558+
559+
int nout = 0;
560+
561+
R__zipMultipleAlgorithm(ROOT::RCompressionSetting::ELevel::kDefaultZLIB, &srcsize, (char *)json, &tgtsize, (char *) buf.data(), &nout, ROOT::RCompressionSetting::EAlgorithm::kZLIB);
562+
563+
return TBase64::Encode(buf.data(), nout);
564+
}
565+
544566
////////////////////////////////////////////////////////////////////////////////
545567
/// Set level of space/newline/array compression
546568
/// Lower digit of compact parameter define formatting rules

0 commit comments

Comments
 (0)