-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from zalando/feature/currency-unit
Re-added support for CurrencyUnit
- Loading branch information
Showing
7 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/org/zalando/jackson/datatype/money/CurrencyUnitDeserializer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.zalando.jackson.datatype.money; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer; | ||
|
||
import javax.money.CurrencyUnit; | ||
import javax.money.Monetary; | ||
import java.io.IOException; | ||
|
||
public final class CurrencyUnitDeserializer extends JsonDeserializer<CurrencyUnit> { | ||
|
||
@Override | ||
public Object deserializeWithType(final JsonParser parser, final DeserializationContext context, | ||
final TypeDeserializer deserializer) throws IOException { | ||
|
||
// effectively assuming no type information at all | ||
return deserialize(parser, context); | ||
} | ||
|
||
@Override | ||
public CurrencyUnit deserialize(final JsonParser parser, final DeserializationContext context) throws IOException { | ||
final String currencyCode = parser.getValueAsString(); | ||
return Monetary.getCurrency(currencyCode); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/zalando/jackson/datatype/money/CurrencyUnitSerializer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.zalando.jackson.datatype.money; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
|
||
import javax.money.CurrencyUnit; | ||
import java.io.IOException; | ||
|
||
public final class CurrencyUnitSerializer extends JsonSerializer<CurrencyUnit> { | ||
|
||
@Override | ||
public void serialize(final CurrencyUnit value, final JsonGenerator generator, final SerializerProvider serializers) | ||
throws IOException { | ||
generator.writeString(value.getCurrencyCode()); | ||
} | ||
|
||
} |
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
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
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
41 changes: 41 additions & 0 deletions
41
src/test/java/org/zalando/jackson/datatype/money/CurrencyUnitDeserializerTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.zalando.jackson.datatype.money; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.javamoney.moneta.CurrencyUnitBuilder; | ||
import org.junit.Test; | ||
|
||
import javax.money.CurrencyUnit; | ||
import javax.money.UnknownCurrencyException; | ||
import java.io.IOException; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public final class CurrencyUnitDeserializerTest { | ||
|
||
private final ObjectMapper unit = new ObjectMapper().findAndRegisterModules(); | ||
|
||
@Test | ||
public void shouldDeserialize() throws IOException { | ||
final CurrencyUnit actual = unit.readValue("\"EUR\"", CurrencyUnit.class); | ||
final CurrencyUnit expected = CurrencyUnitBuilder.of("EUR", "default").build(); | ||
|
||
assertThat(actual, is(expected)); | ||
} | ||
|
||
@Test(expected = UnknownCurrencyException.class) | ||
public void shouldNotDeserializeInvalidCurrency() throws IOException { | ||
unit.readValue("\"FOO\"", CurrencyUnit.class); | ||
} | ||
|
||
@Test | ||
public void shouldDeserializeWithTyping() throws IOException { | ||
unit.enableDefaultTyping(); | ||
|
||
final CurrencyUnit actual = unit.readValue("\"EUR\"", CurrencyUnit.class); | ||
final CurrencyUnit expected = CurrencyUnitBuilder.of("EUR", "default").build(); | ||
|
||
assertThat(actual, is(expected)); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/org/zalando/jackson/datatype/money/CurrencyUnitSerializerTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.zalando.jackson.datatype.money; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.javamoney.moneta.CurrencyUnitBuilder; | ||
import org.junit.Test; | ||
|
||
import javax.money.CurrencyUnit; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public final class CurrencyUnitSerializerTest { | ||
|
||
private final ObjectMapper unit = new ObjectMapper().findAndRegisterModules(); | ||
|
||
@Test | ||
public void shouldSerialize() throws JsonProcessingException { | ||
final String expected = "EUR"; | ||
final CurrencyUnit currency = CurrencyUnitBuilder.of(expected, "default").build(); | ||
|
||
final String actual = unit.writeValueAsString(currency); | ||
|
||
assertThat(actual, is('"' + expected + '"')); | ||
} | ||
|
||
} |