Skip to content

Tag (Alias).valueOf(String) with JsonCreator #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static BearerTokenAliasExample valueOf(String value) {
return new BearerTokenAliasExample(BearerToken.valueOf(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public int hashCode() {
return Boolean.hashCode(value);
}

@JsonCreator
public static BooleanAliasExample valueOf(String value) {
return new BooleanAliasExample(Boolean.parseBoolean(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static DateTimeAliasExample valueOf(String value) {
return new DateTimeAliasExample(ZonedDateTime.parse(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public int hashCode() {
return Double.hashCode(value);
}

@JsonCreator
public static DoubleAliasExample valueOf(String value) {
return new DoubleAliasExample(Double.parseDouble(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public int hashCode() {
return Integer.hashCode(value);
}

@JsonCreator
public static IntegerAliasExample valueOf(String value) {
return new IntegerAliasExample(Integer.parseInt(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static RidAliasExample valueOf(String value) {
return new RidAliasExample(ResourceIdentifier.valueOf(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static SafeLongAliasExample valueOf(String value) {
return new SafeLongAliasExample(SafeLong.valueOf(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static StringAliasExample valueOf(String value) {
return new StringAliasExample(value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public int hashCode() {
return value.hashCode();
}

@JsonCreator
public static UuidAliasExample valueOf(String value) {
return new UuidAliasExample(UUID.fromString(value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static JavaFile generateAliasType(
if (maybeValueOfFactoryMethod.isPresent()) {
spec.addMethod(MethodSpec.methodBuilder("valueOf")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC)
.addAnnotation(JsonCreator.class)
.addParameter(String.class, "value")
.returns(thisClass)
.addCode(maybeValueOfFactoryMethod.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ public void testAliasTypesEqualWhenInnerTypeEqual() throws Exception {
assertThat(BinaryAliasExample.of(byteBuffer)).isEqualTo(BinaryAliasExample.of(byteBuffer));
}

@Test
public void testPrimitiveAliasTypesCanBeDeserializedFromString() throws Exception {
assertThat(mapper.readValue("\"123\"", IntegerAliasExample.class)).isEqualTo(IntegerAliasExample.of(123));
assertThat(mapper.readValue("\"123.0\"", DoubleAliasExample.class)).isEqualTo(DoubleAliasExample.of(123.0));
}

@Test
public void testAliasTypesHashCodeEqualWhenInnerTypeEqual() throws Exception {
assertThat(StringAliasExample.of("a").hashCode()).isEqualTo(StringAliasExample.of("a").hashCode());
Expand Down
3 changes: 1 addition & 2 deletions versions.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
com.fasterxml.jackson.*:jackson-* = 2.7.4
com.fasterxml.jackson.datatype:jackson-datatype-jdk7 = 2.6.7
com.fasterxml.jackson.*:jackson-* = 2.9.6
com.google.code.findbugs:jsr305 = 3.0.2
com.google.errorprone:error_prone_annotations = 2.3.1
com.google.googlejavaformat:google-java-format = 1.5
Expand Down