]>
Commit | Line | Data |
---|---|---|
1cee71ae | 1 | /* |
c4109efb | 2 | * AliJSONReader.cxx |
1cee71ae | 3 | * |
4 | * Created on: 06.11.2014 | |
5 | * Author: markusfasel | |
6 | */ | |
68d39387 | 7 | |
1cee71ae | 8 | #include <TList.h> |
9 | #include <TString.h> | |
10 | ||
c4109efb | 11 | #include "AliJSONData.h" |
12 | #include "AliJSONReader.h" | |
97bf0336 | 13 | |
c4109efb | 14 | AliJSONSyntaxTreeNode::AliJSONSyntaxTreeNode(const char *name, AliJSONSyntaxTreeNode *mother): |
97bf0336 | 15 | fName(name), |
16 | fMotherNode(mother), | |
17 | fEntries(), | |
18 | fDaughters(), | |
19 | fOwner(false) | |
20 | { | |
21 | } | |
22 | ||
c4109efb | 23 | AliJSONSyntaxTreeNode:: ~AliJSONSyntaxTreeNode() { |
97bf0336 | 24 | if(fOwner){ |
c4109efb | 25 | for(std::vector<AliJSONData *>::iterator it = fEntries.begin(); it != fEntries.end(); it++){ |
97bf0336 | 26 | delete *it; |
27 | } | |
28 | } | |
c4109efb | 29 | for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = fDaughters.begin(); it != fDaughters.end(); it++){ |
97bf0336 | 30 | delete *it; |
31 | } | |
32 | } | |
33 | ||
c4109efb | 34 | void AliJSONSyntaxTreeNode::AddEntry(AliJSONData *entry) { |
97bf0336 | 35 | fEntries.push_back(entry); |
36 | } | |
37 | ||
c4109efb | 38 | AliJSONSyntaxTreeNode *AliJSONSyntaxTreeNode::CreateDaughter(const char *name){ |
39 | AliJSONSyntaxTreeNode *daughter = new AliJSONSyntaxTreeNode(name, this); | |
97bf0336 | 40 | fDaughters.push_back(daughter); |
41 | return daughter; | |
42 | } | |
43 | ||
c4109efb | 44 | void AliJSONSyntaxTreeNode::SetOwner(bool owner) { |
97bf0336 | 45 | fOwner = owner; |
c4109efb | 46 | for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = fDaughters.begin(); it != fDaughters.end(); it++) |
97bf0336 | 47 | (*it)->SetOwner(owner); |
48 | } | |
1cee71ae | 49 | |
c4109efb | 50 | AliJSONReader::AliJSONReader() { |
1cee71ae | 51 | |
52 | } | |
53 | ||
c4109efb | 54 | AliJSONReader::~AliJSONReader() { |
1cee71ae | 55 | } |
56 | ||
c4109efb | 57 | TList* AliJSONReader::Decode(const char* jsonstring) const { |
1cee71ae | 58 | /* |
59 | * Decode JSON String | |
60 | * 1st create the abstract syntax tree | |
61 | * 2nd serialise the abstract syntax tree it into a TList | |
62 | */ | |
c4109efb | 63 | AliJSONSyntaxTreeNode * ast = new AliJSONSyntaxTreeNode("", NULL), |
1cee71ae | 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("\"", ""); |
c4109efb | 102 | current->AddEntry(new AliJSONData(key.Data(), value.Data())); |
1cee71ae | 103 | } |
104 | } | |
105 | } | |
106 | ||
107 | ast->SetOwner(false); | |
108 | ||
109 | // Serialise it into a TList | |
110 | TList *entries = new TList; | |
c4109efb | 111 | std::vector<AliJSONData *> &rootnodeentries = ast->GetEntries(); |
112 | for(std::vector<AliJSONData *>::iterator it = rootnodeentries.begin(); it != rootnodeentries.end(); it++) | |
1cee71ae | 113 | entries->Add(*it); |
c4109efb | 114 | std::vector<AliJSONSyntaxTreeNode *> &daughters = ast->GetDaughters(); |
115 | for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = daughters.begin(); it != daughters.end(); it++) | |
1cee71ae | 116 | AddNodeToList(*it, entries); |
3ee2eef5 | 117 | // Delete the syntax tree after being done |
118 | delete ast; | |
1cee71ae | 119 | return entries; |
120 | } | |
121 | ||
c4109efb | 122 | void AliJSONReader::AddNodeToList(AliJSONSyntaxTreeNode* node, TList* consumer) const { |
1cee71ae | 123 | TList *entries = new TList; |
124 | entries->SetName(node->GetName()); | |
c4109efb | 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++) | |
1cee71ae | 128 | entries->Add(*it); |
c4109efb | 129 | for(std::vector<AliJSONSyntaxTreeNode *>::iterator it = daughters.begin(); it != daughters.end(); it++) |
1cee71ae | 130 | AddNodeToList(*it, entries); |
131 | consumer->Add(entries); | |
132 | } |