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;
8   
9   import org.mule.routing.AbstractSplitter;
10  
11  import java.util.ArrayList;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.codehaus.jackson.JsonNode;
16  import org.codehaus.jackson.node.ArrayNode;
17  import org.codehaus.jackson.node.ValueNode;
18  
19  /**
20   * An JSON expression evaluator that returns {@link JsonNode}'s instead of strings.
21   * Arrays are still returned as a {@link List} rather than as an {@link ArrayNode} to
22   * enable splitting using the {@link AbstractSplitter}
23   * 
24   * @see org.mule.module.json.JsonData
25   * @see JsonExpressionEvaluator
26   */
27  public class JsonNodeExpressionEvaluator extends JsonExpressionEvaluator
28  {
29  
30      protected Object extractResultFromNode(JsonNode result)
31      {
32          if (result instanceof ValueNode)
33          {
34              return result.getValueAsText();
35          }
36          if (result instanceof ArrayNode)
37          {
38              List parts = new ArrayList<String>();
39              for (Iterator<JsonNode> i = ((JsonNode) result).getElements(); i.hasNext();)
40              {
41                  JsonNode arrayNode = i.next();
42                  parts.add(extractResultFromNode(arrayNode));
43              }
44              return parts;
45          }
46          else
47          {
48              return result;
49          }
50      }
51  
52      public String getName()
53      {
54          return "json-node";
55      }
56  }