]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/Tools/AliJSONReader.cxx
Add the first macro that uses the JSON configuration instead a list of hard-coded...
[u/mrichter/AliRoot.git] / PWG / Tools / AliJSONReader.cxx
1 /*
2  * AliJSONReader.cxx
3  *
4  *  Created on: 06.11.2014
5  *      Author: markusfasel
6  */
7
8 #include <TList.h>
9 #include <TString.h>
10
11 #include "AliJSONData.h"
12 #include "AliJSONReader.h"
13
14 AliJSONSyntaxTreeNode::AliJSONSyntaxTreeNode(const char *name, AliJSONSyntaxTreeNode *mother):
15     fName(name),
16     fMotherNode(mother),
17     fEntries(),
18     fDaughters(),
19     fOwner(false)
20 {
21 }
22
23 AliJSONSyntaxTreeNode:: ~AliJSONSyntaxTreeNode() {
24   if(fOwner){
25     for(std::vector<AliJSONData *>::iterator it = fEntries.begin(); it != fEntries.end(); it++){
26       delete *it;
27     }
28   }
29   for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = fDaughters.begin(); it != fDaughters.end(); it++){
30     delete *it;
31   }
32 }
33
34 void AliJSONSyntaxTreeNode::AddEntry(AliJSONData *entry) {
35   fEntries.push_back(entry);
36 }
37
38 AliJSONSyntaxTreeNode *AliJSONSyntaxTreeNode::CreateDaughter(const char *name){
39   AliJSONSyntaxTreeNode *daughter = new AliJSONSyntaxTreeNode(name, this);
40   fDaughters.push_back(daughter);
41   return daughter;
42 }
43
44 void AliJSONSyntaxTreeNode::SetOwner(bool owner) {
45   fOwner = owner;
46   for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = fDaughters.begin(); it != fDaughters.end(); it++)
47     (*it)->SetOwner(owner);
48 }
49
50 AliJSONReader::AliJSONReader() {
51
52 }
53
54 AliJSONReader::~AliJSONReader() {
55 }
56
57 TList* AliJSONReader::Decode(const char* jsonstring) const {
58   /*
59    * Decode JSON String
60    * 1st create the abstract syntax tree
61    * 2nd serialise the abstract syntax tree it into a TList
62    */
63   AliJSONSyntaxTreeNode * ast = new AliJSONSyntaxTreeNode("", NULL),
64       *current = ast;
65
66   TString jsontstring(jsonstring);
67   jsontstring.ReplaceAll(" ","");
68
69   // First strip away the left and right brackets
70   int first(jsontstring.First('{')+1), last(jsontstring.Last('}'));
71   jsontstring = jsontstring(first, last-first+1);
72   bool hasNext = jsontstring.Length() > 0;
73   while(hasNext){ // Create the abstract syntax tree
74     if(jsontstring[0] == '}'){
75       current = current->GetMotherNode();
76       jsontstring = jsontstring(1, jsontstring.Length()-1);
77       if(!(jsontstring.Length() || current)) hasNext = false;
78       continue;
79     }
80     // Find the next separator
81     int separator = jsontstring.First(':');
82     if(separator == kNPOS){
83       hasNext = false;
84       break;
85     } else{
86       TString key = jsontstring(0,separator-1);
87       key.ReplaceAll("\"", "");
88       jsontstring = jsontstring(separator + 1, jsontstring.Length() - (separator + 1));
89       if(jsontstring[0] == '{'){
90         // Handle complicated object
91         current = current->CreateDaughter(key.Data());
92         jsontstring = jsontstring(1,jsontstring.Length()-1);
93       } else{
94         // Handle simple value
95         separator = jsontstring.First(',');
96         if(separator == kNPOS){
97           separator = jsontstring.First('}');
98         }
99         TString value = jsontstring(0, separator -1);
100         jsontstring = jsontstring(separator+1, jsontstring.Length() - (separator + 1));
101         value.ReplaceAll("\"", "");
102         current->AddEntry(new AliJSONData(key.Data(), value.Data()));
103       }
104     }
105   }
106
107   ast->SetOwner(false);
108
109   // Serialise it into a TList
110   TList *entries = new TList;
111   std::vector<AliJSONData *> &rootnodeentries = ast->GetEntries();
112   for(std::vector<AliJSONData *>::iterator it = rootnodeentries.begin(); it != rootnodeentries.end(); it++)
113     entries->Add(*it);
114   std::vector<AliJSONSyntaxTreeNode *> &daughters = ast->GetDaughters();
115   for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = daughters.begin(); it != daughters.end(); it++)
116     AddNodeToList(*it, entries);
117   return entries;
118 }
119
120 void AliJSONReader::AddNodeToList(AliJSONSyntaxTreeNode* node, TList* consumer) const {
121   TList *entries = new TList;
122   entries->SetName(node->GetName());
123   std::vector<AliJSONData *> &nodeentries = node->GetEntries();
124   std::vector<AliJSONSyntaxTreeNode *> &daughters = node->GetDaughters();
125   for(std::vector<AliJSONData *>::iterator it = nodeentries.begin(); it != nodeentries.end(); it++)
126     entries->Add(*it);
127   for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = daughters.begin(); it != daughters.end(); it++)
128     AddNodeToList(*it, entries);
129   consumer->Add(entries);
130 }