Line data Source code
1 : /*
2 : * JsonParser.cpp
3 : *
4 : * Created on: Jun 16, 2016
5 : * Author: tobias
6 : */
7 :
8 : #include "JsonParser.h"
9 :
10 : using namespace std;
11 :
12 2457 : JsonParser::JsonParser() {
13 : // TODO Auto-generated constructor stub
14 :
15 2457 : }
16 112 : bool JsonParser::parsing(string json_object){
17 112 : return reader.parse(json_object, root, false);
18 : }
19 :
20 1118 : void JsonParser::makeInterests(Json::Value json_interests,Interests &interests){
21 1118 : Json::Value array;
22 2236 : JsonParser json;
23 1118 : if (! json_interests.isArray()){
24 0 : array = json_interests[INTERESTS_KEY];
25 : }else{
26 1118 : array = json_interests;
27 : }
28 :
29 1118 : Json::Value::iterator it = array.begin();
30 3919 : for (; it != array.end(); ++it){
31 2801 : Json::Value interest = (*it);
32 2801 : interests.add(interest[CATEGORY_KEY].asString(), interest[VALUE_KEY].asString());
33 3919 : }
34 :
35 :
36 1118 : }
37 :
38 1118 : void JsonParser::makeUser(Json::Value json_user,User &user){
39 1118 : Json::Value location = json_user[LOCATION_KEY];
40 2236 : Json::Value json_interests = json_user[INTERESTS_KEY];
41 2236 : JsonParser pars;
42 1118 : if (json_user[PHOTO_KEY].isNull()){
43 17 : user.setPhoto("");
44 : }else{
45 1101 : user.setPhoto(json_user[PHOTO_KEY].asString());
46 : }
47 :
48 :
49 2236 : Interests interests;
50 1118 : this->makeInterests(json_interests, interests);
51 1118 : user.setInterests(interests);
52 :
53 1118 : user.setAlias(json_user[ALIAS_KEY].asString());
54 :
55 1118 : user.setMail(json_user[MAIL_KEY].asString());
56 :
57 1118 : user.setName(json_user[NAME_KEY].asString());
58 :
59 1118 : user.setSex(json_user[SEX_KEY].asString());
60 :
61 1118 : if (json_user.isMember(ID_KEY)){
62 :
63 1113 : user.setId(json_user[ID_KEY].asInt());
64 : }
65 :
66 1118 : user.setAge(json_user[AGE_KEY].asInt());
67 2236 : user.setLocation(location[LATITUDE_KEY].asFloat(), location[LONGITUDE_KEY].asFloat());
68 :
69 1118 : }
70 :
71 42 : Json::Value JsonParser::interestsToJson(Interests* interests){
72 42 : Json::Value interests_json(Json::arrayValue);
73 84 : map<string,vector<string>> inter = interests->allInterests();
74 42 : map<string,vector<string>>::iterator it = inter.begin();
75 :
76 101 : for(; it != inter.end(); ++it){
77 59 : string key = it->first;
78 128 : for (unsigned int i = 0; i < it->second.size(); i++){
79 69 : Json::Value pair;
80 138 : string value = it->second[i];
81 69 : pair[CATEGORY_KEY] = key;
82 69 : pair[VALUE_KEY] = value;
83 69 : interests_json.append(pair);
84 69 : }
85 59 : }
86 :
87 84 : return interests_json;
88 : }
89 :
90 42 : Json::Value JsonParser::userToJson(User *user, bool id){
91 42 : Json::Value user_json;
92 84 : Json::Value location(Json::objectValue);
93 :
94 42 : if (id){
95 22 : user_json[ID_KEY] = user->getID();
96 : }
97 42 : user_json[ALIAS_KEY] = user->getAlias();
98 42 : user_json[NAME_KEY] = user->getName();
99 42 : user_json[MAIL_KEY] = user->getMail();
100 42 : user_json[PHOTO_KEY] = user->getPhoto();
101 42 : user_json[SEX_KEY] = user->getSex();
102 42 : user_json[AGE_KEY] = user->getAge();
103 42 : user_json[INTERESTS_KEY] = interestsToJson(user->getInterests());
104 42 : location[LATITUDE_KEY] = user->getX();
105 42 : location[LONGITUDE_KEY] = user->getY();
106 42 : user_json[LOCATION_KEY] = location;
107 84 : return user_json;
108 : }
109 :
110 12 : string JsonParser::photoToJson(string *photo){
111 12 : Json::Value json;
112 12 : json[PHOTO_PUT_KEY] = *photo;
113 12 : return writer.write(json);
114 : }
115 :
116 205 : Json::Value JsonParser::getValue( string key ){
117 205 : return root[key];
118 : };
119 :
120 65 : string JsonParser::getAsString(Json::Value value){
121 65 : return writer.write(value);
122 : }
123 2457 : JsonParser::~JsonParser() {
124 : // TODO Auto-generated destructor stub
125 2523 : }
126 :
|