Skip to content

Commit

Permalink
deserialize enum support toString value
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jan 1, 2025
1 parent c82332e commit 149ab84
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3231,6 +3231,13 @@ protected ObjectReader createEnumReader(
String name = e.name();
long hashLCase = Fnv.hashCode64LCase(name);
enumMap.putIfAbsent(hashLCase, e);

String str = e.toString();
if (name.equals(str)) {
continue;
}
long hashLCaseStr = Fnv.hashCode64LCase(str);
enumMap.putIfAbsent(hashLCaseStr, e);
}

long[] enumNameHashCodes = new long[enumMap.size()];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,46 @@ public String toString() {
return this.value;
}
}

public enum CityGMLVersion1 {
v3_0("3.0"),
v2_0("2.0"),
v1_0("1.0");

private final String value;

CityGMLVersion1(String value) {
this.value = value;
}

public String toValue() {
return this.value;
}

public static CityGMLVersion1 fromValue(String value) {
for (CityGMLVersion1 v : values()) {
if (v.value.equals(value)) {
return v;
}
}
return null;
}

public String toString() {
return this.value;
}
}

@Test
public void test1() {
Foo1 foo = JSON.parseObject("{\n" +
" \"version\": \"2.0\"\n" +
"}", Foo1.class);

assertEquals(CityGMLVersion1.v2_0, foo.version);
}

public static class Foo1 {
public CityGMLVersion1 version;
}
}

0 comments on commit 149ab84

Please sign in to comment.