-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for multi-valued cookies
- Loading branch information
1 parent
336fc23
commit 827e7cc
Showing
12 changed files
with
257 additions
and
37 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/github/tomakehurst/wiremock/common/ListOrSingleSerialiser.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,33 @@ | ||
package com.github.tomakehurst.wiremock.common; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.type.CollectionType; | ||
import com.fasterxml.jackson.databind.type.TypeFactory; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
public class ListOrSingleSerialiser extends JsonSerializer<ListOrSingle<Object>> { | ||
|
||
@Override | ||
public void serialize(ListOrSingle<Object> value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { | ||
if (value.isEmpty()) { | ||
gen.writeStartArray(); | ||
gen.writeEndArray(); | ||
return; | ||
} | ||
|
||
Object firstValue = value.first(); | ||
if (value.isSingle()) { | ||
JsonSerializer<Object> serializer = serializers.findValueSerializer(firstValue.getClass()); | ||
serializer.serialize(firstValue, gen, serializers); | ||
} else { | ||
CollectionType type = TypeFactory.defaultInstance().constructCollectionType(List.class, firstValue.getClass()); | ||
JsonSerializer<Object> serializer = serializers.findValueSerializer(type); | ||
serializer.serialize(value, gen, serializers); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/github/tomakehurst/wiremock/common/ListOrStringDeserialiser.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,42 @@ | ||
package com.github.tomakehurst.wiremock.common; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
|
||
import java.io.IOException; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
|
||
public class ListOrStringDeserialiser<T> extends JsonDeserializer<ListOrSingle<T>> { | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public ListOrSingle<T> deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException, JsonProcessingException { | ||
JsonNode rootNode = parser.readValueAsTree(); | ||
if (rootNode.isArray()) { | ||
ArrayNode arrayNode = (ArrayNode) rootNode; | ||
List<T> items = newArrayList(); | ||
for (Iterator<JsonNode> i = arrayNode.elements(); i.hasNext();) { | ||
JsonNode node = i.next(); | ||
Object value = getValue(node); | ||
items.add((T) value); | ||
} | ||
|
||
return new ListOrSingle<>(items); | ||
} | ||
|
||
return new ListOrSingle<>((T) getValue(rootNode)); | ||
} | ||
|
||
private static Object getValue(JsonNode node) { | ||
return node.isTextual() ? node.textValue() : | ||
node.isNumber() ? node.numberValue() : | ||
node.isBoolean() ? node.booleanValue() : node.textValue(); | ||
} | ||
} |
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
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
Oops, something went wrong.