-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathParser.java
More file actions
171 lines (144 loc) · 6.83 KB
/
Parser.java
File metadata and controls
171 lines (144 loc) · 6.83 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.kamwithk.ankiconnectandroid.request_parsers;
import android.util.Base64;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Parser {
public static Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
public static Gson gsonNoSerialize = new GsonBuilder().setPrettyPrinting().create();
public static JsonObject parse(String raw_data) {
// Use a more lenient parser that accepts both numeric and string version fields
com.google.gson.stream.JsonReader reader = new com.google.gson.stream.JsonReader(
new java.io.StringReader(raw_data)
);
reader.setLenient(true);
return JsonParser.parseReader(reader).getAsJsonObject();
}
public static String get_action(JsonObject data) {
return data.get("action").getAsString();
}
public static int get_version(JsonObject data, int fallback) {
if (data.has("version")) {
JsonElement versionElement = data.get("version");
// Handle both string and numeric version fields
if (versionElement.isJsonPrimitive()) {
com.google.gson.JsonPrimitive primitive = versionElement.getAsJsonPrimitive();
if (primitive.isNumber()) {
return primitive.getAsInt();
} else if (primitive.isString()) {
try {
return Integer.parseInt(primitive.getAsString());
} catch (NumberFormatException e) {
return fallback;
}
}
}
}
return fallback;
}
public static String getDeckName(JsonObject raw_data) {
return raw_data.get("params").getAsJsonObject().get("note").getAsJsonObject().get("deckName").getAsString();
}
public static String getModelName(JsonObject raw_data) {
return raw_data.get("params").getAsJsonObject().get("note").getAsJsonObject().get("modelName").getAsString();
}
public static String getModelNameFromParam(JsonObject raw_data) {
return raw_data.get("params").getAsJsonObject().get("modelName").getAsString();
}
public static Map<String, String> getNoteValues(JsonObject raw_data) {
Type fieldType = new TypeToken<Map<String, String>>() {}.getType();
return gson.fromJson(raw_data.get("params").getAsJsonObject().get("note").getAsJsonObject().get("fields"), fieldType);
}
public static Set<String> getNoteTags(JsonObject raw_data) {
Type fieldType = new TypeToken<Set<String>>() {}.getType();
return gson.fromJson(raw_data.get("params").getAsJsonObject().get("note").getAsJsonObject().get("tags"), fieldType);
}
public static String getNoteQuery(JsonObject raw_data) {
return raw_data.get("params").getAsJsonObject().get("query").getAsString();
}
public static long getUpdateNoteFieldsId(JsonObject raw_data) {
return raw_data.get("params").getAsJsonObject().get("note").getAsJsonObject().get("id").getAsLong();
}
public static Map<String, String> getUpdateNoteFieldsFields(JsonObject raw_data) {
Type fieldType = new TypeToken<Map<String, String>>() {}.getType();
return gson.fromJson(raw_data.get("params").getAsJsonObject().get("note").getAsJsonObject().get("fields"), fieldType);
}
/**
* For each key ("audio", "video", "picture"), expect EITHER a list or singular json object!
* According to the official Anki-Connect docs:
* > If you choose to include [audio, video, picture keys], they should contain a single object
* > or an array of objects
*/
public static ArrayList<MediaRequest> getNoteMediaRequests(JsonObject raw_data) {
Map<String, MediaRequest.MediaType> media_types = Map.of(
"audio", MediaRequest.MediaType.AUDIO,
"video", MediaRequest.MediaType.VIDEO,
"picture", MediaRequest.MediaType.PICTURE
);
JsonObject note_json = raw_data.get("params").getAsJsonObject().get("note").getAsJsonObject();
ArrayList<MediaRequest> request_medias = new ArrayList<>();
for (Map.Entry<String, MediaRequest.MediaType> entry: media_types.entrySet()) {
JsonElement media_value = note_json.get(entry.getKey());
if (media_value == null) {
continue;
}
if (media_value.isJsonArray()) {
for (JsonElement media_element: media_value.getAsJsonArray()) {
JsonObject media_object = media_element.getAsJsonObject();
MediaRequest requestMedia = MediaRequest.fromJson(media_object, entry.getValue());
request_medias.add(requestMedia);
}
} else if (media_value.isJsonObject()) {
JsonObject media_object = media_value.getAsJsonObject();
MediaRequest requestMedia = MediaRequest.fromJson(media_object, entry.getValue());
request_medias.add(requestMedia);
}
}
return request_medias;
}
/**
* Gets the first field of the note
*/
public static ArrayList<NoteRequest> getNoteFront(JsonObject raw_data) {
JsonArray notes = raw_data.get("params").getAsJsonObject().get("notes").getAsJsonArray();
ArrayList<NoteRequest> projections = new ArrayList<>();
for (JsonElement jsonElement : notes) {
projections.add(NoteRequest.fromJson(jsonElement));
}
return projections;
}
public static boolean[] getNoteTrues(JsonObject raw_data) {
int num_notes = raw_data.get("params").getAsJsonObject().get("notes").getAsJsonArray().size();
boolean[] array = new boolean[num_notes];
Arrays.fill(array, true);
return array;
}
public static ArrayList<Long> getNoteIds(JsonObject raw_data) {
ArrayList<Long> noteIds = new ArrayList<>();
JsonArray jsonNoteIds = raw_data.get("params").getAsJsonObject().get("notes").getAsJsonArray();
for(JsonElement noteId: jsonNoteIds) {
noteIds.add(noteId.getAsLong());
}
return noteIds;
}
public static String getMediaFilename(JsonObject raw_data) {
return raw_data.get("params").getAsJsonObject().get("filename").getAsString();
}
public static byte[] getMediaData(JsonObject raw_data) {
String encoded = raw_data.get("params").getAsJsonObject().get("data").getAsString();
return Base64.decode(encoded, Base64.DEFAULT);
}
public static JsonArray getMultiActions(JsonObject raw_data) {
return raw_data.get("params").getAsJsonObject().get("actions").getAsJsonArray();
}
}