update java petstore samples

This commit is contained in:
wing328 2018-01-26 10:02:24 +08:00
parent 47111b3241
commit 15b3dfbb80
3 changed files with 32 additions and 9 deletions

View File

@ -28,6 +28,7 @@ import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.format.DateTimeFormatter;
import io.swagger.client.model.*;
import okio.ByteString;
import java.io.IOException;
import java.io.StringReader;
@ -46,6 +47,7 @@ public class JSON {
private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();
private ByteArrayAdapter byteArrayAdapter = new ByteArrayAdapter();
public static GsonBuilder createGson() {
GsonFireBuilder fireBuilder = new GsonFireBuilder()
@ -87,6 +89,7 @@ public class JSON {
.registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter)
.registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter)
.registerTypeAdapter(LocalDate.class, localDateTypeAdapter)
.registerTypeAdapter(byte[].class, byteArrayAdapter)
.create();
}
@ -153,6 +156,34 @@ public class JSON {
}
}
/**
* Gson TypeAdapter for Byte Array type
*/
public class ByteArrayAdapter extends TypeAdapter<byte[]> {
@Override
public void write(JsonWriter out, byte[] value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(ByteString.of(value).base64());
}
}
@Override
public byte[] read(JsonReader in) throws IOException {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String bytesAsBase64 = in.nextString();
ByteString byteString = ByteString.decodeBase64(bytesAsBase64);
return byteString.toByteArray();
}
}
}
/**
* Gson TypeAdapter for JSR310 OffsetDateTime type
*/

View File

@ -91,14 +91,6 @@ if(hasProperty('target') && target == 'android') {
main = System.getProperty('mainClass')
classpath = sourceSets.main.runtimeClasspath
}
compileJava {
options.encoding = "UTF8"
}
compileTestJava {
options.encoding = "UTF8"
}
}
dependencies {