+
+ 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();
+ }
+ }