-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle duplicate elements as Array when converting XML to JSON
- Loading branch information
1 parent
83459f3
commit 06a33e4
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
...mentcomponents/libraries/rest/sdk/wrapper/utils/DuplicateToArrayJsonNodeDeserializer.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,29 @@ | ||
package com.paymentcomponents.libraries.rest.sdk.wrapper.utils; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.deser.std.JsonNodeDeserializer; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
public class DuplicateToArrayJsonNodeDeserializer extends JsonNodeDeserializer { | ||
|
||
@Override | ||
protected void _handleDuplicateField(JsonParser p, DeserializationContext ctxt, | ||
JsonNodeFactory nodeFactory, String fieldName, ObjectNode objectNode, | ||
JsonNode oldValue, JsonNode newValue) throws JsonProcessingException { | ||
ArrayNode node; | ||
if (oldValue instanceof ArrayNode) { | ||
node = (ArrayNode) oldValue; | ||
node.add(newValue); | ||
} else { | ||
node = nodeFactory.arrayNode(); | ||
node.add(oldValue); | ||
node.add(newValue); | ||
} | ||
objectNode.set(fieldName, node); | ||
} | ||
} |
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