package com.coinbase.v1.deserializer;

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;

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

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

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;

    }
}

@@ -577,17 +577,24 @@
 zer() %7B%0A
-%09
+        
 super(Mo
@@ -725,17 +725,24 @@
 ontext)%0A
-%09
+        
     thro
@@ -759,17 +759,24 @@
 tion %7B%0A%0A
-%09
+        
 Money re
@@ -789,17 +789,24 @@
  null;%0A%0A
-%09
+        
 ObjectCo
@@ -837,17 +837,24 @@
 odec();%0A
-%09
+        
 JsonNode
@@ -887,17 +887,24 @@
 rser);%0A%0A
-%09
+        
 String c
@@ -919,17 +919,24 @@
 = null;%0A
-%09
+        
 String a
@@ -949,17 +949,24 @@
 = null;%0A
-%09
+        
 Long cen
@@ -977,17 +977,24 @@
  null;%0A%0A
-%09
+        
 if (node
@@ -1009,25 +1009,32 @@
 rrency%22)) %7B%0A
-%09
+        
     currency
@@ -1062,33 +1062,40 @@
 %22).textValue();%0A
-%09
+        
 %7D else if (node.
@@ -1113,25 +1113,32 @@
 cy_iso%22)) %7B%0A
-%09
+        
     currency
@@ -1174,29 +1174,43 @@
 extValue();%0A
-%09%7D%0A%0A%09
+        %7D%0A%0A        
 if (node.has
@@ -1219,25 +1219,32 @@
 amount%22)) %7B%0A
-%09
+        
     amount =
@@ -1276,17 +1276,24 @@
 alue();%0A
-%09
+        
 %7D else i
@@ -1312,25 +1312,32 @@
 %22cents%22)) %7B%0A
-%09
+        
     cents = 
@@ -1367,21 +1367,35 @@
 alue();%0A
-%09%7D%0A%0A%09
+        %7D%0A%0A        
 if (curr
@@ -1443,29 +1443,36 @@
 == null)) %7B%0A
-%09
+     
     
+   
 throw new Js
@@ -1552,20 +1552,23 @@
 ));%0A
-%09%7D%0A%0A%09try %7B%0A%09
+        %7D%0A%0A    
     
@@ -1589,18 +1589,50 @@
 null) %7B%0A
-%09%09
+            try %7B%0A                
 result =
@@ -1671,50 +1671,241 @@
 cy),
- new BigDecimal(amount));%0A%09    %7D else %7B%0A%09%09
+%0A                        Double.valueOf(amount));%0A            %7D catch (ArithmeticException e) %7B%0A%0A                Double doubleValue = Double.valueOf(amount);%0A                Long longValue = doubleValue.longValue();%0A%0A                
 resu
@@ -1921,13 +1921,8 @@
 y.of
-Minor
 (Cur
@@ -1948,177 +1948,185 @@
 cy),
- cents);%0A%09    %7D%0A%09%7D catch (Exception ex)
+%0A                        BigDecimal.valueOf(longValue));%0A            %7D%0A        %7D else
  %7B%0A
-%09
     
-throw new JsonParseException(%22Could not construct Money from arguments%22,%0A%09%09    jsonParser.getCurrentLocation(), ex);%0A%09%7D%0A%0A%09
+        result = Money.ofMinor(CurrencyUnit.of(currency), cents);%0A        %7D%0A%0A        
 retu

Handle cent values for unsupported currencies