Skip to content

Commit

Permalink
fix: Add JacksonSerializer to serialize IntOrString
Browse files Browse the repository at this point in the history
  • Loading branch information
wkclz committed Feb 28, 2024
1 parent 2e9ac1c commit d4fdafb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
4 changes: 4 additions & 0 deletions kubernetes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package io.kubernetes.client.custom;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
Expand All @@ -21,6 +22,7 @@
import java.util.Objects;

@JsonAdapter(IntOrString.IntOrStringAdapter.class)
@JsonSerialize(using = IntOrStringJacksonSerializer.class)
public class IntOrString {
private final boolean isInt;
private final String strValue;
Expand Down Expand Up @@ -67,7 +69,9 @@ public boolean equals(Object o) {
}

private boolean equals(IntOrString o) {
if (isInt != o.isInt) return false;
if (isInt != o.isInt) {
return false;
}
return isInt ? Objects.equals(intValue, o.intValue) : Objects.equals(strValue, o.strValue);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client.custom;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

public class IntOrStringJacksonSerializer extends StdSerializer<IntOrString> {

public IntOrStringJacksonSerializer() {
this(null);
}

public IntOrStringJacksonSerializer(Class<IntOrString> t) {
super(IntOrString.class);
}

@Override
public void serialize(IntOrString value, JsonGenerator jg, SerializerProvider provider) throws IOException {
if (value.isInteger()) {
jg.writeNumber(value.getIntValue());
} else {
jg.writeString(value.getStrValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

public class IntOrStringTest {
Expand Down Expand Up @@ -115,4 +117,14 @@ public void whenCreatedWithString_notEqualAnotherWithDifferentValue() {

assertThat(intOrString1, not(equalTo(intOrString2)));
}

@Test
public void jacksonSerializer_writeValueAsString() throws JsonProcessingException {
IntOrString intOrString1 = new IntOrString(17);
IntOrString intOrString2 = new IntOrString("17");

ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(intOrString1);
mapper.writeValueAsString(intOrString2);
}
}

0 comments on commit d4fdafb

Please sign in to comment.