-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonencoder.cpp
57 lines (55 loc) · 1.51 KB
/
jsonencoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "jsonencoder.h"
/**
* @brief Converts a JSON string into URL encoded string.
*
* @param jsonString The JSON format string.
* @return URL encoded string.
*/
String encode(String jsonString){
for(int i=0; i<jsonString.length(); i++){
switch(jsonString.charAt(i)){
case ' ': jsonString[i]='+';
break;
case ':': jsonString[i]='A';
break;
case '{': jsonString[i]='B';
break;
case '}': jsonString[i]='C';
break;
case '"': jsonString[i]='D';
break;
case ',': jsonString[i]='E';
break;
case '.': jsonString[i]='F';
break;
}
}
return jsonString;
}
/**
* @brief Converts an URL encoded string into JSON string.
*
* @param jsonString The URL encoded string.
* @return JSON format string.
*/
String decode(String codedString){
for(int i=0; i<codedString.length(); i++){
switch(codedString.charAt(i)){
case '+': codedString[i]=' ';
break;
case 'A': codedString[i]=':';
break;
case 'B': codedString[i]='{';
break;
case 'C': codedString[i]='}';
break;
case 'D': codedString[i]='"';
break;
case 'E': codedString[i]=',';
break;
case 'F': codedString[i]='.';
break;
}
}
return codedString;
}