]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG/EMCAL/AliEMCALJSONReader.cxx
Bug fixes in the JSON Parser + add print functions to the configuration object
[u/mrichter/AliRoot.git] / PWG / EMCAL / AliEMCALJSONReader.cxx
CommitLineData
1cee71ae 1/*
2 * AliEMCALJSONReader.cxx
3 *
4 * Created on: 06.11.2014
5 * Author: markusfasel
6 */
68d39387 7
1cee71ae 8#include <TList.h>
9#include <TString.h>
10
97bf0336 11#include "AliEMCALConfigurationObject.h"
12#include "AliEMCALJSONReader.h"
13
14AliEMCALJSONSyntaxTreeNode::AliEMCALJSONSyntaxTreeNode(const char *name, AliEMCALJSONSyntaxTreeNode *mother):
15 fName(name),
16 fMotherNode(mother),
17 fEntries(),
18 fDaughters(),
19 fOwner(false)
20{
21}
22
23AliEMCALJSONSyntaxTreeNode:: ~AliEMCALJSONSyntaxTreeNode() {
24 if(fOwner){
25 for(std::vector<AliEMCALConfigurationObject *>::iterator it = fEntries.begin(); it != fEntries.end(); it++){
26 delete *it;
27 }
28 }
29 for(std::vector<AliEMCALJSONSyntaxTreeNode *>::iterator it = fDaughters.begin(); it != fDaughters.end(); it++){
30 delete *it;
31 }
32}
33
34void AliEMCALJSONSyntaxTreeNode::AddEntry(AliEMCALConfigurationObject *entry) {
35 fEntries.push_back(entry);
36}
37
38AliEMCALJSONSyntaxTreeNode *AliEMCALJSONSyntaxTreeNode::CreateDaughter(const char *name){
39 AliEMCALJSONSyntaxTreeNode *daughter = new AliEMCALJSONSyntaxTreeNode(name, this);
40 fDaughters.push_back(daughter);
41 return daughter;
42}
43
44void AliEMCALJSONSyntaxTreeNode::SetOwner(bool owner) {
45 fOwner = owner;
46 for(std::vector<AliEMCALJSONSyntaxTreeNode *>::iterator it = fDaughters.begin(); it != fDaughters.end(); it++)
47 (*it)->SetOwner(owner);
48}
1cee71ae 49
50AliEMCALJSONReader::AliEMCALJSONReader() {
1cee71ae 51
52}
53
54AliEMCALJSONReader::~AliEMCALJSONReader() {
1cee71ae 55}
56
57TList* AliEMCALJSONReader::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 AliEMCALJSONSyntaxTreeNode * ast = new AliEMCALJSONSyntaxTreeNode("", NULL),
64 *current = ast;
65
66 TString jsontstring(jsonstring);
67 jsontstring.ReplaceAll(" ","");
68
69 // First strip away the left and right brackets
68d39387 70 int first(jsontstring.First('{')+1), last(jsontstring.Last('}'));
71 jsontstring = jsontstring(first, last-first+1);
1cee71ae 72 bool hasNext = jsontstring.Length() > 0;
73 while(hasNext){ // Create the abstract syntax tree
74 if(jsontstring[0] == '}'){
75 current = current->GetMotherNode();
97bf0336 76 jsontstring = jsontstring(1, jsontstring.Length()-1);
1cee71ae 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));
97bf0336 89 if(jsontstring[0] == '{'){
1cee71ae 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(',');
68d39387 96 if(separator == kNPOS){
97 separator = jsontstring.First('}');
98 }
1cee71ae 99 TString value = jsontstring(0, separator -1);
97bf0336 100 jsontstring = jsontstring(separator+1, jsontstring.Length() - (separator + 1));
1cee71ae 101 value.ReplaceAll("\"", "");
102 current->AddEntry(new AliEMCALConfigurationObject(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<AliEMCALConfigurationObject *> &rootnodeentries = ast->GetEntries();
112 for(std::vector<AliEMCALConfigurationObject *>::iterator it = rootnodeentries.begin(); it != rootnodeentries.end(); it++)
113 entries->Add(*it);
114 std::vector<AliEMCALJSONSyntaxTreeNode *> &daughters = ast->GetDaughters();
115 for(std::vector<AliEMCALJSONSyntaxTreeNode *>::iterator it = daughters.begin(); it != daughters.end(); it++)
116 AddNodeToList(*it, entries);
117 return entries;
118}
119
120void AliEMCALJSONReader::AddNodeToList(AliEMCALJSONSyntaxTreeNode* node, TList* consumer) const {
121 TList *entries = new TList;
122 entries->SetName(node->GetName());
123 std::vector<AliEMCALConfigurationObject *> &nodeentries = node->GetEntries();
124 std::vector<AliEMCALJSONSyntaxTreeNode *> &daughters = node->GetDaughters();
125 for(std::vector<AliEMCALConfigurationObject *>::iterator it = nodeentries.begin(); it != nodeentries.end(); it++)
126 entries->Add(*it);
127 for(std::vector<AliEMCALJSONSyntaxTreeNode *>::iterator it = daughters.begin(); it != daughters.end(); it++)
128 AddNodeToList(*it, entries);
129 consumer->Add(entries);
130}