Skip to content

Commit cfbaf4c

Browse files
linevmueschel
authored andcommitted
json: special handling for std::map in TBufferJSON
1 parent 7627fb7 commit cfbaf4c

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

net/http/src/TBufferJSON.cxx

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -793,39 +793,41 @@ void TBufferJSON::JsonWriteObject(const void *obj, const TClass *cl, Bool_t chec
793793
}
794794

795795
} else {
796-
// write like blob values, but skipping first element with size
797-
// TODO: make special handling for std::map, should pack each each pair in separate object
798796
const char* separ = "[";
799-
TString blob;
800797

801-
for (Int_t k=1;k<=stack->fValues.GetLast();k++) {
802-
blob.Append(separ); separ = fArraySepar.Data();
803-
blob.Append(stack->fValues.At(k)->GetName());
798+
if (fValue.Length() > 0) {
799+
stack->fValues.Add(new TObjString(fValue));
800+
fValue.Clear();
804801
}
805802

806-
if (fValue.Length()>0) {
807-
blob.Append(separ);
808-
blob.Append(fValue);
803+
Int_t size = TString(stack->fValues.At(0)->GetName()).Atoi();
804+
805+
if ((size*2 == stack->fValues.GetLast()) &&
806+
((special_kind==TClassEdit::kMap) || (special_kind==TClassEdit::kMultiMap))) {
807+
// special handling for std::map. Create entries like { 'first' : key, 'second' : value }
808+
for (Int_t k=1;k<stack->fValues.GetLast();k+=2) {
809+
fValue.Append(separ); separ = fArraySepar.Data();
810+
fValue.Append("{");
811+
fValue.Append("\"first\"");
812+
fValue.Append(fSemicolon);
813+
fValue.Append(stack->fValues.At(k)->GetName());
814+
fValue.Append(fArraySepar);
815+
fValue.Append("\"second\"");
816+
fValue.Append(fSemicolon);
817+
fValue.Append(stack->fValues.At(k+1)->GetName());
818+
fValue.Append("}");
819+
}
820+
} else {
821+
// for most stl containers write just like blob, but skipping first element with size
822+
for (Int_t k=1;k<=stack->fValues.GetLast();k++) {
823+
fValue.Append(separ); separ = fArraySepar.Data();
824+
fValue.Append(stack->fValues.At(k)->GetName());
825+
}
809826
}
810827

811-
blob.Append("]");
828+
fValue.Append("]");
812829
stack->fValues.Delete();
813-
fValue = blob;
814-
}
815-
816-
/*
817-
switch(isstlcont) {
818-
case TClassEdit::kVector : break;
819-
case TClassEdit::kList : break;
820-
case TClassEdit::kDeque : break;
821-
case TClassEdit::kMap : break;
822-
case TClassEdit::kMultiMap : break;
823-
case TClassEdit::kSet : break;
824-
case TClassEdit::kMultiSet : break;
825-
case TClassEdit::kBitSet : break;
826-
default: break;
827830
}
828-
*/
829831
}
830832

831833
if ((special_kind==0) &&

0 commit comments

Comments
 (0)