Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: JsonToObject.java 19715 2010-09-24 15:08:06Z 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.io.File;
 import java.io.Reader;
 import java.net.URL;
 import java.util.Map;


A transformer that will convert a JSON encoded object graph to a java object. The object type is determined by the 'returnClass' attribute. Note that this transformers supports Arrays and Lists. For example, to convert a JSON string to an array of org.foo.Person, set the the returnClass=[Lorg.foo.Person;.

The JSON engine can be configured using the jsonConfig attribute. This is an object reference to an instance of: net.sf.json.JsonConfig. This can be created as a spring bean.

 
 public class JsonToObject extends AbstractJsonTransformer
 {
     private static final DataType<JsonDataJSON_TYPE = DataTypeFactory.create(JsonData.class);
     
     private Map<Class<?>, Class<?>> deserializationMixins = new HashMap<Class<?>, Class<?>>();
     
     public JsonToObject()
     {
         this.registerSourceType(DataTypeFactory.create(Reader.class));
         this.registerSourceType(DataTypeFactory.create(URL.class));
         this.registerSourceType(DataTypeFactory.create(File.class));
         setReturnDataType();
     }
 
     @Override
     public void initialise() throws InitialisationException
     {
         super.initialise();
         //Add shared mixins first
         for (Map.Entry<Class<?>, Class<?>> entry : getMixins().entrySet())
         {
             getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
         }
 
         for (Map.Entry<Class<?>, Class<?>> entry : .entrySet())
         {
             getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
         }
     }
 
     @Override
     public Object transformMessage(MuleMessage messageString outputEncodingthrows TransformerException
     {
         Object src = message.getPayload();
         Object returnValue;
         InputStream is = null;
         Reader reader = null;
 
         try
         {
             if (src instanceof InputStream)
             {
                 is = (InputStreamsrc;
             }
             else if (src instanceof File)
             {
                 is = new FileInputStream((Filesrc);
             }
             else if (src instanceof URL)
             {
                 is = ((URLsrc).openStream();
             }
             else if (src instanceof byte[])
             {
                 is = new ByteArrayInputStream((byte[]) src);
             }
            
            if (src instanceof Reader)
            {
                if (getReturnDataType().equals())
                {
                    returnValue = new JsonData((Readersrc);
                }
                else
                {
                    returnValue = getMapper().readValue((ReadersrcgetReturnDataType().getType());
                }
            }
            else if (src instanceof String)
            {
                if (getReturnDataType().equals())
                {
                    returnValue = new JsonData((Stringsrc);
                }
                else
                {
                    returnValue = getMapper().readValue((StringsrcgetReturnDataType().getType());
                }
            }
            else
            {
                reader = new InputStreamReader(isoutputEncoding);
                if (getReturnDataType().equals())
                {
                    returnValue = new JsonData(reader);
                }
                else
                {
                    returnValue = getMapper().readValue(readergetReturnDataType().getType());
                }
            }
            return returnValue;
        }
        catch (Exception e)
        {
            throw new TransformerException(CoreMessages.transformFailed("json",
                getReturnDataType().getType().getName()), thise);
        }
        finally
        {
            IOUtils.closeQuietly(reader);
            IOUtils.closeQuietly(is);
        }
    }
    public Map<Class<?>, Class<?>> getDeserializationMixins()
    {
        return ;
    }
    public void setDeserializationMixins(Map<Class<?>, Class<?>> deserializationMixins)
    {
        this. = deserializationMixins;
    }
New to GrepCode? Check out our FAQ X