/xchange-coinbase/src/main/java/org/knowm/xchange/coinbase/dto/account/CoinbaseAddresses.java

http://github.com/timmolter/XChange · Java · 81 lines · 62 code · 18 blank · 1 comment · 1 complexity · 72d9ea8e86f0201441cac7ede1f27a30 MD5 · raw file

  1. package org.knowm.xchange.coinbase.dto.account;
  2. import com.fasterxml.jackson.annotation.JsonProperty;
  3. import com.fasterxml.jackson.core.JsonParser;
  4. import com.fasterxml.jackson.core.JsonProcessingException;
  5. import com.fasterxml.jackson.core.ObjectCodec;
  6. import com.fasterxml.jackson.databind.DeserializationContext;
  7. import com.fasterxml.jackson.databind.JsonDeserializer;
  8. import com.fasterxml.jackson.databind.JsonNode;
  9. import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.Date;
  13. import java.util.List;
  14. import org.knowm.xchange.coinbase.dto.CoinbasePagedResult;
  15. import org.knowm.xchange.coinbase.dto.account.CoinbaseAddresses.CoinbaseAddressesDeserializer;
  16. import org.knowm.xchange.utils.DateUtils;
  17. /** @author jamespedwards42 */
  18. @JsonDeserialize(using = CoinbaseAddressesDeserializer.class)
  19. public class CoinbaseAddresses extends CoinbasePagedResult {
  20. private final List<CoinbaseAddress> addresses;
  21. private CoinbaseAddresses(
  22. @JsonProperty("addresses") final List<CoinbaseAddress> addresses,
  23. @JsonProperty("total_count") final int totalCount,
  24. @JsonProperty("num_pages") final int numPages,
  25. @JsonProperty("current_page") final int currentPage) {
  26. super(totalCount, numPages, currentPage);
  27. this.addresses = addresses;
  28. }
  29. public List<CoinbaseAddress> getAddresses() {
  30. return addresses;
  31. }
  32. @Override
  33. public String toString() {
  34. return "CoinbaseAddresses [addresses=" + addresses + "]";
  35. }
  36. static class CoinbaseAddressesDeserializer extends JsonDeserializer<CoinbaseAddresses> {
  37. @Override
  38. public CoinbaseAddresses deserialize(JsonParser jp, DeserializationContext ctxt)
  39. throws IOException, JsonProcessingException {
  40. final ObjectCodec oc = jp.getCodec();
  41. final JsonNode node = oc.readTree(jp);
  42. final JsonNode addressesArrayNode = node.path("addresses");
  43. final List<CoinbaseAddress> addresses = new ArrayList<>();
  44. for (JsonNode addressNode : addressesArrayNode) {
  45. addresses.add(getAddressFromNode(addressNode));
  46. }
  47. final int totalCount = node.path("total_count").asInt();
  48. final int numPages = node.path("num_pages").asInt();
  49. final int currentPage = node.path("current_page").asInt();
  50. return new CoinbaseAddresses(addresses, totalCount, numPages, currentPage);
  51. }
  52. private CoinbaseAddress getAddressFromNode(JsonNode addressNode)
  53. throws com.fasterxml.jackson.databind.exc.InvalidFormatException {
  54. final JsonNode nestedAddressNode = addressNode.path("address");
  55. final String address = nestedAddressNode.path("address").asText();
  56. final String callbackUrl = nestedAddressNode.path("callback_url").asText();
  57. final String label = nestedAddressNode.path("label").asText();
  58. final Date createdAt =
  59. DateUtils.fromISO8601DateString(nestedAddressNode.path("created_at").asText());
  60. return new CoinbaseAddress(address, callbackUrl, label, createdAt);
  61. }
  62. }
  63. }