Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: ObjectToJson.java 19713 2010-09-24 14:54:29Z dirk.olmes $
   * --------------------------------------------------------------------------------------
   * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
   *
   * The software in this package is published under the terms of the CPAL v1.0
   * license, a copy of which has been included with this distribution in the
   * LICENSE.txt file.
   */
 package org.mule.module.json.transformers;
 
 
 import java.util.List;
 import java.util.Map;
 
Converts a java object to a JSON encoded object that can be consumed by other languages such as Javascript or Ruby.

The returnClass for this transformer is always java.lang.String, there is no need to set this.

 
 public class ObjectToJson extends AbstractJsonTransformer
 {
    
logger used by this class
 
     protected transient final Log logger = LogFactory.getLog(ObjectToJson.class);
 
     private Map<Class<?>, Class<?>> serializationMixins = new HashMap<Class<?>, Class<?>>();
 
     protected Class<?> sourceClass;
 
     private boolean handleException = false;
 
     private IsJsonFilter isJsonFilter = new IsJsonFilter();
 
     public ObjectToJson()
     {
     }
 
     @Override
     public void initialise() throws InitialisationException
     {
         super.initialise();
 
         //restrict the handled types
         if (getSourceClass() != null)
         {
             .clear();
             registerSourceType(DataTypeFactory.create(getSourceClass()));
         }
 
         //Add shared mixins first
         for (Map.Entry<Class<?>, Class<?>> entry : getMixins().entrySet())
         {
             getMapper().getSerializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
         }
 
         for (Map.Entry<Class<?>, Class<?>> entry : .entrySet())
         {
             getMapper().getSerializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
         }
     }
 
     @Override
     public Object transformMessage(MuleMessage messageString outputEncodingthrows TransformerException
     {
         Object src = message.getPayload();
         if (src instanceof String && .accept(src))
         {
             //Nothing to transform
             return src;
         }
 
         // Checks if there's an exception
         if (message.getExceptionPayload() != null && this.isHandleException())
         {
             if (.isDebugEnabled())
             {
                 .debug("Found exception with null payload");
             }
             src = this.getException(message.getExceptionPayload().getException());
         }
 
         StringWriter writer = new StringWriter();
        try
        {
            getMapper().writeValue(writersrc);
        }
        catch (IOException e)
        {
            throw new TransformerException(thise);
        }
        
        if (.getType().equals(byte[].class))
        {
            try
            {
                return writer.toString().getBytes(outputEncoding);
            }
            catch (UnsupportedEncodingException uee)
            {
                throw new TransformerException(thisuee);
            }
        }
        else
        {
            return writer.toString();
        }
    }

    
The reason of having this is because the original exception object is way too complex and it breaks JSON-lib.
    private Exception getException(Throwable t)
    {
        Exception returnValue = null;
        List<ThrowablecauseStack = new ArrayList<Throwable>();
        for (Throwable tempCause = ttempCause != nulltempCause = tempCause.getCause())
        {
            causeStack.add(tempCause);
        }
        for (int i = causeStack.size() - 1; i >= 0; i--)
        {
            Throwable tempCause = causeStack.get(i);
            // There is no cause at the very root
            if (i == causeStack.size())
            {
                returnValue = new Exception(tempCause.getMessage());
                returnValue.setStackTrace(tempCause.getStackTrace());
            }
            else
            {
                returnValue = new Exception(tempCause.getMessage(), returnValue);
                returnValue.setStackTrace(tempCause.getStackTrace());
            }
        }
        return returnValue;
    }
    public boolean isHandleException()
    {
        return this.;
    }
    public void setHandleException(boolean handleException)
    {
        this. = handleException;
    }
    public Class<?> getSourceClass()
    {
        return ;
    }
    public void setSourceClass(Class<?> sourceClass)
    {
        this. = sourceClass;
    }
    public Map<Class<?>, Class<?>> getSerializationMixins()
    {
        return ;
    }
    public void setSerializationMixins(Map<Class<?>, Class<?>> serializationMixins)
    {
        this. = serializationMixins;
    }
New to GrepCode? Check out our FAQ X