-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
172 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/io/geilehner/storyblok/adapters/DoubleTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.geilehner.storyblok.adapters; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
public class DoubleTypeAdapter extends TypeAdapter<Double> { | ||
@Override | ||
public Double read(JsonReader reader) throws IOException { | ||
if (reader.peek() == JsonToken.NULL) { | ||
reader.nextNull(); | ||
return null; | ||
} | ||
String stringValue = reader.nextString(); | ||
try { | ||
return Double.parseDouble(stringValue); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter writer, Double value) throws IOException { | ||
if (value == null) { | ||
writer.nullValue(); | ||
return; | ||
} | ||
writer.value(value.toString()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/io/geilehner/storyblok/adapters/IntegerTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.geilehner.storyblok.adapters; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
|
||
public class IntegerTypeAdapter extends TypeAdapter<Integer> { | ||
@Override | ||
public Integer read(JsonReader reader) throws IOException { | ||
if (reader.peek() == JsonToken.NULL) { | ||
reader.nextNull(); | ||
return null; | ||
} | ||
String stringValue = reader.nextString(); | ||
try { | ||
return Integer.parseInt(stringValue); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter writer, Integer value) throws IOException { | ||
if (value == null) { | ||
writer.nullValue(); | ||
return; | ||
} | ||
writer.value(value.toString()); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/io/geilehner/storyblok/adapters/LocalDateTimeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.geilehner.storyblok.adapters; | ||
|
||
import com.google.gson.*; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
|
||
public class LocalDateTimeAdapter implements JsonDeserializer<LocalDateTime> { | ||
@Override | ||
public LocalDateTime deserialize(JsonElement json, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
if(json.getAsJsonPrimitive().getAsString() == null || !json.getAsJsonPrimitive().getAsString().isEmpty()){ | ||
try{ | ||
return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSz")); | ||
} | ||
catch (DateTimeParseException e){ | ||
return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/io/geilehner/storyblok/adapters/LongTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.geilehner.storyblok.adapters; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
|
||
public class LongTypeAdapter extends TypeAdapter<Long> { | ||
@Override | ||
public Long read(JsonReader reader) throws IOException { | ||
if (reader.peek() == JsonToken.NULL) { | ||
reader.nextNull(); | ||
return null; | ||
} | ||
String stringValue = reader.nextString(); | ||
try { | ||
Long value = Long.valueOf(stringValue); | ||
return value; | ||
} catch (NumberFormatException e) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter writer, Long value) throws IOException { | ||
if (value == null) { | ||
writer.nullValue(); | ||
return; | ||
} | ||
writer.value(value); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/io/geilehner/storyblok/adapters/UUIDTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.geilehner.storyblok.adapters; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
public class UUIDTypeAdapter extends TypeAdapter<UUID> { | ||
@Override | ||
public UUID read(JsonReader reader) throws IOException { | ||
if (reader.peek() == JsonToken.NULL) { | ||
reader.nextNull(); | ||
return null; | ||
} | ||
String stringValue = reader.nextString(); | ||
try { | ||
return UUID.fromString(stringValue); | ||
} catch (Exception e) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void write(JsonWriter writer, UUID value) throws IOException { | ||
if (value == null) { | ||
writer.nullValue(); | ||
return; | ||
} | ||
writer.value(value.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters