-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from AutomateThePlanet/smpInternal
Smp internal release 31/10/2024
- Loading branch information
Showing
11 changed files
with
177 additions
and
56 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
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
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
53 changes: 53 additions & 0 deletions
53
...b/src/main/java/solutions/bellatrix/web/infrastructure/EmptyObjectTypeAdapterFactory.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,53 @@ | ||
package solutions.bellatrix.web.infrastructure; | ||
|
||
import com.google.gson.*; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class EmptyObjectTypeAdapterFactory implements TypeAdapterFactory { | ||
@Override | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); | ||
|
||
return new TypeAdapter<T>() { | ||
@Override | ||
public void write(JsonWriter out, T value) throws IOException { | ||
delegate.write(out, value); | ||
} | ||
|
||
@Override | ||
public T read(JsonReader in) throws IOException { | ||
if (in.peek() == JsonToken.BEGIN_ARRAY) { | ||
JsonArray arr = new JsonParser().parse(in).getAsJsonArray(); | ||
if (arr.size() == 0) { | ||
if (isCollectionType(type.getType())) { | ||
return delegate.fromJsonTree(arr); | ||
} else { | ||
return delegate.fromJsonTree(new JsonObject()); | ||
} | ||
} | ||
return delegate.fromJsonTree(arr); | ||
} | ||
return delegate.read(in); | ||
} | ||
}; | ||
} | ||
|
||
private boolean isCollectionType(Type type) { | ||
if (type instanceof Class<?>) { | ||
return Collection.class.isAssignableFrom((Class<?>)type); | ||
} else if (type instanceof ParameterizedType) { | ||
return isCollectionType(((ParameterizedType)type).getRawType()); | ||
} | ||
return false; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
bellatrix.web/src/main/java/solutions/bellatrix/web/infrastructure/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,22 @@ | ||
package solutions.bellatrix.web.infrastructure; | ||
|
||
import com.google.gson.*; | ||
|
||
import java.lang.reflect.Type; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class LocalDateTimeAdapter implements JsonSerializer<LocalDateTime>, JsonDeserializer<LocalDateTime> { | ||
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); | ||
|
||
@Override | ||
public JsonElement serialize(LocalDateTime src, Type typeOfSrc, JsonSerializationContext context) { | ||
return new JsonPrimitive(formatter.format(src).replace("+00:00", "")); | ||
} | ||
|
||
@Override | ||
public LocalDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
return LocalDateTime.parse(json.getAsString().replace("+00:00", ""), formatter); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...x.web/src/main/java/solutions/bellatrix/web/infrastructure/OffsetDateTimeTypeAdapter.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 solutions.bellatrix.web.infrastructure; | ||
|
||
import com.google.gson.*; | ||
|
||
import java.lang.reflect.Type; | ||
import java.time.LocalDateTime; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class OffsetDateTimeTypeAdapter implements JsonSerializer<OffsetDateTime>, JsonDeserializer<OffsetDateTime> { | ||
private final DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME; | ||
private final DateTimeFormatter localDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"); | ||
|
||
@Override | ||
public JsonElement serialize(OffsetDateTime src, Type typeOfSrc, JsonSerializationContext context) { | ||
return new JsonPrimitive(formatter.format(src)); | ||
} | ||
|
||
@Override | ||
public OffsetDateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) | ||
throws JsonParseException { | ||
OffsetDateTime result; | ||
try { | ||
result = OffsetDateTime.parse(json.getAsString(), formatter); | ||
} | ||
catch(Exception ex) { | ||
result = LocalDateTime.parse(json.getAsString(), localDateTimeFormatter).atOffset(ZoneOffset.UTC); | ||
} | ||
|
||
return result; | ||
} | ||
} |
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
Oops, something went wrong.