Skip to content

substring function #671

@curtis-cantrell

Description

@curtis-cantrell

I'd love to contribute a substring function, but I see a couple of pull request that already deal with extending function capability that has been hanging around for a few years.

Example: Adding ability to define Functions #354

So, I'm not sure if add this would be used or not...

In a project I"m working on uses jayway, and we have an json file that has a property that is a series of bytes represented as a String. Kind'of like this: "bytes: "000100010010"

I'd like to be able to get just a single character... with substring(3,4,$.bytes)

I added a Substring function, updated the PathFunctionFactory, and wrote unit test... What are the chances something like this could be added?

Thank you,
Curtis Cantrell

package com.jayway.jsonpath.internal.function.text;

import java.util.List;

import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.function.Parameter;
import com.jayway.jsonpath.internal.function.PathFunction;

public class Substring implements PathFunction {
	
	/**
	 * Returns a new string that is a substring of this string. 
	 * <p>
	 * The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. 
	 * <p>
	 * Thus the length of the substring is endIndex-beginIndex. 
	 * <p>
	 * In other words you can say that beginIndex is inclusive and endIndex is exclusive while getting the substring.
	 */
	@Override
    public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
		
        String retVal = null;
        
        if (parameters != null) {
        	
        	 if (parameters.size() != 3)
        	 {
        		 throw new RuntimeException("Function substring must have 3 parameters 'substring(int, int, string)'");
        	 }
        		 
        	 Parameter begin = parameters.get(0);
        	 Parameter end = parameters.get(1);
        	 Parameter text = parameters.get(2);
        	 
        	 if (!(begin.getValue() instanceof Integer))
        	 {
        		 throw new RuntimeException("The begining index must be a number");
        	 }
        	 
        	 if (!(end.getValue() instanceof Integer))
        	 {
        		 throw new RuntimeException("The ending index must be a number");
        	 }

        	 if (!(text.getValue() instanceof String))
        	 {
        		 throw new RuntimeException("Function substring must be invoked with a String parameter");
        	 }
        	 
        	 Integer beginingIndex = (Integer)begin.getValue();
        	 Integer endingIndex = (Integer)end.getValue();
        	 String  textString = (String)text.getValue();
        	 
        	 retVal =  textString.substring(beginingIndex,  endingIndex);
        	 
        }
        return retVal;
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions