AliPhysics  ff1d528 (ff1d528)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliJSONReader.cxx
Go to the documentation of this file.
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 
15  fName(name),
16  fMotherNode(mother),
17  fEntries(),
18  fDaughters(),
19  fOwner(false)
20 {
21 }
22 
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 
35  fEntries.push_back(entry);
36 }
37 
39  AliJSONSyntaxTreeNode *daughter = new AliJSONSyntaxTreeNode(name, this);
40  fDaughters.push_back(daughter);
41  return daughter;
42 }
43 
45  fOwner = owner;
46  for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = fDaughters.begin(); it != fDaughters.end(); it++)
47  (*it)->SetOwner(owner);
48 }
49 
51 
52 }
53 
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  // Delete the syntax tree after being done
118  delete ast;
119  return entries;
120 }
121 
123  TList *entries = new TList;
124  entries->SetName(node->GetName());
125  std::vector<AliJSONData *> &nodeentries = node->GetEntries();
126  std::vector<AliJSONSyntaxTreeNode *> &daughters = node->GetDaughters();
127  for(std::vector<AliJSONData *>::iterator it = nodeentries.begin(); it != nodeentries.end(); it++)
128  entries->Add(*it);
129  for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = daughters.begin(); it != daughters.end(); it++)
130  AddNodeToList(*it, entries);
131  consumer->Add(entries);
132 }
std::vector< AliJSONSyntaxTreeNode * > fDaughters
Definition: AliJSONReader.h:37
AliJSONSyntaxTreeNode(const char *name, AliJSONSyntaxTreeNode *mother)
virtual ~AliJSONReader()
std::vector< AliJSONSyntaxTreeNode * > & GetDaughters()
Definition: AliJSONReader.h:26
std::vector< AliJSONData * > fEntries
Definition: AliJSONReader.h:36
std::vector< AliJSONData * > & GetEntries()
Definition: AliJSONReader.h:27
void AddNodeToList(AliJSONSyntaxTreeNode *node, TList *consumer) const
const char * GetName() const
Definition: AliJSONReader.h:28
AliJSONSyntaxTreeNode * CreateDaughter(const char *name)
TList * Decode(const char *jsosnstring) const
void AddEntry(AliJSONData *entry)
void SetOwner(bool owner=true)