Skip to content

Commit

Permalink
Merge pull request #334 from /issues/333
Browse files Browse the repository at this point in the history
Issues/333 - Key JSON deserialization
  • Loading branch information
cnorburn authored Sep 11, 2024
2 parents 48de573 + e47e354 commit eacee23
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.casper.sdk.jackson.deserializer;

import com.casper.sdk.exception.CasperClientException;
import com.casper.sdk.exception.DeserializationException;
import com.casper.sdk.exception.NoSuchKeyTagException;
import com.casper.sdk.model.key.AbstractSerializedKeyTaggedHex;
import com.casper.sdk.model.key.Key;
import com.casper.sdk.model.key.Tag;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand All @@ -28,17 +30,28 @@ public abstract class AbstractSerializedKeyTaggedHexDeserializer<T extends Abstr
private static final String NULL_PUBLIC_KEY = "00";

@Override
public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode node = p.getCodec().readTree(p);
public T deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
final JsonNode node = p.getCodec().readTree(p);

if (NULL_PUBLIC_KEY.equals(node.textValue())) {
final String strKey = node.textValue();

if (strKey.contains("-")) {
try {
//noinspection unchecked
return (T) Key.fromKeyString(strKey);
} catch (NoSuchKeyTagException e) {
throw new CasperClientException("No such key: " + strKey, e);
}
}

if (NULL_PUBLIC_KEY.equals(strKey)) {
return null;
}

T object = this.getInstanceOf();
final T object = this.getInstanceOf();

try {
byte[] bytes = ByteUtils.parseHexString(node.asText());
final byte[] bytes = ByteUtils.parseHexString(strKey);
this.loadKey(object, bytes);
} catch (NoSuchAlgorithmException | NoSuchKeyTagException e) {
throw new DeserializationException("Problem deserializing Algorithm tagged hex string", e);
Expand All @@ -49,5 +62,5 @@ public T deserialize(JsonParser p, DeserializationContext ctxt) throws IOExcepti

protected abstract T getInstanceOf();

protected abstract void loadKey(T key, byte[] bytes) throws NoSuchAlgorithmException, NoSuchKeyTagException;
protected abstract void loadKey(final T key, final byte[] bytes) throws NoSuchAlgorithmException, NoSuchKeyTagException;
}
Loading

0 comments on commit eacee23

Please sign in to comment.