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
Expand Up @@ -25,41 +25,46 @@
import nostr.event.tag.VoteTag;

import java.io.IOException;
import java.util.Map;
import java.util.function.Function;

public class TagDeserializer<T extends BaseTag> extends JsonDeserializer<T> {

private static final Map<String, Function<JsonNode, ? extends BaseTag>> TAG_DECODERS = Map.ofEntries(
Map.entry("a", AddressTag::deserialize),
Map.entry("d", IdentifierTag::deserialize),
Map.entry("e", EventTag::deserialize),
Map.entry("g", GeohashTag::deserialize),
Map.entry("l", LabelTag::deserialize),
Map.entry("L", LabelNamespaceTag::deserialize),
Map.entry("p", PubKeyTag::deserialize),
Map.entry("r", ReferenceTag::deserialize),
Map.entry("t", HashtagTag::deserialize),
Map.entry("u", UrlTag::deserialize),
Map.entry("v", VoteTag::deserialize),
Map.entry("emoji", EmojiTag::deserialize),
Map.entry("expiration", ExpirationTag::deserialize),
Map.entry("nonce", NonceTag::deserialize),
Map.entry("price", PriceTag::deserialize),
Map.entry("relays", RelaysTag::deserialize),
Map.entry("subject", SubjectTag::deserialize)
);

@Override
public T deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {

JsonNode node = jsonParser.getCodec().readTree(jsonParser);
Comment thread
tcheeric marked this conversation as resolved.
// Extract relevant data from the JSON node
var code = node.get(0);

if (code == null) {
throw new IOException("Unknown tag code: " + null);
if (!node.isArray() || node.size() == 0 || node.get(0) == null) {
throw new IOException("Malformed JSON: Expected a non-empty array.");
}
String code = node.get(0).asText();

Function<JsonNode, ? extends BaseTag> decoder = TAG_DECODERS.get(code);
BaseTag tag = decoder != null
? decoder.apply(node)
: new GenericTagDecoder<>().decode(node.toString());

// Perform custom deserialization logic based on the concrete class
return switch (code.asText()) {
case "a" -> AddressTag.deserialize(node);
case "d" -> IdentifierTag.deserialize(node);
case "e" -> EventTag.deserialize(node);
case "g" -> GeohashTag.deserialize(node);
case "l" -> LabelTag.deserialize(node);
case "L" -> LabelNamespaceTag.deserialize(node);
case "p" -> PubKeyTag.deserialize(node);
case "r" -> ReferenceTag.deserialize(node);
case "t" -> HashtagTag.deserialize(node);
case "u" -> UrlTag.deserialize(node);
case "v" -> VoteTag.deserialize(node);
case "emoji" -> EmojiTag.deserialize(node);
case "expiration" -> ExpirationTag.deserialize(node);
case "nonce" -> NonceTag.deserialize(node);
case "price" -> PriceTag.deserialize(node);
case "relays" -> RelaysTag.deserialize(node);
case "subject" -> SubjectTag.deserialize(node);
default -> (T) new GenericTagDecoder<>().decode(node.toString());
};
return (T) tag;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package nostr.event.unit;

import nostr.event.BaseTag;
import nostr.event.tag.AddressTag;
import nostr.event.tag.EventTag;
import nostr.event.tag.PriceTag;
import nostr.event.tag.UrlTag;
import nostr.event.tag.GenericTag;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static nostr.base.IEvent.MAPPER_AFTERBURNER;
import static org.junit.jupiter.api.Assertions.*;

class TagDeserializerTest {

@Test
void testAddressTagDeserialization() throws Exception {
String pubKey = "bbbd79f81439ff794cf5ac5f7bff9121e257f399829e472c7a14d3e86fe76984";
String json = "[\"a\",\"1:" + pubKey + ":test\",\"ws://localhost:8080\"]";
BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class);
assertInstanceOf(AddressTag.class, tag);
AddressTag aTag = (AddressTag) tag;
assertEquals(1, aTag.getKind());
assertEquals(pubKey, aTag.getPublicKey().toString());
assertEquals("test", aTag.getIdentifierTag().getUuid());
assertEquals("ws://localhost:8080", aTag.getRelay().getUri());
}

@Test
void testEventTagDeserialization() throws Exception {
String id = "494001ac0c8af2a10f60f23538e5b35d3cdacb8e1cc956fe7a16dfa5cbfc4346";
String json = "[\"e\",\"" + id + "\",\"wss://relay.example.com\",\"root\"]";
BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class);
assertInstanceOf(EventTag.class, tag);
EventTag eTag = (EventTag) tag;
assertEquals(id, eTag.getIdEvent());
assertEquals("wss://relay.example.com", eTag.getRecommendedRelayUrl());
assertEquals("root", eTag.getMarker().getValue());
}

@Test
void testPriceTagDeserialization() throws Exception {
String json = "[\"price\",\"10.99\",\"USD\"]";
BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class);
assertInstanceOf(PriceTag.class, tag);
PriceTag pTag = (PriceTag) tag;
assertEquals(new BigDecimal("10.99"), pTag.getNumber());
assertEquals("USD", pTag.getCurrency());
}

@Test
void testUrlTagDeserialization() throws Exception {
String json = "[\"u\",\"http://example.com\"]";
BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class);
assertInstanceOf(UrlTag.class, tag);
UrlTag uTag = (UrlTag) tag;
assertEquals("http://example.com", uTag.getUrl());
}

@Test
void testGenericFallback() throws Exception {
String json = "[\"unknown\",\"value\"]";
BaseTag tag = MAPPER_AFTERBURNER.readValue(json, BaseTag.class);
assertInstanceOf(GenericTag.class, tag);
GenericTag gTag = (GenericTag) tag;
assertEquals("unknown", gTag.getCode());
assertEquals("value", gTag.getAttributes().get(0).getValue());
}
}