1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.classic.jsonTest;
16
17 import com.fasterxml.jackson.core.JacksonException;
18 import com.fasterxml.jackson.core.JsonParser;
19 import com.fasterxml.jackson.databind.DeserializationContext;
20 import com.fasterxml.jackson.databind.JsonNode;
21 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
22 import org.slf4j.event.KeyValuePair;
23
24 import java.io.IOException;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 public class KeyValuePairDeserializer extends StdDeserializer<KeyValuePair> {
29 public KeyValuePairDeserializer() {
30 this(null);
31 }
32
33
34 public KeyValuePairDeserializer(Class<?> vc) {
35 super(vc);
36 }
37
38
39 @Override
40 public KeyValuePair deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
41 throws IOException, JacksonException {
42
43 JsonNode node = jsonParser.getCodec().readTree(jsonParser);
44
45 if(node.isObject()) {
46 Iterator<Map.Entry<String, JsonNode>> it = node.fields();
47 if(it.hasNext()) {
48 Map.Entry<String, JsonNode> entry = it.next();
49 String key = entry.getKey();
50 String value = entry.getValue().asText();
51 KeyValuePair kvp = new KeyValuePair(key, value);
52 return kvp;
53 }
54 }
55
56 return null;
57 }
58 }