-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Deserialization error when "extensions" contains "extra" field (#10)
* use custom deserialization to handle list extensions * Fix parsing error when extensions contain "extra" field Co-authored-by: Joshua Hight <[email protected]> Co-authored-by: Martin Wilhelm <[email protected]>
- Loading branch information
1 parent
8cd76b6
commit 027705c
Showing
5 changed files
with
102 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
#Intellij's dirty laundry | ||
.idea/* | ||
*.iml | ||
|
||
#maven build artifcts | ||
target/* | ||
|
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
60 changes: 60 additions & 0 deletions
60
src/io/calidog/certstream/CertStreamCertificatePOJODeserializer.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,60 @@ | ||
package io.calidog.certstream; | ||
|
||
import com.google.gson.*; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CertStreamCertificatePOJODeserializer implements JsonDeserializer<CertStreamCertificatePOJO> { | ||
|
||
@Override | ||
public CertStreamCertificatePOJO deserialize( | ||
JsonElement jsonElement, | ||
Type type, | ||
JsonDeserializationContext jsonDeserializationContext | ||
) throws JsonParseException { | ||
|
||
JsonObject jsonObj = jsonElement.getAsJsonObject(); | ||
JsonObject jsonExtensions = new JsonObject(); | ||
|
||
// remove extensions from json object | ||
if (jsonObj.has("extensions")) | ||
{ | ||
jsonExtensions = jsonObj.remove("extensions").getAsJsonObject(); | ||
} | ||
|
||
// parse entry normally | ||
CertStreamCertificatePOJO retVal = new Gson().fromJson(jsonElement, CertStreamCertificatePOJO.class); | ||
|
||
// parse extensions externally | ||
retVal.extensions = deserializeExtension(jsonExtensions); | ||
|
||
return retVal; | ||
} | ||
|
||
private HashMap<String, String[]> deserializeExtension(JsonObject extensions){ | ||
final HashMap<String, String[]> finalMap = new HashMap<>(extensions.size()); | ||
|
||
extensions.entrySet().forEach((Map.Entry<String, JsonElement> entry) -> { | ||
String key = entry.getKey(); | ||
JsonElement value = entry.getValue(); | ||
String[] extensionValues; | ||
|
||
try { | ||
extensionValues = new String[] { value.getAsString() }; | ||
} catch (IllegalStateException | UnsupportedOperationException e) { | ||
JsonArray extensionJsonArray = value.getAsJsonArray(); | ||
extensionValues = new String[extensionJsonArray.size()]; | ||
|
||
for (int i = 0; i < extensionJsonArray.size(); i++) { | ||
extensionValues[i] = extensionJsonArray.get(i).getAsString(); | ||
} | ||
} | ||
|
||
finalMap.put(key, extensionValues); | ||
}); | ||
|
||
return finalMap; | ||
} | ||
} |