-
Notifications
You must be signed in to change notification settings - Fork 30
bugfix/default post content type and primitive types #167
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
Merged
Parent:
Feature/v2
baywet
merged 3 commits into
feature/v2
from
bugfix/default-post-content-type-and-primitive-types
Mar 11, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
60 changes: 60 additions & 0 deletions
60
src/main/java/com/microsoft/graph/serializer/EdmNativeTypeSerializer.java
This file contains hidden or 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,60 @@ | ||
| package com.microsoft.graph.serializer; | ||
|
|
||
| import java.lang.reflect.Type; | ||
| import java.math.BigDecimal; | ||
| import java.util.UUID; | ||
|
|
||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonParseException; | ||
| import com.microsoft.graph.logger.ILogger; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** Deserializer for native EDM types from the service. Used for actions and functions that return native types for example. */ | ||
| public class EdmNativeTypeSerializer { | ||
| /** | ||
| * Deserializes native EDM types from the service. Used for actions and functions that return native types for example. | ||
| * @param <T> Expected return type. | ||
| * @param json json to deserialize. | ||
| * @param type class of the expected return type. | ||
| * @param logger logger to use. | ||
| * @return the deserialized type or null. | ||
| */ | ||
| @Nullable | ||
| public static <T> T deserialize(@Nonnull final JsonElement json, @Nonnull final Class<T> type, @Nonnull final ILogger logger) { | ||
| if (json == null || type == null) { | ||
| return null; | ||
| } else if(json.isJsonPrimitive()) { | ||
| return getPrimitiveValue(json, type); | ||
| } else if(json.isJsonObject()) { | ||
| final JsonElement element = json.getAsJsonObject().get("@odata.null"); | ||
| if(element != null && element.isJsonPrimitive()) { | ||
| return getPrimitiveValue(element, type); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| @SuppressWarnings("unchecked") | ||
| private static <T> T getPrimitiveValue(final JsonElement json, final Class<T> type) { | ||
| if(type == Boolean.class) { | ||
| return (T) Boolean.valueOf(json.getAsBoolean()); | ||
| } else if(type == String.class) { | ||
MIchaelMainer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return (T)json.getAsString(); | ||
| } else if(type == Integer.class) { | ||
| return (T) Integer.valueOf(json.getAsInt()); | ||
| } else if(type == UUID.class) { | ||
| return (T) UUID.fromString(json.getAsString()); | ||
| } else if(type == Long.class) { | ||
| return (T) Long.valueOf(json.getAsLong()); | ||
| } else if (type == Float.class) { | ||
| return (T) Float.valueOf(json.getAsFloat()); | ||
| } else if (type == BigDecimal.class) { | ||
| return (T) json.getAsBigDecimal(); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
76 changes: 76 additions & 0 deletions
76
src/test/java/com/microsoft/graph/serializer/EdmNativeTypeSerializerTests.java
This file contains hidden or 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,76 @@ | ||
| package com.microsoft.graph.serializer; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.UUID; | ||
|
|
||
| import com.microsoft.graph.logger.DefaultLogger; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class EdmNativeTypeSerializerTests { | ||
| @Test | ||
| public void testBoolean() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":true}"; | ||
| final Boolean result = serializer.deserializeObject(source, Boolean.class); | ||
|
|
||
| assertEquals(Boolean.valueOf(true), result); | ||
| } | ||
| @Test | ||
| public void testInteger() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":12}"; | ||
| final Integer result = serializer.deserializeObject(source, Integer.class); | ||
|
|
||
| assertEquals(Integer.valueOf(12), result); | ||
| } | ||
| @Test | ||
| public void testString() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":\"toto\"}"; | ||
| final String result = serializer.deserializeObject(source, String.class); | ||
|
|
||
| assertEquals("toto", result); | ||
| } | ||
| @Test | ||
| public void testFloat() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":12.5}"; | ||
| final Float result = serializer.deserializeObject(source, Float.class); | ||
|
|
||
| assertEquals(Float.valueOf("12.5"), result); | ||
| } | ||
| @Test | ||
| public void testLong() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":12}"; | ||
| final Long result = serializer.deserializeObject(source, Long.class); | ||
|
|
||
| assertEquals(Long.valueOf(12), result); | ||
| } | ||
| @Test | ||
| public void testBigDecimal() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":12}"; | ||
| final BigDecimal result = serializer.deserializeObject(source, BigDecimal.class); | ||
|
|
||
| assertEquals(BigDecimal.valueOf(12), result); | ||
| } | ||
| @Test | ||
| public void testUUID() throws Exception { | ||
| final DefaultSerializer serializer = new DefaultSerializer(new DefaultLogger()); | ||
|
|
||
| final String source = "{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#Edm.Null\",\"@odata.null\":\"0E6558C3-9640-4385-860A-2A894AC5C246\"}"; | ||
| final UUID result = serializer.deserializeObject(source, UUID.class); | ||
|
|
||
| assertEquals(UUID.fromString("0E6558C3-9640-4385-860A-2A894AC5C246"), result); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.