Skip to content

Commit e1b30ab

Browse files
authored
Merge pull request #3127 from wkclz/wkclz/JacksonSerializerOfIntOrString
fix: Add JacksonSerializer to serialize IntOrString
2 parents 880b3d8 + d4fdafb commit e1b30ab

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

kubernetes/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@
163163
<groupId>com.google.code.gson</groupId>
164164
<artifactId>gson</artifactId>
165165
</dependency>
166+
<dependency>
167+
<groupId>com.fasterxml.jackson.core</groupId>
168+
<artifactId>jackson-databind</artifactId>
169+
</dependency>
166170
<dependency>
167171
<groupId>io.gsonfire</groupId>
168172
<artifactId>gson-fire</artifactId>

kubernetes/src/main/java/io/kubernetes/client/custom/IntOrString.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package io.kubernetes.client.custom;
1414

15+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
1516
import com.google.gson.TypeAdapter;
1617
import com.google.gson.annotations.JsonAdapter;
1718
import com.google.gson.stream.JsonReader;
@@ -21,6 +22,7 @@
2122
import java.util.Objects;
2223

2324
@JsonAdapter(IntOrString.IntOrStringAdapter.class)
25+
@JsonSerialize(using = IntOrStringJacksonSerializer.class)
2426
public class IntOrString {
2527
private final boolean isInt;
2628
private final String strValue;
@@ -67,7 +69,9 @@ public boolean equals(Object o) {
6769
}
6870

6971
private boolean equals(IntOrString o) {
70-
if (isInt != o.isInt) return false;
72+
if (isInt != o.isInt) {
73+
return false;
74+
}
7175
return isInt ? Objects.equals(intValue, o.intValue) : Objects.equals(strValue, o.strValue);
7276
}
7377

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.custom;
14+
15+
import com.fasterxml.jackson.core.JsonGenerator;
16+
import com.fasterxml.jackson.databind.SerializerProvider;
17+
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
18+
19+
import java.io.IOException;
20+
21+
public class IntOrStringJacksonSerializer extends StdSerializer<IntOrString> {
22+
23+
public IntOrStringJacksonSerializer() {
24+
this(null);
25+
}
26+
27+
public IntOrStringJacksonSerializer(Class<IntOrString> t) {
28+
super(IntOrString.class);
29+
}
30+
31+
@Override
32+
public void serialize(IntOrString value, JsonGenerator jg, SerializerProvider provider) throws IOException {
33+
if (value.isInteger()) {
34+
jg.writeNumber(value.getIntValue());
35+
} else {
36+
jg.writeString(value.getStrValue());
37+
}
38+
}
39+
}

kubernetes/src/test/java/io/kubernetes/client/custom/IntOrStringTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import static org.hamcrest.Matchers.not;
1818
import static org.junit.Assert.*;
1919

20+
import com.fasterxml.jackson.core.JsonProcessingException;
21+
import com.fasterxml.jackson.databind.ObjectMapper;
2022
import org.junit.Test;
2123

2224
public class IntOrStringTest {
@@ -115,4 +117,14 @@ public void whenCreatedWithString_notEqualAnotherWithDifferentValue() {
115117

116118
assertThat(intOrString1, not(equalTo(intOrString2)));
117119
}
120+
121+
@Test
122+
public void jacksonSerializer_writeValueAsString() throws JsonProcessingException {
123+
IntOrString intOrString1 = new IntOrString(17);
124+
IntOrString intOrString2 = new IntOrString("17");
125+
126+
ObjectMapper mapper = new ObjectMapper();
127+
mapper.writeValueAsString(intOrString1);
128+
mapper.writeValueAsString(intOrString2);
129+
}
118130
}

0 commit comments

Comments
 (0)