Skip to content

Commit

Permalink
Merge pull request #108 from lewisheadden/IntOrStringSupport
Browse files Browse the repository at this point in the history
Add IntOrString model and GSON adapter
  • Loading branch information
brendandburns authored Nov 3, 2017
2 parents e085bc9 + ef5f427 commit ca70501
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.kubernetes.client.custom;

import com.google.gson.TypeAdapter;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;

@JsonAdapter(IntOrString.IntOrStringAdapter.class)
public class IntOrString {
private final boolean isInt;
private final String strValue;
private final Integer intValue;

public IntOrString(final String value) {
this.isInt = false;
this.strValue = value;
this.intValue = null;
}

public IntOrString(final int value) {
this.isInt = true;
this.intValue = value;
this.strValue = null;
}

public boolean isInteger() {
return isInt;
}

public String getStrValue() {
if (isInt) {
throw new IllegalStateException("Not a string");
}
return strValue;
}

public Integer getIntValue() {
if (!isInt) {
throw new IllegalStateException("Not an integer");
}
return intValue;
}

public static class IntOrStringAdapter extends TypeAdapter<IntOrString> {
@Override
public void write(JsonWriter jsonWriter, IntOrString intOrString) throws IOException {
if (intOrString.isInteger()) {
jsonWriter.value(intOrString.getIntValue());
} else {
jsonWriter.value(intOrString.getStrValue());
}
}

@Override
public IntOrString read(JsonReader jsonReader) throws IOException {
final JsonToken nextToken = jsonReader.peek();
if (nextToken == JsonToken.NUMBER) {
return new IntOrString(jsonReader.nextInt());
} else if (nextToken == JsonToken.STRING) {
return new IntOrString(jsonReader.nextString());
} else {
throw new IllegalStateException("Could not deserialize to IntOrString. Was " + nextToken);
}
}
}
}

0 comments on commit ca70501

Please sign in to comment.