View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.json.transformers;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.api.lifecycle.InitialisationException;
11  import org.mule.api.transformer.DataType;
12  import org.mule.api.transformer.TransformerException;
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.module.json.JsonData;
15  import org.mule.transformer.types.DataTypeFactory;
16  import org.mule.util.IOUtils;
17  
18  import java.io.ByteArrayInputStream;
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.InputStream;
22  import java.io.InputStreamReader;
23  import java.io.Reader;
24  import java.net.URL;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  /**
29   * A transformer that will convert a JSON encoded object graph to a java object. The
30   * object type is determined by the 'returnType' attribute. Note that this
31   * transformers supports Arrays and Lists. For example, to convert a JSON string to
32   * an array of org.foo.Person, set the the returnClass=[Lorg.foo.Person;.
33   */
34  public class JsonToObject extends AbstractJsonTransformer
35  {
36      private static final DataType<JsonData> JSON_TYPE = DataTypeFactory.create(JsonData.class);
37  
38      private Map<Class<?>, Class<?>> deserializationMixins = new HashMap<Class<?>, Class<?>>();
39  
40      public JsonToObject()
41      {
42          this.registerSourceType(DataTypeFactory.create(Reader.class));
43          this.registerSourceType(DataTypeFactory.create(URL.class));
44          this.registerSourceType(DataTypeFactory.create(File.class));
45          this.registerSourceType(DataTypeFactory.STRING);
46          this.registerSourceType(DataTypeFactory.INPUT_STREAM);
47          this.registerSourceType(DataTypeFactory.BYTE_ARRAY);
48          setReturnDataType(JSON_TYPE);
49      }
50  
51      @Override
52      public void initialise() throws InitialisationException
53      {
54          super.initialise();
55          //Add shared mixins first
56          for (Map.Entry<Class<?>, Class<?>> entry : getMixins().entrySet())
57          {
58              getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
59          }
60  
61          for (Map.Entry<Class<?>, Class<?>> entry : deserializationMixins.entrySet())
62          {
63              getMapper().getDeserializationConfig().addMixInAnnotations(entry.getKey(), entry.getValue());
64          }
65      }
66  
67      @Override
68      public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException
69      {
70          Object src = message.getPayload();
71          Object returnValue;
72          InputStream is = null;
73          Reader reader = null;
74  
75          try
76          {
77              if (src instanceof InputStream)
78              {
79                  is = (InputStream) src;
80              }
81              else if (src instanceof File)
82              {
83                  is = new FileInputStream((File) src);
84              }
85              else if (src instanceof URL)
86              {
87                  is = ((URL) src).openStream();
88              }
89              else if (src instanceof byte[])
90              {
91                  is = new ByteArrayInputStream((byte[]) src);
92              }
93  
94              if (src instanceof Reader)
95              {
96                  if (getReturnDataType().equals(JSON_TYPE))
97                  {
98                      returnValue = new JsonData((Reader) src);
99                  }
100                 else
101                 {
102                     returnValue = getMapper().readValue((Reader) src, getReturnDataType().getType());
103                 }
104             }
105             else if (src instanceof String)
106             {
107                 if (getReturnDataType().equals(JSON_TYPE))
108                 {
109                     returnValue = new JsonData((String) src);
110                 }
111                 else
112                 {
113                     returnValue = getMapper().readValue((String) src, getReturnDataType().getType());
114                 }
115             }
116             else
117             {
118                 reader = new InputStreamReader(is, outputEncoding);
119                 if (getReturnDataType().equals(JSON_TYPE))
120                 {
121                     returnValue = new JsonData(reader);
122                 }
123                 else
124                 {
125                     returnValue = getMapper().readValue(reader, getReturnDataType().getType());
126                 }
127             }
128             return returnValue;
129         }
130         catch (Exception e)
131         {
132             throw new TransformerException(CoreMessages.transformFailed("json",
133                 getReturnDataType().getType().getName()), this, e);
134         }
135         finally
136         {
137             IOUtils.closeQuietly(reader);
138             IOUtils.closeQuietly(is);
139         }
140     }
141 
142     public Map<Class<?>, Class<?>> getDeserializationMixins()
143     {
144         return deserializationMixins;
145     }
146 
147     public void setDeserializationMixins(Map<Class<?>, Class<?>> deserializationMixins)
148     {
149         this.deserializationMixins = deserializationMixins;
150     }
151 }