-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up rest api, increase testing via postman
- Loading branch information
Showing
5 changed files
with
121 additions
and
9 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
44 changes: 44 additions & 0 deletions
44
src/main/java/io/mapsmessaging/rest/translation/GsonDateTimeDeserialiser.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,44 @@ | ||
/* | ||
* Copyright [ 2020 - 2024 ] [Matthew Buckton] | ||
* Copyright [ 2024 - 2024 ] [Maps Messaging B.V.] | ||
* | ||
* 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.mapsmessaging.rest.translation; | ||
|
||
import com.google.gson.*; | ||
import java.lang.reflect.Type; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class GsonDateTimeDeserialiser implements JsonDeserializer<Object> { | ||
|
||
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
|
||
@Override | ||
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { | ||
if (json.isJsonNull()) { | ||
return null; | ||
} | ||
|
||
// Handle LocalDateTime specifically | ||
if (typeOfT == LocalDateTime.class) { | ||
return LocalDateTime.parse(json.getAsString(), FORMATTER); | ||
} | ||
|
||
// Delegate to Gson for other types | ||
return context.deserialize(json, typeOfT); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/io/mapsmessaging/rest/translation/GsonDateTimeSerialiser.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,58 @@ | ||
/* | ||
* Copyright [ 2020 - 2024 ] [Matthew Buckton] | ||
* Copyright [ 2024 - 2024 ] [Maps Messaging B.V.] | ||
* | ||
* 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.mapsmessaging.rest.translation; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
import java.lang.reflect.Type; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class GsonDateTimeSerialiser implements JsonSerializer<Object> { | ||
|
||
private static final DateTimeFormatter FORMATTER = | ||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
|
||
@Override | ||
public JsonElement serialize(Object src, Type typeOfSrc, JsonSerializationContext context) { | ||
if (src == null) { | ||
return context.serialize(null); | ||
} | ||
|
||
// Handle LocalDateTime specifically | ||
if (src instanceof LocalDateTime) { | ||
LocalDateTime localDateTime = (LocalDateTime) src; | ||
return context.serialize(localDateTime.format(FORMATTER)); | ||
} | ||
|
||
// Handle other types explicitly if needed | ||
if (src instanceof String || src instanceof Number || src instanceof Boolean) { | ||
return context.serialize(src); | ||
} | ||
|
||
// For custom objects, serialize fields into a JsonObject | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("class", src.getClass().getName()); | ||
jsonObject.add("fields", context.serialize(src)); // Serialize object fields | ||
|
||
return jsonObject; | ||
} | ||
} |
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