1
2 /*
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache license, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the license for the specific language governing permissions and
16 * limitations under the license.
17 */
18 package org.apache.logging.log4j.core.jackson;
19
20 import java.io.IOException;
21 import java.util.Map;
22
23 import org.apache.logging.log4j.core.impl.ContextDataFactory;
24 import org.apache.logging.log4j.util.StringMap;
25
26 import com.fasterxml.jackson.core.JsonParser;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.fasterxml.jackson.core.JsonToken;
29 import com.fasterxml.jackson.databind.DeserializationContext;
30 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
31
32 /**
33 * <p>
34 * <em>Consider this class private.</em>
35 * </p>
36 */
37 public class ContextDataDeserializer extends StdDeserializer<StringMap> {
38
39 private static final long serialVersionUID = 1L;
40
41 ContextDataDeserializer() {
42 super(Map.class);
43 }
44
45 @Override
46 public StringMap deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException,
47 JsonProcessingException {
48
49 // Sanity check: verify that we got "Json Object":
50 // JsonToken tok = jp.nextToken();
51 // if (tok != JsonToken.START_OBJECT) {
52 // throw new IOException("Expected data to start with an Object");
53 // }
54 final StringMap contextData = ContextDataFactory.createContextData();
55 // Iterate over object fields:
56 while (jp.nextToken() != JsonToken.END_OBJECT) {
57 final String fieldName = jp.getCurrentName();
58
59 // move to value
60 jp.nextToken();
61 contextData.putValue(fieldName, jp.getText());
62 }
63 return contextData;
64 }
65 }