001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with 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     */
018    
019    package org.apache.hadoop.yarn.util.timeline;
020    
021    import java.io.IOException;
022    
023    import org.apache.hadoop.classification.InterfaceAudience.Public;
024    import org.apache.hadoop.classification.InterfaceStability.Evolving;
025    import org.codehaus.jackson.JsonGenerationException;
026    import org.codehaus.jackson.map.AnnotationIntrospector;
027    import org.codehaus.jackson.map.JsonMappingException;
028    import org.codehaus.jackson.map.ObjectMapper;
029    import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
030    import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;
031    
032    /**
033     * The helper class for the timeline module.
034     * 
035     */
036    @Public
037    @Evolving
038    public class TimelineUtils {
039    
040      private static ObjectMapper mapper;
041    
042      static {
043        mapper = new ObjectMapper();
044        AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
045        mapper.setAnnotationIntrospector(introspector);
046        mapper.getSerializationConfig()
047            .setSerializationInclusion(Inclusion.NON_NULL);
048      }
049    
050      /**
051       * Serialize a POJO object into a JSON string not in a pretty format
052       * 
053       * @param o
054       *          an object to serialize
055       * @return a JSON string
056       * @throws IOException
057       * @throws JsonMappingException
058       * @throws JsonGenerationException
059       */
060      public static String dumpTimelineRecordtoJSON(Object o)
061          throws JsonGenerationException, JsonMappingException, IOException {
062        return dumpTimelineRecordtoJSON(o, false);
063      }
064    
065      /**
066       * Serialize a POJO object into a JSON string
067       * 
068       * @param o
069       *          an object to serialize
070       * @param pretty
071       *          whether in a pretty format or not
072       * @return a JSON string
073       * @throws IOException
074       * @throws JsonMappingException
075       * @throws JsonGenerationException
076       */
077      public static String dumpTimelineRecordtoJSON(Object o, boolean pretty)
078          throws JsonGenerationException, JsonMappingException, IOException {
079        if (pretty) {
080          return mapper.defaultPrettyPrintingWriter().writeValueAsString(o);
081        } else {
082          return mapper.writeValueAsString(o);
083        }
084      }
085    
086    }