001 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache license, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the license for the specific language governing permissions and 016 * limitations under the license. 017 */ 018package org.apache.logging.log4j.core.jackson; 019 020import java.io.IOException; 021import java.util.Map; 022 023import org.apache.logging.log4j.core.impl.ContextDataFactory; 024import org.apache.logging.log4j.util.StringMap; 025 026import com.fasterxml.jackson.core.JsonParser; 027import com.fasterxml.jackson.core.JsonProcessingException; 028import com.fasterxml.jackson.core.JsonToken; 029import com.fasterxml.jackson.databind.DeserializationContext; 030import com.fasterxml.jackson.databind.deser.std.StdDeserializer; 031 032/** 033 * <p> 034 * <em>Consider this class private.</em> 035 * </p> 036 */ 037public class ContextDataDeserializer extends StdDeserializer<StringMap> { 038 039 private static final long serialVersionUID = 1L; 040 041 ContextDataDeserializer() { 042 super(Map.class); 043 } 044 045 @Override 046 public StringMap deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, 047 JsonProcessingException { 048 049 // Sanity check: verify that we got "Json Object": 050// JsonToken tok = jp.nextToken(); 051// if (tok != JsonToken.START_OBJECT) { 052// throw new IOException("Expected data to start with an Object"); 053// } 054 final StringMap contextData = ContextDataFactory.createContextData(); 055 // Iterate over object fields: 056 while (jp.nextToken() != JsonToken.END_OBJECT) { 057 final String fieldName = jp.getCurrentName(); 058 059 // move to value 060 jp.nextToken(); 061 contextData.putValue(fieldName, jp.getText()); 062 } 063 return contextData; 064 } 065}