View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2023, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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  }