-
Notifications
You must be signed in to change notification settings - Fork 448
chore: added a common serializer and its tests #878
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
baf498e
e348355
b737594
953e73e
04f1e08
9c2ba4d
c469b61
3ba6d3c
5845dc3
0810ce5
ac0ac22
76fa5e1
9228a41
51c3e57
2ae7c6b
cceb784
81d565a
1d53b88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,100 @@ | ||||||||||||||||||||||||||||||||||||
package com.twilio.converter; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
import com.fasterxml.jackson.core.JsonProcessingException; | ||||||||||||||||||||||||||||||||||||
import com.fasterxml.jackson.databind.JsonMappingException; | ||||||||||||||||||||||||||||||||||||
import com.fasterxml.jackson.databind.ObjectMapper; | ||||||||||||||||||||||||||||||||||||
import com.twilio.constant.EnumConstants.ParameterType; | ||||||||||||||||||||||||||||||||||||
import com.twilio.exception.ApiConnectionException; | ||||||||||||||||||||||||||||||||||||
import com.twilio.exception.ApiException; | ||||||||||||||||||||||||||||||||||||
import com.twilio.http.Request; | ||||||||||||||||||||||||||||||||||||
import java.io.IOException; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
import java.time.LocalDate; | ||||||||||||||||||||||||||||||||||||
import java.time.ZonedDateTime; | ||||||||||||||||||||||||||||||||||||
import java.time.format.DateTimeFormatter; | ||||||||||||||||||||||||||||||||||||
import java.util.Map; | ||||||||||||||||||||||||||||||||||||
import java.util.Objects; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public class Serializer { | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public static <T> void toString(Request request, String key, T value, ParameterType parameterType) { | ||||||||||||||||||||||||||||||||||||
if (value == null) return; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
String stringValue = convertToString(value); | ||||||||||||||||||||||||||||||||||||
addParamToRequest(request, key, stringValue, parameterType); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public static <T> void toString(Request request, ObjectMapper mapper, T value) { | ||||||||||||||||||||||||||||||||||||
if (value == null) return; | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (mapper == null) { | ||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException("ObjectMapper is required for JSON serialization"); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
String stringValue = toJson(value, mapper); | ||||||||||||||||||||||||||||||||||||
request.setBody(stringValue); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
public static String toJson(Object object, ObjectMapper mapper) { | ||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||
return mapper.writeValueAsString(object); | ||||||||||||||||||||||||||||||||||||
} catch (final JsonMappingException e) { | ||||||||||||||||||||||||||||||||||||
throw new ApiException(e.getMessage(), e); | ||||||||||||||||||||||||||||||||||||
} catch (JsonProcessingException e) { | ||||||||||||||||||||||||||||||||||||
throw new ApiException(e.getMessage(), e); | ||||||||||||||||||||||||||||||||||||
} catch (final IOException e) { | ||||||||||||||||||||||||||||||||||||
throw new ApiConnectionException(e.getMessage(), e); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
private static <T> String convertToString(T value) { | ||||||||||||||||||||||||||||||||||||
if (value instanceof Map) { | ||||||||||||||||||||||||||||||||||||
return Converter.mapToJson((Map<String, ? extends Object>) value); | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cast to
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||
return String.valueOf(value); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
private static void addParamToRequest(Request request, String key, String value, ParameterType parameterType) { | ||||||||||||||||||||||||||||||||||||
Objects.requireNonNull(parameterType, "ParameterType cannot be null"); | ||||||||||||||||||||||||||||||||||||
switch (parameterType) { | ||||||||||||||||||||||||||||||||||||
case HEADER: | ||||||||||||||||||||||||||||||||||||
request.addHeaderParam(key, value); | ||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||
case QUERY: | ||||||||||||||||||||||||||||||||||||
request.addQueryParam(key, value); | ||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||
case URLENCODED: | ||||||||||||||||||||||||||||||||||||
request.addPostParam(key, value); | ||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How will body params be added? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. addressed json content type serialization. |
||||||||||||||||||||||||||||||||||||
case JSON: | ||||||||||||||||||||||||||||||||||||
request.setBody(value); | ||||||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||||||
throw new IllegalArgumentException("Unsupported ParameterType: " + parameterType); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||
Inequality fields are only supported in Query parameters. | ||||||||||||||||||||||||||||||||||||
dateBefore is upperBound and dateAfter is lowerBound | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
public static void toString(final Request request, final String key, LocalDate date, LocalDate dateBefore, LocalDate dateAfter) { | ||||||||||||||||||||||||||||||||||||
if (date != null) { | ||||||||||||||||||||||||||||||||||||
request.addQueryParam(key, date.format(DateTimeFormatter.ofPattern(Request.QUERY_STRING_DATE_FORMAT))); | ||||||||||||||||||||||||||||||||||||
} else if (dateAfter != null || dateBefore != null) { | ||||||||||||||||||||||||||||||||||||
request.addQueryDateRange(key, dateAfter, dateBefore); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
/* | ||||||||||||||||||||||||||||||||||||
Inequality fields are only supported in Query parameters. | ||||||||||||||||||||||||||||||||||||
dateBefore is upperBound and dateAfter is lowerBound | ||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||
public static void toString(final Request request, final String key, ZonedDateTime date, ZonedDateTime dateBefore, ZonedDateTime dateAfter) { | ||||||||||||||||||||||||||||||||||||
if (date != null) { | ||||||||||||||||||||||||||||||||||||
request.addQueryParam(key, date.format(DateTimeFormatter.ofPattern(Request.QUERY_STRING_DATE_TIME_FORMAT))); | ||||||||||||||||||||||||||||||||||||
} else if (dateAfter != null || dateBefore != null) { | ||||||||||||||||||||||||||||||||||||
request.addQueryDateTimeRange(key, dateAfter, dateBefore); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The objectToJson() can be used here instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean using mapper to convert to string ?
We can not use it, because \n (new line) is treated as actual new line for toString method, whereas mapper keep /n as it as.