95765ef820b9017f668490e76bb650c2a1fafce5
[ric-app/admin.git] / src / json / json_handler.hpp
1
2 /*
3 ==================================================================================
4
5         Copyright (c) 2018-2019 AT&T Intellectual Property.
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20 /*
21  Author : Ashwin Sridharan
22
23   
24 */
25
26 #pragma once
27 #ifndef JSON_HANDLER
28 #define JSON_HANDLER
29
30 #include <rapidjson/document.h>
31 #include <rapidjson/writer.h>
32 #include <rapidjson/stringbuffer.h>
33 #include <rapidjson/schema.h>
34 #include <iostream>
35 #include <unordered_map>
36 #include <map>
37 #include <stdio.h>
38 #include <memory>
39 #include <vector>
40 #include <mdclog/mdclog.h>
41 #include <queue>
42
43
44 #define MAX_QUEUE_SIZE 100
45
46 using namespace rapidjson;
47
48
49 struct DataContainer {  
50   enum  Types {boolean, integer, uinteger, big_integer, ubig_integer, real, str} tag ;    
51   struct  {
52     union  {
53       bool b;
54       int i;
55       unsigned int ui;
56       long int l;
57       unsigned long int ul;
58       double f;
59     } ;
60     std::string s;
61   } value;  
62 };
63   
64 class TrieNode;
65
66 class TrieNode{
67 public:
68
69   TrieNode (int);
70   TrieNode( std::string );
71
72   void set_type(DataContainer::Types);
73   int get_type(void) const {return _val.tag;} ;
74
75   void set_value(bool val); 
76   void set_value(int val);
77   void set_value(unsigned int val);
78   
79   void set_value(long  int );
80   void set_value(unsigned long int );
81   void set_value(double val);
82   void set_value(const char *);
83   void set_value(std::string val);
84   void set_value(const char * , size_t );
85
86   void print_id(void);
87   void print_value(void);
88   void add_child(TrieNode *);
89   
90   DataContainer const * get_value(void) const  { return & _val; };
91   DataContainer const *  get_id(void) const  { return & _id;};
92
93   std::vector<TrieNode *> &   get_children(void){ return _children;} ;
94   std::string &  name(void) {return _name; };
95   bool is_child(void){  return _children.size() ? false : true; };
96   
97 private:
98   std::vector<TrieNode *> _children;
99   int val_type;
100   std::string _name;
101   DataContainer _id;
102   DataContainer _val;
103 };
104   
105 class jsonHandler {
106
107 public:
108   jsonHandler(void);
109
110   
111   void load_schema(std::string);
112   void load_schema(std::string, TrieNode *root);
113   void load_schema(std::string , std::vector<std::string> & );
114
115   void load_buffer(std::string);
116   void  load_buffer(std::string, TrieNode * );
117   std::string get_buffer(void);
118
119   int get_values(const  char *, int , std::string & , TrieNode *, std::vector<TrieNode *> & );
120   int get_values( std::string & , TrieNode *, std::vector<TrieNode *> & );
121   
122   int set_values (const char *, int, std::string & , std::vector<TrieNode *>);
123   int set_values (std::string & , std::vector<TrieNode *>);
124   
125   bool is_valid(const char *, int, std::string &);
126   
127 private:
128
129   void load_file(std::string, std::string &);
130
131   bool traverse_doc(Value &, TrieNode *, bool, std::string &, std::vector<TrieNode *> & );
132
133   bool find_member(const std::string, std::string &, std::vector<std::string> &, Value & );
134
135   bool find_member(const std::string, std::string &, TrieNode *, Value &);
136   bool find_member(Value &,  std::string &, TrieNode *, Value &);
137
138   bool add_array_objects(std::queue<Value> &, Value &);
139
140   bool _is_root, _is_schema, _is_buffer;
141
142   std::unique_ptr<SchemaDocument> _ref_schema_doc;
143   std::map <std::string, DataContainer> _key_value_pairs;
144   std::string _contents;
145   std::string _buffer;
146   
147 };
148
149
150 #endif