130#include <nlohmann/json.hpp>
165 ::Error(
"TArrayIndexProducer",
"Problem with JSON coding of element %s type %d",
elem->GetName(),
173 for (
int dim = 0; dim <
elem->GetArrayDim(); dim++)
200 for (
int dim = 0; dim <
member->GetArrayDim(); dim++) {
355 std::unique_ptr<TArrayIndexProducer>
fIndx;
398 if (!
json->is_object())
return -1;
401 for (
auto it =
json->begin(); it !=
json->end(); ++it) {
408 if (
json->is_array())
412 if (
json->is_object() && (
json->count(
"$arr") == 1))
413 return json->at(
"len").get<
int>();
420 auto res = std::stoi(
fValues.back());
431 auto indx = std::make_unique<TArrayIndexProducer>(
fElem, -1,
"");
434 if (!
indx->IsArray() || (
indx->NumDimensions() < 2))
444 fStlRead = std::make_unique<StlRead>();
447 if (!
fNode->is_object()) {
448 ::Error(
"TJSONStackObj::AssignStl",
"when reading %s expecting JSON object", cl->
GetName());
454 if (!
fNode->is_array() && !(
fNode->is_object() && (
fNode->count(
"$arr") == 1))) {
455 ::Error(
"TJSONStackObj::AssignStl",
"when reading %s expecting JSON array", cl->
GetName());
477 :
TBufferText(
mode), fOutBuffer(), fOutput(nullptr), fValue(), fStack(), fSemicolon(
" : "), fArraySepar(
", "),
478 fNumericLocale(), fTypeNameTag(
"_typename")
525 void *ptr = (
void *)obj;
665 if (
member->GetArrayIndex() != 0) {
675 ptr = *((
char **)ptr);
712 Error(
"StoreObject",
"Can not store object into TBuffer for reading");
776 char *buffer = (
char *)
malloc(buflen);
824 return json.Length();
856 char *buffer = (
char *)
malloc(buflen);
904 return json.Length();
926 return (
TObject *)(((
char *)obj) + delta);
953 if (
docu.is_null() || (!
docu.is_object() && !
docu.is_array()))
994 ::Error(
"TBufferJSON::ConvertFromJSONChecked",
"expected class %s is not base for read class %s",
1000 return (
char *)res -
offset;
1013 Info(
"JsonWriteMember",
"Write member %s type %s ndim %d",
member->GetName(),
member->GetTrueTypeName(),
1017 if (
strcmp(
member->GetTrueTypeName(),
"const char*") == 0)
1035 shift =
indx.ReduceDimension();
1037 char *
ppp = (
char *)ptr;
1080 }
while (!
indx.IsDone());
1100 if (
arr && (
arr->GetSize() > 0)) {
1101 arr->Streamer(*
this);
1104 Warning(
"TBufferJSON",
"When streaming TArray, more than 1 object in the stack, use second item");
1119 return "<not supported>";
1132 next->fNode = (nlohmann::json *)
readnode;
1133 }
else if (
fStack.size() > 0) {
1134 auto prev =
Stack();
1135 next->fLevel += prev->fLevel;
1136 next->fMemberPtr = prev->fMemberPtr;
1138 fStack.emplace_back(next);
1150 return fStack.size() > 0 ?
fStack.back().get() :
nullptr;
1167 if (
Stack()->fLevel > 0)
1183 stack->fMemberPtr = &stack->fMemberCnt;
1202 stack->fMemberCnt = 0;
1222 if ((
strcmp(
elem->GetTypeName(),
"TString") == 0) && (
strcmp(
elem->GetFullName(),
"fLineStyle[30]") == 0)) {
1224 if (
st1->IsStreamerInfo() &&
st1->fInfo && (
strcmp(
st1->fInfo->GetName(),
"TStyle") == 0))
1253 Error(
"JsonStartElement",
"Missing JSON structure for element %s",
elem_name);
1260 Error(
"JsonStartElement",
"Missing array when reading TArray class for element %s",
elem->GetName());
1329 Info(
"JsonWriteObject",
"Object %p class %s check_map %s", obj, cl ? cl->
GetName() :
"null",
1409 Info(
"JsonWriteObject",
"Starting object %p write for class: %s", obj, cl->
GetName());
1416 (
const_cast<TClass *
>(cl))->Streamer((
void *)obj, *
this);
1419 Info(
"JsonWriteObject",
"Done object %p write for class: %s", obj, cl->
GetName());
1422 if (stack->
fValues.size() != 1)
1423 Error(
"JsonWriteObject",
"Problem when writing array");
1426 if (stack->
fValues.size() > 2)
1427 Error(
"JsonWriteObject",
"Problem when writing TString or std::string");
1465 }
else if (stack->
fValues.empty()) {
1468 Error(
"JsonWriteObject",
"With empty stack fValue!=0");
1490 Error(
"JsonWriteObject",
"Empty value when it should contain something");
1495 const char *
separ =
"[";
1504 if (
pairtype.Index(
"unordered_map<") == 0)
1506 else if (
pairtype.Index(
"unordered_multimap<") == 0)
1508 else if (
pairtype.Index(
"multimap<") == 0)
1510 else if (
pairtype.Index(
"map<") == 0)
1556 Info(
"JsonWriteObject",
"Create blob value for class %s", cl->
GetName());
1561 const char *
separ =
"[";
1593 Error(
"JsonWriteObject",
"Non-empty object output for special class %s", cl->
GetName());
1613 auto map =
dynamic_cast<TMap *
>(col);
1614 auto lst =
dynamic_cast<TList *
>(col);
1624 auto lnk =
lst->FirstLink();
1635 sopt.Append(
lnk->GetAddOption());
1638 sopt.Append(
"null");
1646 while (
auto obj = iter()) {
1667 while (
auto obj = iter()) {
1697 TMap *map =
nullptr;
1702 map =
dynamic_cast<TMap *
>(col);
1708 std::string
name =
json->at(
"name");
1711 nlohmann::json &
arr =
json->at(
"arr");
1714 for (
int n = 0;
n <
size; ++
n) {
1731 Error(
"JsonReadCollection",
1732 "Cannot detect class name for TClonesArray - typename tag not configured");
1736 Error(
"JsonReadCollection",
"TClonesArray size %d smaller than required %d",
clones->GetSize(),
size);
1754 Error(
"JsonReadCollection",
"Try to add object %s not derived from TObject",
readClass->GetName());
1771 Error(
"JsonReadCollection",
"Try to add object %s not derived from TObject",
readClass->GetName());
1803 if (
json->is_null())
1809 if (
json->is_object() && (
json->size() == 1) && (
json->find(
"$ref") !=
json->end())) {
1810 unsigned refid =
json->at(
"$ref").get<
unsigned>();
1818 Error(
"JsonReadObject",
"Fail to find object for reference %u",
refid);
1826 Info(
"JsonReadObject",
"Extract object reference %u %p cl:%s expects:%s",
refid,
ref_obj,
ref_cl->GetName(),
1838 Info(
"JsonReadObject",
"Read string from %s",
json->dump().c_str());
1841 *((std::string *)obj) =
json->get<std::string>();
1843 *((
TString *)obj) =
json->get<std::string>().c_str();
1854 Error(
"JsonReadObject",
"No object when reading base class");
1884 Error(
"JsonReadObject",
"Not array when expecting such %s",
json->dump().c_str());
1895 Info(
"JsonReadObject",
"Reading baseclass %s ptr %p",
objClass->GetName(), obj);
1902 Error(
"JsonReadObject",
"Cannot find class %s",
clname.c_str());
1920 Error(
"JsonReadObject",
"Not possible to read %s and casting to %s pointer as the two classes are unrelated",
1923 Error(
"JsonReadObject",
"Reading %s and casting to %s pointer is currently not supported",
1935 Info(
"JsonReadObject",
"Reading object of class %s refid %u ptr %p",
jsonClass->GetName(),
fJsonrCnt, obj);
1969 Info(
"JsonReadObject",
"Calling streamer of class %s",
jsonClass->GetName());
1972 Error(
"JsonReadObject",
"Should not be used for reading of base class %s",
jsonClass->GetName());
1975 jsonClass->Streamer((
void *)obj, *
this);
1987 Info(
"JsonReadObject",
"Reading object of class %s done",
jsonClass->GetName());
2001 nlohmann::json *
json = node ? (nlohmann::json *)node :
Stack()->
fNode;
2003 UInt_t uid =
json->at(
"fUniqueID").get<
unsigned>();
2004 UInt_t bits =
json->at(
"fBits").get<
unsigned>();
2007 tobj->SetUniqueID(uid);
2027 Info(
"IncrementLevel",
"Class: %s", (
info ?
info->GetClass()->GetName() :
"custom"));
2038 cl =
sinfo->GetClass();
2044 Info(
"WorkWithClass",
"Class: %s", cl->
GetName());
2074 Info(
"DecrementLevel",
"Class: %s", (
info ?
info->GetClass()->GetName() :
"custom"));
2082 Info(
"DecrementLevel",
" Perform post-processing elem: %s", stack->
fElem->
GetName());
2091 Error(
"DecrementLevel",
" Mismatch of streamer info");
2096 Info(
"DecrementLevel",
"Class: %s done", (
info ?
info->GetClass()->GetName() :
"custom"));
2115 Info(
"SetStreamerElementNumber",
"Element name %s",
elem->GetName());
2129 Error(
"WorkWithElement",
"stack is empty");
2134 Info(
"WorkWithElement",
" Start element %s type %d typename %s",
elem ?
elem->GetName() :
"---",
2142 Info(
"WorkWithElement",
" Perform post-processing elem: %s", stack->
fElem->
GetName());
2152 Error(
"WorkWithElement",
"Lost of stack");
2158 Error(
"WorkWithElement",
"Problem in Inc/Dec level");
2165 Error(
"WorkWithElement",
"streamer info returns elem = nullptr");
2261 Error(
"ClassMember",
"Invalid member name");
2269 if (
strcmp(typeName,
"raw:data") == 0)
2274 if (
dt && (
dt->GetType() > 0) && (
dt->GetType() < 20))
2293 Error(
"ClassMember",
"Invalid class specifier %s", typeName);
2333 Error(
"ClassMember",
"Invalid combination name = %s type = %s",
name, typeName);
2392 Info(
"PerformPostProcessing",
"reformat string value = '%s'",
fValue.
Data());
2400 }
else if ((stack->
fValues.size() == 1) && (stack->
fValues[0] ==
"1")) {
2403 Error(
"PerformPostProcessing",
"Wrong values for kOffsetP element %s", (
elem ?
elem->GetName() :
"---"));
2418 Error(
"PerformPostProcessing",
"When storing TObject/TRef, strange number of items %d", cnt);
2450 if (!stack->
fValues.empty()) {
2495 if (!cl &&
Stack()->fClVersion) {
2501 Info(
"ReadVersion",
"Result: %d Class: %s", res, (cl ? cl->
GetName() :
"---"));
2520 Info(
"ReadObjectAny",
"From current JSON node");
2657template <
typename T>
2660 Info(
"ReadArray",
"Not implemented");
2661 return value ? 1 : 0;
2771template <
typename T>
2778 Info(
"ReadFastArray",
"Reading array sz %d from JSON %s",
arrsize,
json->dump().substr(0, 30).c_str());
2785 for (
int cnt = 0; cnt <
arrsize; ++cnt) {
2787 for (
int k = 1; k <
lastdim; ++k)
2793 std::string str =
json->get<std::string>();
2794 for (
int cnt = 0; cnt <
arrsize; ++cnt)
2795 arr[cnt] = (cnt < (
int)str.length()) ? str[cnt] : 0;
2796 }
else if (
json->is_object() && (
json->count(
"$arr") == 1)) {
2798 Error(
"ReadFastArray",
"Mismatch compressed array size %d %d",
arrsize,
json->at(
"len").get<
int>());
2800 for (
int cnt = 0; cnt <
arrsize; ++cnt)
2803 if (
json->count(
"b") == 1) {
2804 auto base64 =
json->at(
"b").get<std::string>();
2806 int offset = (
json->count(
"o") == 1) ?
json->at(
"o").get<
int>() : 0;
2811 if (
arrsize * (
long)
sizeof(T) < (
offset + decode.Length())) {
2812 Error(
"ReadFastArray",
"Base64 data %ld larger than target array size %ld", (
long) decode.Length() +
offset, (
long) (
arrsize*
sizeof(T)));
2813 }
else if ((
sizeof(T) > 1) && (decode.Length() %
sizeof(T) != 0)) {
2814 Error(
"ReadFastArray",
"Base64 data size %ld not matches with element size %ld", (
long) decode.Length(), (
long)
sizeof(T));
2832 for (
unsigned sub = 0; sub <
v.size(); ++sub)
2837 for (
unsigned sub = 0; sub <
ncopy; ++sub)
2838 arr[
p++] =
v.get<T>();
2840 idname = std::to_string(++
id);
2844 Error(
"ReadFastArray",
"Mismatch array sizes %d %d",
arrsize, (
int)
json->size());
2845 for (
int cnt = 0; cnt <
arrsize; ++cnt)
2846 arr[cnt] =
json->at(cnt).get<T>();
2972 Info(
"ReadFastArray",
"void* n:%d cl:%s",
n, cl->
GetName());
2982 char *obj = (
char *)start;
2992 Info(
"ReadFastArray",
"Indexes ndim:%d totallen:%d",
indexes.NumDimensions(),
indexes.TotalLength());
3012 Info(
"ReadFastArray",
"void** n:%d cl:%s prealloc:%s",
n, cl->
GetName(), (
isPreAlloc ?
"true" :
"false"));
3039 void *old = start[
j];
3045 start[
j] = (
const_cast<TClass *
>(cl))->New();
3053template <
typename T>
3116 }
else if (
nsame > 5) {
3135 if ((
nsame > 1) || (pp -
p0 == 1)) {
3277template <
typename T>
3290 Fatal(
"JsonWriteFastArray",
"Not enough space left in the buffer (1GB limit). %lld elements is greater than the max left of %d",
arrsize,
maxElements);
3308 if (++cnt ==
indexes.GetSize()) {
3455 Info(
"WriteFastArray",
"void *start cl:%s n:%lld", cl ? cl->
GetName() :
"---",
n);
3469 char *obj = (
char *)start;
3509 Info(
"WriteFastArray",
"void **startp cl:%s n:%lld", cl->
GetName(),
n);
3538 start[
j] = (
const_cast<TClass *
>(cl))->New();
3564 Info(
"StreamObject",
"Class: %s", (cl ? cl->
GetName() :
"none"));
3575template <
typename T>
3594 if (!
Stack()->fValues.empty())
3629 if (!
Stack()->fValues.empty())
3681 if (
json->is_null())
3682 val = std::numeric_limits<Float_t>::quiet_NaN();
3693 if (
json->is_null())
3694 val = std::numeric_limits<Double_t>::quiet_NaN();
3704 Error(
"ReadCharP",
"Not implemented");
3738 std::size_t
nch = str.length();
3740 s =
new char[
nch + 1];
3959 if (std::isinf(
value)) {
3961 }
else if (std::isnan(
value)) {
3975 if (std::isinf(
value)) {
3977 }
else if (std::isnan(
value)) {
4061 case 0:
n =
len;
break;
4073 }
else if (
c < 0x80) {
4075 }
else if ((
n <
len - 1) && ((
c & 0xe0) == 0xc0) && ((
value[
n+1] & 0xc0) == 0x80)) {
4076 unsigned code = ((unsigned)
value[
n+1] & 0x3f) | (((unsigned)
c & 0x1f) << 6);
4079 }
else if ((
n <
len - 2) && ((
c & 0xf0) == 0xe0) && ((
value[
n+1] & 0xc0) == 0x80) && ((
value[
n+2] & 0xc0) == 0x80)) {
4080 unsigned code = ((unsigned)
value[
n+2] & 0x3f) | (((unsigned)
value[
n+1] & 0x3f) << 6) | (((
unsigned)
c & 0x0f) << 12);
4083 }
else if ((
n <
len - 3) && ((
c & 0xf8) == 0xf0) && ((
value[
n+1] & 0xc0) == 0x80) && ((
value[
n+2] & 0xc0) == 0x80) && ((
value[
n+3] & 0xc0) == 0x80)) {
4084 unsigned code = ((unsigned)
value[
n+3] & 0x3f) | (((unsigned)
value[
n+2] & 0x3f) << 6) | (((
unsigned)
value[
n+1] & 0x3f) << 12) | (((unsigned)
c & 0x07) << 18);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
unsigned long long ULong64_t
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char mode
const_iterator begin() const
Array of integers (32 bits per element).
void Set(Int_t n) override
Set size of this array to n ints.
JSON array separators for multi-dimensional JSON arrays It fully reproduces array dimensions as in or...
TArrayI & GetIndices()
return array with current index
nlohmann::json * ExtractNode(nlohmann::json *topnode, bool next=true)
Int_t NumDimensions() const
returns number of array dimensions
Int_t TotalLength() const
returns total number of elements in array
TArrayIndexProducer(TDataMember *member, Int_t extradim, const char *separ)
const char * NextSeparator()
increment indexes and returns intermediate or last separator
TArrayIndexProducer(TStreamerElement *elem, Int_t arraylen, const char *separ)
Abstract array base class.
static TString Decode(const char *data)
Decode a base64 string date into a generic TString.
static TString Encode(const char *data)
Transform data into a null terminated base64 string.
void InitMap() override
Create the fMap container and initialize them with the null object.
void MapObject(const TObject *obj, UInt_t offset=1) override
Add object to the fMap container.
Long64_t GetObjectTag(const void *obj)
Returns tag for specified object from objects map (if exists) Returns 0 if object not included into o...
void GetMappedObject(UInt_t tag, void *&ptr, TClass *&ClassPtr) const override
Retrieve the object stored in the buffer's object map at 'tag' Set ptr and ClassPtr respectively to t...
Int_t WriteObjectAny(const void *obj, const TClass *ptrClass, Bool_t cacheReuse=kTRUE) override
Write object to I/O buffer.
Class for serializing object to and from JavaScript Object Notation (JSON) format.
void ReadULong(ULong_t &l) final
Reads ULong_t value from buffer.
void JsonWriteBasic(Char_t value)
converts Char_t to string and add to json value buffer
void WriteShort(Short_t s) final
Writes Short_t value to buffer.
void JsonWriteCollection(TCollection *obj, const TClass *objClass)
store content of ROOT collection
TString fSemicolon
! depending from compression level, " : " or ":"
Int_t fCompact
! 0 - no any compression, 1 - no spaces in the begin, 2 - no new lines, 3 - no spaces at all
void ReadULong64(ULong64_t &l) final
Reads ULong64_t value from buffer.
void WriteStdString(const std::string *s) final
Writes a std::string.
void JsonWriteFastArray(const T *arr, Long64_t arrsize, const char *typname, void(TBufferJSON::*method)(const T *, Int_t, const char *))
Template method to write array of arbitrary dimensions Different methods can be used for store last a...
void * ReadObjectAny(const TClass *clCast) final
Read object from buffer. Only used from TBuffer.
static TObject * ConvertFromJSON(const char *str)
Read TObject-based class from JSON, produced by ConvertToJSON() method.
void ClassBegin(const TClass *, Version_t=-1) final
Should be called in the beginning of custom class streamer.
Int_t JsonReadArray(T *value)
Read static array from JSON - not used.
void IncrementLevel(TVirtualStreamerInfo *) final
Function is called from TStreamerInfo WriteBuffer and ReadBuffer functions and indent new level in js...
void WriteLong(Long_t l) final
Writes Long_t value to buffer.
TString fValue
! buffer for current value
void WriteUInt(UInt_t i) final
Writes UInt_t value to buffer.
void ReadFloat(Float_t &f) final
Reads Float_t value from buffer.
static TString ConvertToJSON(const TObject *obj, Int_t compact=0, const char *member_name=nullptr)
Converts object, inherited from TObject class, to JSON string Lower digit of compact parameter define...
void WriteCharStar(char *s) final
Writes a char*.
void PerformPostProcessing(TJSONStackObj *stack, const TClass *obj_cl=nullptr)
Function is converts TObject and TString structures to more compact representation.
void ReadShort(Short_t &s) final
Reads Short_t value from buffer.
void JsonReadFastArray(T *arr, Int_t arrsize, bool asstring=false)
Template method to read array from the JSON.
TString StoreObject(const void *obj, const TClass *cl)
Store provided object as JSON structure Allows to configure different TBufferJSON properti