Package com.coinbase.api.deserializer

Source Code of com.coinbase.api.deserializer.MoneyDeserializer

package com.coinbase.api.deserializer;

import java.io.IOException;
import java.math.BigDecimal;

import org.joda.money.CurrencyUnit;
import org.joda.money.Money;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

public class MoneyDeserializer extends StdDeserializer<Money> {

    public MoneyDeserializer() {
  super(Money.class);
    }

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

  Money result = null;

  ObjectCodec oc = jsonParser.getCodec();
  JsonNode node = oc.readTree(jsonParser);

  String currency = null;
  String amount = null;
  Long cents = null;

  if (node.has("currency")) {
      currency = node.get("currency").textValue();
  } else if (node.has("currency_iso")) {
      currency = node.get("currency_iso").textValue();
  }

  if (node.has("amount")) {
      amount = node.get("amount").textValue();
  } else if (node.has("cents")) {
      cents = node.get("cents").longValue();
  }

  if (currency == null || (amount == null && cents == null)) {
      throw new JsonParseException("Wrong format for Money", jsonParser.getCurrentLocation());
  }

  try {
      if (amount != null) {
    result = Money.of(CurrencyUnit.of(currency), new BigDecimal(amount));
      } else {
    result = Money.ofMinor(CurrencyUnit.of(currency), cents);
      }
  } catch (Exception ex) {
      throw new JsonParseException("Could not construct Money from arguments",
        jsonParser.getCurrentLocation(), ex);
  }

  return result;

    }
}
TOP

Related Classes of com.coinbase.api.deserializer.MoneyDeserializer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.