Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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