Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: JsonExpressionEvaluator.java 19191 2010-08-25 21:05:23Z tcarlson $
   * --------------------------------------------------------------------------------------
   * 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;
 
 
An expression evaluator to allow users to define json expressions in their mule configuration, i.e. #[json:person/addresses[0]/postcode]

See the JsonData object for mor information about the query syntax.

It is also possible to use this evaluator in org.mule.routing.filters.ExpressionFilter objects. For example a filter could be defined as -

#[json:person/registered]

Where 'registered' is a boolean value. It is also possible to filter on the existence of a value i.e.

#[json:person/favouriteColour]

Which would return true if 'favouriteColour' has been set. This evaluator also dds two logic operators you can use to create more sophisticated boolean expressions; equals and not equals -

#[json:person/favouriteColour = red]

or

#[json:person/favouriteColour != brown]

See also:
JsonData
 
 public class JsonExpressionEvaluator implements ExpressionEvaluator
 {
    
logger used by this class
 
     protected transient final Log logger = LogFactory.getLog(JsonExpressionEvaluator.class);
 
     public Object evaluate(String expressionMuleMessage message)
     {
         String compareTo = null;
         boolean not = false;
         int start = expression.lastIndexOf("/");
         if (start == -1)
         {
             start = 0;
         }
         int i=0;
         if ((i = expression.indexOf("!="start)) > -1)
         {
             compareTo = expression.substring(i + 2, expression.length()).trim();
             expression = expression.substring(0, i).trim();
             not = true;
         }
         else if ((i = expression.indexOf("="start)) > -1)
         {
             compareTo = expression.substring(i + 1, expression.length()).trim();
             expression = expression.substring(0, i).trim();
         }
         
         try
         {
             String json = message.getPayloadAsString();
             JsonData data = new JsonData(json);
             try
             {
                 Object result = data.get(expression);
                 if (compareTo != null)
                 {
                     if (compareTo.equalsIgnoreCase("null"))
                     {
                         boolean answer = result == null;
                         return (not ? !answer : answer);
                     }
                    else if (result instanceof Number && NumberUtils.isDigits(compareTo))
                    {
                        boolean answer = NumberUtils.createNumber(compareTo).equals(result);
                        return (not ? !answer : answer);
                    }
                    else if (result instanceof Boolean && (compareTo.equalsIgnoreCase("true") || compareTo.equalsIgnoreCase("false")))
                    {
                        boolean answer = result.equals(Boolean.valueOf(compareTo));
                        return (not ? !answer : answer);
                    }
                    else
                    {
                        boolean answer = compareTo.equals(result);
                        return (not ? !answer : answer);
                    }
                }
                else
                {
                    return result;
                }
            }
            catch (IllegalArgumentException e)
            {
                if (compareTo == null)
                {
                    .debug("returning null for json expression: " + expression + ": " + e.getMessage());
                    return null;
                }
                //If the element does not exist but is matching against 'null' return true, otherwise false
                return (compareTo.equalsIgnoreCase("null")) & !not;
            }
        }
        catch (Exception e)
        {
            throw new MuleRuntimeException(CoreMessages.failedToProcessExtractorFunction(getName() + ":" + expression), e);
        }
    }
    public void setName(String name)
    {
        throw new UnsupportedOperationException("setName");
    }
    public String getName()
    {
        return "json";
    }
New to GrepCode? Check out our FAQ X