Skip to content

Commit 3d8ae10

Browse files
author
jantje
committed
Library json is working
1 parent c0eba84 commit 3d8ae10

File tree

8 files changed

+333
-326
lines changed

8 files changed

+333
-326
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package io.sloeber.core.Gson;
2+
3+
import java.lang.reflect.Type;
4+
5+
import com.google.gson.JsonDeserializationContext;
6+
import com.google.gson.JsonDeserializer;
7+
import com.google.gson.JsonElement;
8+
import com.google.gson.JsonObject;
9+
import com.google.gson.JsonParseException;
10+
11+
public class LibraryDeserializer implements JsonDeserializer<LibraryJson> {
12+
13+
@SuppressWarnings("nls")
14+
@Override
15+
public LibraryJson deserialize(JsonElement json, Type jsonType, JsonDeserializationContext arg2)
16+
throws JsonParseException {
17+
JsonObject jsonObject = json.getAsJsonObject();
18+
LibraryJson lib = new LibraryJson();
19+
try {
20+
lib.name = getSafeString(jsonObject, "name");
21+
lib.version = getSafeString(jsonObject, "version");
22+
lib.author = getSafeString(jsonObject, "author");
23+
lib.maintainer = getSafeString(jsonObject, "maintainer");
24+
lib.sentence = getSafeString(jsonObject, "sentence");
25+
lib.paragraph = getSafeString(jsonObject, "paragraph");
26+
lib.website = getSafeString(jsonObject, "website");
27+
lib.category = getSafeString(jsonObject, "category");
28+
for (JsonElement curType : jsonObject.get("architectures").getAsJsonArray()) {
29+
lib.architectures.add(curType.getAsString());
30+
}
31+
for (JsonElement curType : jsonObject.get("types").getAsJsonArray()) {
32+
lib.types.add(curType.getAsString());
33+
}
34+
lib.url = getSafeString(jsonObject, "url");
35+
lib.archiveFileName = getSafeString(jsonObject, "archiveFileName");
36+
lib.size = jsonObject.get("size").getAsInt();
37+
lib.checksum = getSafeString(jsonObject, "checksum");
38+
} catch (Exception e) {
39+
throw new JsonParseException("failed to parse json " + e.getMessage());
40+
}
41+
42+
return lib;
43+
}
44+
45+
private static String getSafeString(JsonObject jsonObject, String fieldName) {
46+
JsonElement field = jsonObject.get(fieldName);
47+
if (field == null) {
48+
return "no info found in file"; //$NON-NLS-1$
49+
}
50+
return field.getAsString();
51+
52+
}
53+
54+
}

io.sloeber.core/src/io/sloeber/core/managers/LibraryIndex.java renamed to io.sloeber.core/src/io/sloeber/core/Gson/LibraryIndexJson.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.sloeber.core.managers;
1+
package io.sloeber.core.Gson;
22

33
import java.io.File;
44
import java.util.ArrayList;
@@ -21,18 +21,18 @@
2121
* @author jan
2222
*
2323
*/
24-
public class LibraryIndex {
24+
public class LibraryIndexJson {
2525
private String jsonFileName;
26-
private List<Library> libraries;
26+
private List<LibraryJson> libraries;
2727

2828
// category name to library name
2929
private Map<String, Set<String>> categories = new HashMap<>();
3030

3131
// library name to latest version of library
32-
private Map<String, Library> latestLibs = new HashMap<>();
32+
private Map<String, LibraryJson> latestLibs = new HashMap<>();
3333

3434
public void resolve() {
35-
for (Library library : this.libraries) {
35+
for (LibraryJson library : this.libraries) {
3636
String name = library.getName();
3737

3838
String category = library.getCategory();
@@ -47,7 +47,7 @@ public void resolve() {
4747
}
4848
categoryLibs.add(name);
4949

50-
Library current = this.latestLibs.get(name);
50+
LibraryJson current = this.latestLibs.get(name);
5151
if (current != null) {
5252
if (Version.compare(library.getVersion(), current.getVersion()) > 0) {
5353
this.latestLibs.put(name, library);
@@ -58,21 +58,21 @@ public void resolve() {
5858
}
5959
}
6060

61-
public Library getLatestLibrary(String name) {
61+
public LibraryJson getLatestLibrary(String name) {
6262
return this.latestLibs.get(name);
6363
}
6464

65-
public Library getLibrary(String libName, String version) {
66-
for (Library library : this.libraries) {
65+
public LibraryJson getLibrary(String libName, String version) {
66+
for (LibraryJson library : this.libraries) {
6767
if (library.getName().equals(libName) && (library.getVersion().equals(version))) {
6868
return library;
6969
}
7070
}
7171
return null;
7272
}
7373

74-
public Library getInstalledLibrary(String libName) {
75-
for (Library library : this.libraries) {
74+
public LibraryJson getInstalledLibrary(String libName) {
75+
for (LibraryJson library : this.libraries) {
7676
if (library.getName().equals(libName) && library.isInstalled()) {
7777
return library;
7878
}
@@ -84,20 +84,20 @@ public Set<String> getCategories() {
8484
return this.categories.keySet();
8585
}
8686

87-
public Collection<Library> getLatestLibraries(String category) {
87+
public Collection<LibraryJson> getLatestLibraries(String category) {
8888
Set<String> categoryLibs = this.categories.get(category);
8989
if (categoryLibs == null) {
9090
return new ArrayList<>(0);
9191
}
9292

93-
List<Library> libs = new ArrayList<>(categoryLibs.size());
93+
List<LibraryJson> libs = new ArrayList<>(categoryLibs.size());
9494
for (String name : categoryLibs) {
9595
libs.add(this.latestLibs.get(name));
9696
}
9797
return libs;
9898
}
9999

100-
public Map<String, Library> getLatestLibraries() {
100+
public Map<String, LibraryJson> getLatestLibraries() {
101101
return this.latestLibs;
102102
}
103103

@@ -110,22 +110,22 @@ public Map<String, Library> getLatestLibraries() {
110110
*/
111111
public Map<String, LibraryDescriptor> getLatestInstallableLibraries() {
112112
Map<String, LibraryDescriptor> ret = new HashMap<>();
113-
for (Entry<String, Library> curLibrary : this.latestLibs.entrySet()) {
113+
for (Entry<String, LibraryJson> curLibrary : this.latestLibs.entrySet()) {
114114
if (!curLibrary.getValue().isAVersionInstalled()) {
115115
ret.put(curLibrary.getKey(),new LibraryDescriptor( curLibrary.getValue()));
116116
}
117117
}
118118
return ret;
119119
}
120120

121-
public Collection<Library> getLibraries(String category) {
121+
public Collection<LibraryJson> getLibraries(String category) {
122122
Set<String> categoryLibs = this.categories.get(category);
123123
if (categoryLibs == null) {
124124
return new ArrayList<>(0);
125125
}
126126

127-
List<Library> libs = new ArrayList<>(categoryLibs.size());
128-
for (Library curLibrary : this.libraries) {
127+
List<LibraryJson> libs = new ArrayList<>(categoryLibs.size());
128+
for (LibraryJson curLibrary : this.libraries) {
129129
if (categoryLibs.contains(curLibrary.getName())) {
130130
libs.add(curLibrary);
131131
}
@@ -159,7 +159,7 @@ public Map<String, LibraryDescriptor> getLatestInstallableLibraries(Set<String>
159159
if (libNames.isEmpty()) {
160160
return ret;
161161
}
162-
for (Entry<String, Library> curLibrary : this.latestLibs.entrySet()) {
162+
for (Entry<String, LibraryJson> curLibrary : this.latestLibs.entrySet()) {
163163
if (libNames.contains(curLibrary.getKey())) {
164164
if (!curLibrary.getValue().isAVersionInstalled()) {
165165
ret.put(curLibrary.getKey(), new LibraryDescriptor(curLibrary.getValue()));

0 commit comments

Comments
 (0)