-
-
Save mjaggard/6a7ad41ba80fd643d044 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
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; | |
/** | |
* Will deserialize a Money that was serialized with the MoneySerializer. Only pays attention to 'amount' and 'curency'. | |
*/ | |
public class MoneyDeserializer extends StdDeserializer<Money> { | |
/** */ | |
private static final long serialVersionUID = 1L; | |
/** */ | |
public MoneyDeserializer() { | |
super(Money.class); | |
} | |
/** */ | |
@Override | |
public Money deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { | |
JsonNode moneyTree = jp.readValueAsTree(); | |
int amount = moneyTree.get("amount").asInt(); | |
JsonNode currencyNode = moneyTree.get("currency"); | |
CurrencyUnit currency = CurrencyUnit.of(currencyNode.asText()); | |
return Money.ofMinor(currency, amount); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.JsonSerializer; | |
import com.fasterxml.jackson.databind.SerializerProvider; | |
import com.example.MoneyUtils; | |
import org.joda.money.Money; | |
import java.io.IOException; | |
/** | |
* Configuring this serializer will make Money objects render as a structure like this: | |
* { amount: 2388, str: "23.88", pretty: "$23.88", symbol: "$", currency: "USD" } | |
*/ | |
public class MoneySerializer extends JsonSerializer<Money> { | |
@Override | |
public void serialize(Money value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { | |
jgen.writeStartObject(); | |
{ | |
jgen.writeNumberField("amount", value.getAmountMinorInt()); | |
jgen.writeStringField("str", value.getAmount().toString()); | |
jgen.writeStringField("symbol", value.getCurrencyUnit().getSymbol()); | |
jgen.writeStringField("currency", value.getCurrencyUnit().getCode()); | |
String pretty = prettyPrintWithCents(value); | |
jgen.writeStringField("pretty", pretty); | |
} | |
jgen.writeEndObject(); | |
} | |
/** | |
* Makes a nicely formatted version like $50.00 or $23.99. Always shows cents. | |
*/ | |
private String prettyPrintWithCents(Money money) { | |
StringBuilder bld = new StringBuilder(money.getCurrencyUnit().getSymbol()); | |
bld.append(money.getAmount().toPlainString()); | |
return bld.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment