]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/cli/JsonSignedPreKeyStore.java
Make Json store use Jackson instead of Gson (as it's already linked)
[signal-cli] / src / main / java / cli / JsonSignedPreKeyStore.java
index 000a0aa41fa2f9f4a457e524c8c5838abb949b56..9b48fa97034763b865e01ad4450081c6bcf33212 100644 (file)
@@ -1,7 +1,9 @@
 package cli;
 
-import org.json.JSONArray;
-import org.json.JSONObject;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.*;
 import org.whispersystems.libaxolotl.InvalidKeyIdException;
 import org.whispersystems.libaxolotl.state.SignedPreKeyRecord;
 import org.whispersystems.libaxolotl.state.SignedPreKeyStore;
@@ -12,7 +14,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
-public class JsonSignedPreKeyStore implements SignedPreKeyStore {
+class JsonSignedPreKeyStore implements SignedPreKeyStore {
 
     private final Map<Integer, byte[]> store = new HashMap<>();
 
@@ -20,23 +22,9 @@ public class JsonSignedPreKeyStore implements SignedPreKeyStore {
 
     }
 
-    public JsonSignedPreKeyStore(JSONArray list) throws IOException {
-        for (int i = 0; i < list.length(); i++) {
-            JSONObject k = list.getJSONObject(i);
-            try {
-                store.put(k.getInt("id"), Base64.decode(k.getString("record")));
-            } catch (IOException e) {
-                System.out.println("Error while decoding prekey for: " + k.getString("name"));
-            }
-        }
-    }
 
-    public JSONArray getJson() {
-        JSONArray result = new JSONArray();
-        for (Integer id : store.keySet()) {
-            result.put(new JSONObject().put("id", id.toString()).put("record", store.get(id)));
-        }
-        return result;
+    public void addSignedPreKeys(Map<Integer, byte[]> preKeys) {
+        store.putAll(preKeys);
     }
 
     @Override
@@ -81,4 +69,46 @@ public class JsonSignedPreKeyStore implements SignedPreKeyStore {
     public void removeSignedPreKey(int signedPreKeyId) {
         store.remove(signedPreKeyId);
     }
+
+    public static class JsonSignedPreKeyStoreDeserializer extends JsonDeserializer<JsonSignedPreKeyStore> {
+
+        @Override
+        public JsonSignedPreKeyStore deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
+            JsonNode node = jsonParser.getCodec().readTree(jsonParser);
+
+
+            Map<Integer, byte[]> preKeyMap = new HashMap<>();
+            if (node.isArray()) {
+                for (JsonNode preKey : node) {
+                    Integer preKeyId = preKey.get("id").asInt();
+                    try {
+                        preKeyMap.put(preKeyId, Base64.decode(preKey.get("record").asText()));
+                    }  catch (IOException e) {
+                        System.out.println(String.format("Error while decoding prekey for: %s", preKeyId));
+                    }
+                }
+            }
+
+            JsonSignedPreKeyStore keyStore = new JsonSignedPreKeyStore();
+            keyStore.addSignedPreKeys(preKeyMap);
+
+            return keyStore;
+
+        }
+    }
+
+    public static class JsonSignedPreKeyStoreSerializer extends JsonSerializer<JsonSignedPreKeyStore> {
+
+        @Override
+        public void serialize(JsonSignedPreKeyStore jsonPreKeyStore, JsonGenerator json, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
+            json.writeStartArray();
+            for (Map.Entry<Integer, byte[]> signedPreKey : jsonPreKeyStore.store.entrySet()) {
+                json.writeStartObject();
+                json.writeNumberField("id", signedPreKey.getKey());
+                json.writeStringField("record", Base64.encodeBytes(signedPreKey.getValue()));
+                json.writeEndObject();
+            }
+            json.writeEndArray();
+        }
+    }
 }