-
Notifications
You must be signed in to change notification settings - Fork 32
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 #105 from HubSpot/asymetric_serialization
Add support for asymetric property name serialization / deserialization
- Loading branch information
Showing
7 changed files
with
278 additions
and
18 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...tations/src/main/java/com/hubspot/rosetta/annotations/RosettaDeserializationProperty.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,25 @@ | ||
package com.hubspot.rosetta.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* like @JsonSetter only limited to Rosetta mapping/binding | ||
*/ | ||
@Target( | ||
{ | ||
ElementType.ANNOTATION_TYPE, | ||
ElementType.FIELD, | ||
ElementType.METHOD, | ||
ElementType.PARAMETER, | ||
} | ||
) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@RosettaAnnotation | ||
public @interface RosettaDeserializationProperty { | ||
String USE_DEFAULT_NAME = ""; | ||
|
||
String value() default USE_DEFAULT_NAME; | ||
} |
25 changes: 25 additions & 0 deletions
25
...notations/src/main/java/com/hubspot/rosetta/annotations/RosettaSerializationProperty.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,25 @@ | ||
package com.hubspot.rosetta.annotations; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* like @JsonGetter only limited to Rosetta mapping/binding | ||
*/ | ||
@Target( | ||
{ | ||
ElementType.ANNOTATION_TYPE, | ||
ElementType.FIELD, | ||
ElementType.METHOD, | ||
ElementType.PARAMETER, | ||
} | ||
) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@RosettaAnnotation | ||
public @interface RosettaSerializationProperty { | ||
String USE_DEFAULT_NAME = ""; | ||
|
||
String value() default USE_DEFAULT_NAME; | ||
} |
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
47 changes: 47 additions & 0 deletions
47
RosettaCore/src/test/java/com/hubspot/rosetta/annotations/RosettaGetterTest.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,47 @@ | ||
package com.hubspot.rosetta.annotations; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.hubspot.rosetta.Rosetta; | ||
import com.hubspot.rosetta.beans.RosettaGetterBean; | ||
import java.io.IOException; | ||
import org.junit.Test; | ||
|
||
public class RosettaGetterTest { | ||
|
||
private static final String EXPECTED_ROSETTA_JSON = | ||
"{\"jsonIgnoreRosettaUse\":\"Here\",\"mccartney_song_title\":\"Hey Jude\",\"other_value\":\"blah\"}"; | ||
private static final String ROSETTA_JSON = | ||
"{\"jsonIgnoreRosettaUse\":\"Here\",\"mcCartneySongTitle\":\"Hey Jude\",\"otherValue\":\"blah\"}"; | ||
private static final String JACKSON_JSON = | ||
"{\"mcCartneySongTitle\":\"Hey Jude\",\"otherValue\":\"blah\"}"; | ||
|
||
@Test | ||
public void itWritesWithTheGetterName() throws JsonProcessingException { | ||
RosettaGetterBean bean = new RosettaGetterBean(); | ||
bean.setMcCartneySongTitle("Hey Jude"); | ||
bean.setJsonIgnoreRosettaUse("Here"); | ||
bean.setSomeOtherValue("blah"); | ||
assertThat(Rosetta.getMapper().writeValueAsString(bean)) | ||
.isEqualTo(EXPECTED_ROSETTA_JSON); | ||
assertThat(new ObjectMapper().writeValueAsString(bean)).isEqualTo(JACKSON_JSON); | ||
} | ||
|
||
@Test | ||
public void itReadsThePropertyName() throws IOException { | ||
RosettaGetterBean rosettaRead = Rosetta | ||
.getMapper() | ||
.readValue(ROSETTA_JSON, RosettaGetterBean.class); | ||
assertThat(rosettaRead.getMcCartneySongTitle()).isEqualTo("Hey Jude"); | ||
assertThat(rosettaRead.getJsonIgnoreRosettaUse()).isNull(); | ||
assertThat(rosettaRead.getSomeOtherValue()).isEqualTo("blah"); | ||
|
||
RosettaGetterBean jsonRead = new ObjectMapper() | ||
.readValue(JACKSON_JSON, RosettaGetterBean.class); | ||
assertThat(jsonRead.getJsonIgnoreRosettaUse()).isNull(); | ||
assertThat(jsonRead.getMcCartneySongTitle()).isEqualTo("Hey Jude"); | ||
assertThat(jsonRead.getSomeOtherValue()).isEqualTo("blah"); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
RosettaCore/src/test/java/com/hubspot/rosetta/annotations/RosettaSetterTest.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,50 @@ | ||
package com.hubspot.rosetta.annotations; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.hubspot.rosetta.Rosetta; | ||
import com.hubspot.rosetta.beans.RosettaSetterBean; | ||
import java.io.IOException; | ||
import org.junit.Test; | ||
|
||
public class RosettaSetterTest { | ||
|
||
private static final String EXPECTED_ROSETTA_JSON = | ||
"{\"mcCartneySongTitle\":\"Hey Jude\",\"otherValue\":\"blah\"}"; | ||
private static final String EXPECTED_JACKSON_JSON = | ||
"{\"mcCartneySongTitle\":\"Hey Jude\",\"otherValue\":\"blah\"}"; | ||
private static final String ROSETTA_JSON = | ||
"{\"mccartney_song_title\":\"Hey Jude\",\"jsonIgnoreRosettaUse\":\"Here\",\"other_value\":\"blah\"}"; | ||
private static final String JACKSON_JSON = | ||
"{\"mcCartneySongTitle\":\"Hey Jude\",\"jsonIgnoreRosettaUse\":\"Here\",\"otherValue\":\"blah\"}"; | ||
|
||
@Test | ||
public void itWritesWithThePropertyName() throws JsonProcessingException { | ||
RosettaSetterBean bean = new RosettaSetterBean(); | ||
bean.setMcCartneySongTitle("Hey Jude"); | ||
bean.setJsonIgnoreRosettaUse("Here"); | ||
bean.setSomeOtherValue("blah"); | ||
assertThat(Rosetta.getMapper().writeValueAsString(bean)) | ||
.isEqualTo(EXPECTED_ROSETTA_JSON); | ||
assertThat(new ObjectMapper().writeValueAsString(bean)) | ||
.isEqualTo(EXPECTED_JACKSON_JSON); | ||
} | ||
|
||
@Test | ||
public void itReadsThePropertyName() throws IOException { | ||
RosettaSetterBean rosettaRead = Rosetta | ||
.getMapper() | ||
.readValue(ROSETTA_JSON, RosettaSetterBean.class); | ||
assertThat(rosettaRead.getMcCartneySongTitle()).isEqualTo("Hey Jude"); | ||
assertThat(rosettaRead.getJsonIgnoreRosettaUse()).isEqualTo("Here"); | ||
assertThat(rosettaRead.getSomeOtherValue()).isEqualTo("blah"); | ||
|
||
RosettaSetterBean jsonRead = new ObjectMapper() | ||
.readValue(JACKSON_JSON, RosettaSetterBean.class); | ||
assertThat(jsonRead.getJsonIgnoreRosettaUse()).isNull(); | ||
assertThat(jsonRead.getMcCartneySongTitle()).isEqualTo("Hey Jude"); | ||
assertThat(jsonRead.getSomeOtherValue()).isEqualTo("blah"); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
RosettaCore/src/test/java/com/hubspot/rosetta/beans/RosettaGetterBean.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,46 @@ | ||
package com.hubspot.rosetta.beans; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.hubspot.rosetta.annotations.RosettaSerializationProperty; | ||
|
||
public class RosettaGetterBean { | ||
|
||
@RosettaSerializationProperty("mccartney_song_title") | ||
private String mcCartneySongTitle; | ||
|
||
@RosettaSerializationProperty | ||
@JsonIgnore | ||
private String jsonIgnoreRosettaUse; | ||
|
||
@JsonProperty("otherValue") | ||
@RosettaSerializationProperty("other_value") | ||
private String someOtherValue; | ||
|
||
public String getMcCartneySongTitle() { | ||
return mcCartneySongTitle; | ||
} | ||
|
||
public RosettaGetterBean setMcCartneySongTitle(String mcCartneySongTitle) { | ||
this.mcCartneySongTitle = mcCartneySongTitle; | ||
return this; | ||
} | ||
|
||
public String getJsonIgnoreRosettaUse() { | ||
return jsonIgnoreRosettaUse; | ||
} | ||
|
||
public String getSomeOtherValue() { | ||
return someOtherValue; | ||
} | ||
|
||
public RosettaGetterBean setSomeOtherValue(String someOtherValue) { | ||
this.someOtherValue = someOtherValue; | ||
return this; | ||
} | ||
|
||
public RosettaGetterBean setJsonIgnoreRosettaUse(String jsonIgnoreRosettaUse) { | ||
this.jsonIgnoreRosettaUse = jsonIgnoreRosettaUse; | ||
return this; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
RosettaCore/src/test/java/com/hubspot/rosetta/beans/RosettaSetterBean.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,46 @@ | ||
package com.hubspot.rosetta.beans; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.hubspot.rosetta.annotations.RosettaDeserializationProperty; | ||
|
||
public class RosettaSetterBean { | ||
|
||
@RosettaDeserializationProperty("mccartney_song_title") | ||
private String mcCartneySongTitle; | ||
|
||
@RosettaDeserializationProperty | ||
@JsonIgnore | ||
private String jsonIgnoreRosettaUse; | ||
|
||
@JsonProperty("otherValue") | ||
@RosettaDeserializationProperty("other_value") | ||
private String someOtherValue; | ||
|
||
public String getMcCartneySongTitle() { | ||
return mcCartneySongTitle; | ||
} | ||
|
||
public RosettaSetterBean setMcCartneySongTitle(String mcCartneySongTitle) { | ||
this.mcCartneySongTitle = mcCartneySongTitle; | ||
return this; | ||
} | ||
|
||
public String getSomeOtherValue() { | ||
return someOtherValue; | ||
} | ||
|
||
public RosettaSetterBean setSomeOtherValue(String someOtherValue) { | ||
this.someOtherValue = someOtherValue; | ||
return this; | ||
} | ||
|
||
public String getJsonIgnoreRosettaUse() { | ||
return jsonIgnoreRosettaUse; | ||
} | ||
|
||
public RosettaSetterBean setJsonIgnoreRosettaUse(String jsonIgnoreRosettaUse) { | ||
this.jsonIgnoreRosettaUse = jsonIgnoreRosettaUse; | ||
return this; | ||
} | ||
} |