1 /**
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  *      http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */

14 package net.logstash.logback.composite;
15
16 import java.io.IOException;
17 import java.util.Map;
18
19 import net.logstash.logback.fieldnames.LogstashCommonFieldNames;
20
21 import com.fasterxml.jackson.core.JsonGenerator;
22 import com.fasterxml.jackson.databind.JsonMappingException;
23
24 public class JsonWritingUtils {
25
26     /**
27      * Writes entries of the map as fields.
28      */

29     public static void writeMapEntries(JsonGenerator generator, Map<?, ?> map) throws IOException, JsonMappingException {
30         if (map != null) {
31             for (Map.Entry<?, ?> entry : map.entrySet()) {
32                 if (entry.getKey() != null && entry.getValue() != null) {
33                     generator.writeFieldName(entry.getKey().toString());
34                     generator.writeObject(entry.getValue());
35                 }
36             }
37         }
38     }
39
40     /**
41      * Writes a map as String fields to the generator if and only if the fieldName and values are not null.
42      */

43     public static void writeMapStringFields(JsonGenerator generator, String fieldName, Map<String, String> map) throws IOException, JsonMappingException {
44         writeMapStringFields(generator, fieldName, map, false);
45     }
46     
47     /**
48      * Writes a map as String fields to the generator if and only if the fieldName and values are not null.
49      * @param lowerCaseKeys when true, the map keys will be written in lowercase.
50      */

51     public static void writeMapStringFields(JsonGenerator generator, String fieldName, Map<String, String> map, boolean lowerCaseKeys) throws IOException, JsonMappingException {
52         if (shouldWriteField(fieldName) && map != null && !map.isEmpty()) {
53             generator.writeObjectFieldStart(fieldName);
54             for (Map.Entry<String, String> entry : map.entrySet()) {
55                 String key = entry.getKey() != null && lowerCaseKeys
56                         ? entry.getKey().toLowerCase()
57                         : entry.getKey();
58                 writeStringField(generator, key, entry.getValue());
59             }
60             generator.writeEndObject();
61         }
62     }
63     
64     /**
65      * Writes an array of strings to the generator if and only if the fieldName and values are not null.
66      */

67     public static void writeStringArrayField(JsonGenerator generator, String fieldName, String[] fieldValues) throws IOException {
68         if (shouldWriteField(fieldName) && fieldValues != null && fieldValues.length > 0) {
69             generator.writeArrayFieldStart(fieldName);
70             for (String fieldValue : fieldValues) {
71                 generator.writeString(fieldValue);
72             }
73             generator.writeEndArray();
74         }
75     }
76
77     /**
78      * Writes the field to the generator if and only if the fieldName and fieldValue are not null.
79      */

80     public static void writeStringField(JsonGenerator generator, String fieldName, String fieldValue) throws IOException {
81         if (shouldWriteField(fieldName) && fieldValue != null) {
82             generator.writeStringField(fieldName, fieldValue);
83         }
84     }
85
86     /**
87      * Writes the field to the generator if and only if the fieldName is not null.
88      */

89     public static void writeNumberField(JsonGenerator generator, String fieldName, int fieldValue) throws IOException {
90         if (shouldWriteField(fieldName)) {
91             generator.writeNumberField(fieldName, fieldValue);
92         }
93     }
94
95     /**
96      * Writes the field to the generator if and only if the fieldName is not null.
97      */

98     public static void writeNumberField(JsonGenerator generator, String fieldName, long fieldValue) throws IOException {
99         if (shouldWriteField(fieldName)) {
100             generator.writeNumberField(fieldName, fieldValue);
101         }
102     }
103
104     public static boolean shouldWriteField(String fieldName) {
105         return fieldName != null && !fieldName.equals(LogstashCommonFieldNames.IGNORE_FIELD_INDICATOR);
106     }
107
108 }
109