-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement custom JsonDeserializer to support "property_" prefixed cus…
…tom metadata
- Loading branch information
Jonas Dickel
committed
Aug 6, 2024
1 parent
5ab8473
commit 3a3cf25
Showing
4 changed files
with
158 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.bynder.sdk.util; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.bynder.sdk.model.Media; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.JsonDeserializationContext; | ||
import com.google.gson.JsonDeserializer; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParseException; | ||
|
||
public class MediaTypeAdapter implements JsonDeserializer<Media> { | ||
|
||
private static final String PROPERTY_PREFIX = "property_"; | ||
private static final String CUSTOM_METAPROPERTY_FIELDNAME = "customMetaproperties"; | ||
|
||
private final Gson gson; | ||
|
||
public MediaTypeAdapter() { | ||
this.gson = new GsonBuilder().registerTypeAdapter(Boolean.class, new BooleanTypeAdapter()).create(); | ||
} | ||
|
||
@Override | ||
public Media deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) | ||
throws JsonParseException { | ||
JsonObject jsonObject = json.getAsJsonObject(); | ||
Media media = gson.fromJson(jsonObject, Media.class); | ||
|
||
Map<String, List<String>> metaproperties = new LinkedHashMap<>(); | ||
|
||
for (Map.Entry<String, JsonElement> elementJson : jsonObject.entrySet()) { | ||
if (elementJson.getKey().startsWith(PROPERTY_PREFIX)) { | ||
String propertyName = elementJson.getKey().substring(PROPERTY_PREFIX.length()); | ||
List<String> values = metaproperties.getOrDefault(metaproperties, new ArrayList<>()); | ||
if (elementJson.getValue().isJsonArray()) { | ||
for (JsonElement element : elementJson.getValue().getAsJsonArray()) { | ||
values.add(element.getAsString()); | ||
} | ||
} else { | ||
values.add(elementJson.getValue().getAsString()); | ||
} | ||
metaproperties.put(propertyName, values); | ||
} | ||
} | ||
setMetaproperties(media, metaproperties); | ||
return media; | ||
} | ||
|
||
private void setMetaproperties(Media media, Map<String, List<String>> metaproperties) { | ||
try { | ||
Field metapropertiesField = media.getClass().getDeclaredField(CUSTOM_METAPROPERTY_FIELDNAME); | ||
metapropertiesField.setAccessible(true); | ||
metapropertiesField.set(media, metaproperties); | ||
} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) { | ||
// does not occur unless Media class is changed | ||
} | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
src/test/java/com/bynder/sdk/util/MediaTypeAdapterTest.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,75 @@ | ||
/* | ||
* Copyright (c) 2017 Bynder B.V. All rights reserved. | ||
* | ||
* Licensed under the MIT License. See LICENSE file in the project root for full license | ||
* information. | ||
* | ||
* JUnit framework component copyright (c) 2002-2017 JUnit. All Rights Reserved. Licensed under | ||
* Eclipse Public License - v 1.0. You may obtain a copy of the License at | ||
* https://www.eclipse.org/legal/epl-v10.html. | ||
*/ | ||
package com.bynder.sdk.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.junit.Test; | ||
|
||
import com.bynder.sdk.model.Media; | ||
import com.google.gson.JsonParser; | ||
|
||
/** | ||
* Tests the {@link BooleanTypeAdapter} class methods. | ||
*/ | ||
public class MediaTypeAdapterTest { | ||
|
||
|
||
/** | ||
* Given JSON | ||
* | ||
* { | ||
* "id": "FF5DB884-5665-4127-88D0116D8EB379FE", | ||
* "isPublic": 0, | ||
* "property_Articlenumber": "16773", | ||
* "property_Language": [ | ||
* "Neutral" | ||
* ] | ||
* } | ||
* | ||
*/ | ||
private final String givenApiResponse = "{ \"id\": \"FF5DB884-5665-4127-88D0116D8EB379FE\", \"isPublic\": 0, \"property_Articlenumber\": \"16773\", \"property_Language\": [ \"Neutral\" ]}"; | ||
|
||
|
||
/** | ||
* Tests that | ||
* {@link MediaTypeAdapter#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)} | ||
* correctly converts all "property_" prefixed json fields into a custom Map<String,List<String>> when deserializing the Json response returned by the | ||
* API. | ||
*/ | ||
@Test | ||
public void deserializeWithMediaTypeAdapter() { | ||
MediaTypeAdapter mediaTypeAdapter = new MediaTypeAdapter(); | ||
|
||
Media actualMedia = mediaTypeAdapter.deserialize(JsonParser.parseString(givenApiResponse), null, null); | ||
|
||
// common default field like ID | ||
assertEquals("FF5DB884-5665-4127-88D0116D8EB379FE", actualMedia.getId()); | ||
|
||
// boolean using BooleanTypeAdapter inside MediaTypeAdapter | ||
assertEquals(Boolean.FALSE, actualMedia.isPublic()); | ||
|
||
// new custom "property_" which is returned as string from API# | ||
|
||
assertNotNull(actualMedia.getCustomMetaproperties()); | ||
assertTrue(actualMedia.getCustomMetaproperties().containsKey("Articlenumber")); | ||
assertEquals(Arrays.asList("16773"), actualMedia.getCustomMetaproperties().get("Articlenumber")); | ||
|
||
// new custom "property_" which is returned as array from API | ||
assertNotNull(actualMedia.getCustomMetaproperties()); | ||
assertTrue(actualMedia.getCustomMetaproperties().containsKey("Language")); | ||
assertEquals(Arrays.asList("Neutral"), actualMedia.getCustomMetaproperties().get("Language")); | ||
} | ||
} |