com.jayway.jsonpath.Option Java Examples

The following examples show how to use com.jayway.jsonpath.Option. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: micro-integrator   Author: wso2   File: EnrichIntegrationJsonPathTestCase.java    License: Apache License 2.0 7 votes vote down vote up
private void setJsonPathConfiguration() {
    Configuration.setDefaults(new Configuration.Defaults() {

        private final JsonProvider jsonProvider = new GsonJsonProvider(new GsonBuilder().serializeNulls().create());
        private final MappingProvider mappingProvider = new GsonMappingProvider();

        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #2
Source Project: Quicksql   Author: Qihoo360   File: JsonFunctions.java    License: MIT License 6 votes vote down vote up
public static String jsonRemove(JsonValueContext input, String... pathSpecs) {
  try {
    DocumentContext ctx = JsonPath.parse(input.obj,
        Configuration
            .builder()
            .options(Option.SUPPRESS_EXCEPTIONS)
            .jsonProvider(JSON_PATH_JSON_PROVIDER)
            .mappingProvider(JSON_PATH_MAPPING_PROVIDER)
            .build());
    for (String pathSpec : pathSpecs) {
      if ((pathSpec != null) && (ctx.read(pathSpec) != null)) {
        ctx.delete(pathSpec);
      }
    }
    return ctx.jsonString();
  } catch (Exception ex) {
    throw RESOURCE.invalidInputForJsonRemove(
        input.toString(), Arrays.toString(pathSpecs)).ex();
  }
}
 
Example #3
Source Project: hop   Author: project-hop   File: FastJsonReader.java    License: Apache License 2.0 6 votes vote down vote up
private Configuration deleteOptionFromConfiguration( Configuration config, Option option ) {
  Configuration currentConf = config;
  if ( currentConf != null ) {
    EnumSet<Option> currentOptions = EnumSet.noneOf( Option.class );
    currentOptions.addAll( currentConf.getOptions() );
    if ( currentOptions.remove( option ) ) {
      if ( log.isDebug() ) {
        log.logDebug( BaseMessages.getString( PKG, "JsonReader.Debug.Configuration.Option.Delete", option ) );
      }
      currentConf = Configuration.defaultConfiguration().addOptions( currentOptions.toArray( new Option[currentOptions.size()] ) );
    }
  }
  if ( log.isDebug() ) {
    log.logDebug( BaseMessages.getString( PKG, "JsonReader.Debug.Configuration.Options", currentConf.getOptions() ) );
  }
  return currentConf;
}
 
Example #4
Source Project: spring-cloud-app-broker   Author: spring-cloud   File: CloudFoundryAcceptanceTest.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeEach
void configureJsonPath() {
	Configuration.setDefaults(new Configuration.Defaults() {
		private final JsonProvider jacksonJsonProvider = new JacksonJsonProvider();

		private final MappingProvider jacksonMappingProvider = new JacksonMappingProvider();

		@Override
		public JsonProvider jsonProvider() {
			return jacksonJsonProvider;
		}

		@Override
		public MappingProvider mappingProvider() {
			return jacksonMappingProvider;
		}

		@Override
		public Set<Option> options() {
			return EnumSet.noneOf(Option.class);
		}
	});
}
 
Example #5
Source Project: rmlmapper-java   Author: RMLio   File: JSONRecordFactory.java    License: MIT License 6 votes vote down vote up
/**
 * This method returns the records from a JSON document based on an iterator.
 * @param document the document from which records need to get.
 * @param iterator the used iterator.
 * @return a list of records.
 * @throws IOException
 */
@Override
List<Record> getRecordsFromDocument(Object document, String iterator) throws IOException {
    List<Record> records = new ArrayList<>();

    Configuration conf = Configuration.builder()
            .options(Option.AS_PATH_LIST).build();

    try {
        List<String> pathList = JsonPath.using(conf).parse(document).read(iterator);

        for(String p :pathList) {
            records.add(new JSONRecord(document, p));
        }
    } catch(PathNotFoundException e) {
        logger.warn(e.getMessage(), e);
    }

    return records;
}
 
Example #6
Source Project: interface-test   Author: QianmiOpen   File: InterfaceTestApplication.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("WeakerAccess")
public void init(){
    // jsonpath配置初始化
    com.jayway.jsonpath.Configuration.setDefaults(new com.jayway.jsonpath.Configuration.Defaults() {

        private final JsonProvider jsonProvider = new JacksonJsonProvider();
        private final MappingProvider mappingProvider = new JacksonMappingProvider();

        @Override
        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        @Override
        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        @Override
        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #7
Source Project: interface-test   Author: QianmiOpen   File: TestCaseLoaderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    com.jayway.jsonpath.Configuration.setDefaults(new com.jayway.jsonpath.Configuration.Defaults() {
        private final JsonProvider jsonProvider = new JacksonJsonProvider();
        private final MappingProvider mappingProvider = new JacksonMappingProvider();

        @Override
        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        @Override
        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        @Override
        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #8
Source Project: light-4j   Author: networknt   File: MaskTest.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void runOnceBeforeClass() {
    Configuration.setDefaults(new Configuration.Defaults() {

        private final JsonProvider jsonProvider = new JacksonJsonProvider();
        private final MappingProvider mappingProvider = new JacksonMappingProvider();

        @Override
        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        @Override
        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        @Override
        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #9
Source Project: light-4j   Author: networknt   File: JsonPathStartupHookProvider.java    License: Apache License 2.0 6 votes vote down vote up
static void configJsonPath() {
    Configuration.setDefaults(new Configuration.Defaults() {

        private final JsonProvider jsonProvider = new JacksonJsonProvider();
        private final MappingProvider mappingProvider = new JacksonMappingProvider();

        @Override
        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        @Override
        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        @Override
        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #10
Source Project: product-ei   Author: wso2   File: ForEachnativeJSONTestCase.java    License: Apache License 2.0 6 votes vote down vote up
private void setJsonPathConfiguration() {
    Configuration.setDefaults(new Configuration.Defaults() {

        private final JsonProvider jsonProvider = new GsonJsonProvider(new GsonBuilder().serializeNulls().create());
        private final MappingProvider mappingProvider = new GsonMappingProvider();

        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #11
Source Project: product-ei   Author: wso2   File: EnrichIntegrationJsonPathTestCase.java    License: Apache License 2.0 6 votes vote down vote up
private void setJsonPathConfiguration() {
    Configuration.setDefaults(new Configuration.Defaults() {

        private final JsonProvider jsonProvider = new GsonJsonProvider(new GsonBuilder().serializeNulls().create());
        private final MappingProvider mappingProvider = new GsonMappingProvider();

        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #12
Source Project: querqy   Author: querqy   File: PropertiesBuilder.java    License: Apache License 2.0 6 votes vote down vote up
public PropertiesBuilder() {
    objectMapper = new ObjectMapper();
    objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
    objectMapper.configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true);
    objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
    objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
    objectMapper.configure(JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS, true);
    objectMapper.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true);



    jsonPathConfiguration = Configuration.builder()
            .jsonProvider(new JacksonJsonProvider())
            .mappingProvider(new JacksonMappingProvider()).build();
    jsonPathConfiguration.addOptions(Option.ALWAYS_RETURN_LIST);

    primitiveProperties = new HashMap<>();
    jsonObjectString =  new StringBuilder();

}
 
Example #13
Source Project: carbon-apimgt   Author: wso2   File: TestSchemaValidator.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void init() {
    // Set GsonJsonProvider as the default Jayway JSON path default configuration
    // Which is set by synapse-core at runtime of the server
    Configuration.setDefaults(new Configuration.Defaults() {
        private final JsonProvider jsonProvider = new GsonJsonProvider(new GsonBuilder().serializeNulls().create());
        private final MappingProvider mappingProvider = new GsonMappingProvider();

        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #14
Source Project: calcite   Author: apache   File: JsonFunctions.java    License: Apache License 2.0 6 votes vote down vote up
public static String jsonRemove(JsonValueContext input, String... pathSpecs) {
  try {
    DocumentContext ctx = JsonPath.parse(input.obj,
        Configuration
            .builder()
            .options(Option.SUPPRESS_EXCEPTIONS)
            .jsonProvider(JSON_PATH_JSON_PROVIDER)
            .mappingProvider(JSON_PATH_MAPPING_PROVIDER)
            .build());
    for (String pathSpec : pathSpecs) {
      if ((pathSpec != null) && (ctx.read(pathSpec) != null)) {
        ctx.delete(pathSpec);
      }
    }
    return ctx.jsonString();
  } catch (Exception ex) {
    throw RESOURCE.invalidInputForJsonRemove(
        input.toString(), Arrays.toString(pathSpecs)).ex();
  }
}
 
Example #15
Source Project: light   Author: networknt   File: LightServer.java    License: Apache License 2.0 6 votes vote down vote up
static void configJsonPath() {
    Configuration.setDefaults(new Configuration.Defaults() {

        private final JsonProvider jsonProvider = new JacksonJsonProvider();
        private final MappingProvider mappingProvider = new JacksonMappingProvider();

        @Override
        public JsonProvider jsonProvider() {
            return jsonProvider;
        }

        @Override
        public MappingProvider mappingProvider() {
            return mappingProvider;
        }

        @Override
        public Set<Option> options() {
            return EnumSet.noneOf(Option.class);
        }
    });
}
 
Example #16
Source Project: pom-manipulation-ext   Author: release-engineering   File: JSONIO.java    License: Apache License 2.0 6 votes vote down vote up
public DocumentContext parseJSON( File jsonFile ) throws ManipulationException
{
    if ( jsonFile == null || !jsonFile.exists() )
    {
        throw new ManipulationException( "JSON File not found" );
    }

    this.encoding = detectEncoding( jsonFile );
    this.charset = Charset.forName( encoding.getJavaName() );
    this.jsonFile = jsonFile;
    this.eol = detectEOL( jsonFile );

    DocumentContext doc;

    try ( FileInputStream in = new FileInputStream( jsonFile ) )
    {
        Configuration conf = Configuration.builder().options( Option.ALWAYS_RETURN_LIST ).build();
        doc = JsonPath.using( conf ).parse( in, charset.name() );
    }
    catch ( IOException e )
    {
        logger.error( "Unable to parse JSON File", e );
        throw new ManipulationException( "Unable to parse JSON File", e );
    }
    return doc;
}
 
Example #17
Source Project: tutorials   Author: eugenp   File: ServiceIntegrationTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenStructure_whenRequestingHighestRevenueMovieTitle_thenSucceed() {
    DocumentContext context = JsonPath.parse(jsonString);
    List<Object> revenueList = context.read("$[*]['box office']");
    Integer[] revenueArray = revenueList.toArray(new Integer[0]);
    Arrays.sort(revenueArray);

    int highestRevenue = revenueArray[revenueArray.length - 1];
    Configuration pathConfiguration = Configuration.builder()
        .options(Option.AS_PATH_LIST)
        .build();
    List<String> pathList = JsonPath.using(pathConfiguration)
        .parse(jsonString)
        .read("$[?(@['box office'] == " + highestRevenue + ")]");

    Map<String, String> dataRecord = context.read(pathList.get(0));
    String title = dataRecord.get("title");

    assertEquals("Skyfall", title);
}
 
Example #18
Source Project: pentaho-kettle   Author: pentaho   File: FastJsonReader.java    License: Apache License 2.0 6 votes vote down vote up
private Configuration deleteOptionFromConfiguration( Configuration config, Option option ) {
  Configuration currentConf = config;
  if ( currentConf != null ) {
    EnumSet<Option> currentOptions = EnumSet.noneOf( Option.class );
    currentOptions.addAll( currentConf.getOptions() );
    if ( currentOptions.remove( option ) ) {
      if ( log.isDebug() ) {
        log.logDebug( BaseMessages.getString( PKG, "JsonReader.Debug.Configuration.Option.Delete", option ) );
      }
      currentConf = Configuration.defaultConfiguration().addOptions( currentOptions.toArray( new Option[currentOptions.size()] ) );
    }
  }
  if ( log.isDebug() ) {
    log.logDebug( BaseMessages.getString( PKG, "JsonReader.Debug.Configuration.Options", currentConf.getOptions() ) );
  }
  return currentConf;
}
 
Example #19
Source Project: camel-quarkus   Author: apache   File: JsonPathTestRoute.java    License: Apache License 2.0 5 votes vote down vote up
public static String getName(@JsonPath("person.firstName") String first,
        @JsonPath(value = "person.middleName", options = Option.SUPPRESS_EXCEPTIONS) String middle,
        @JsonPath("person.lastName") String last) {
    if (middle != null) {
        return first + " " + middle + " " + last;
    }
    return first + " " + last;
}
 
Example #20
Source Project: client-encryption-java   Author: Mastercard   File: FieldLevelEncryption.java    License: MIT License 5 votes vote down vote up
/**
 * Specify the JSON engine to be used.
 * @param jsonEngine A {@link com.mastercard.developer.json.JsonEngine} instance
 */
public static synchronized Configuration withJsonEngine(JsonEngine jsonEngine) {
    FieldLevelEncryption.jsonEngine = jsonEngine;
    FieldLevelEncryption.jsonPathConfig = new Configuration.ConfigurationBuilder()
            .jsonProvider(jsonEngine.getJsonProvider())
            .options(Option.SUPPRESS_EXCEPTIONS)
            .build();
    return jsonPathConfig;
}
 
Example #21
Source Project: hop   Author: project-hop   File: FastJsonReader.java    License: Apache License 2.0 5 votes vote down vote up
private void setDefaultPathLeafToNull( boolean value ) {
  if ( value != this.defaultPathLeafToNull ) {
    this.defaultPathLeafToNull = value;
    if ( !this.defaultPathLeafToNull ) {
      this.jsonConfiguration = deleteOptionFromConfiguration( this.jsonConfiguration, Option.DEFAULT_PATH_LEAF_TO_NULL );
    }
  }
}
 
Example #22
Source Project: elasticsearch-migration   Author: hubrick   File: CreateIndexMigration.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, String> getParameters() {
    final Integer numberOfReplicas = (Integer) Optional.ofNullable(JsonPath.parse(definition, com.jayway.jsonpath.Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS)).read("$.settings.number_of_replicas")).orElse(0) + 1;
    return ImmutableMap.of(
            "wait_for_active_shards", numberOfReplicas.toString()
    );
}
 
Example #23
Source Project: HaoReader   Author: Invinciblelee   File: MApplication.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    try {
        versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
        versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        versionCode = 0;
        versionName = "0.0.0";
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannelIdDownload();
        createChannelIdReadAloud();
        createChannelIdAudioBook();
        createChannelIdWeb();
    }

    RxJavaPlugins.setErrorHandler(throwable -> {
        if (DEBUG) {
            throwable.printStackTrace();
        }
    });

    ContextHolder.initialize(this);

    CrashHandler.getInstance().install();

    Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);

    boolean nightTheme = AppConfigHelper.get().getPreferences().getBoolean("nightTheme", false);
    AppCompatDelegate.setDefaultNightMode(nightTheme ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
}
 
Example #24
Source Project: interface-test   Author: QianmiOpen   File: NashornTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
    public void test() throws ScriptException {
        com.jayway.jsonpath.Configuration.setDefaults(new com.jayway.jsonpath.Configuration.Defaults() {

            private final JsonProvider jsonProvider = new JacksonJsonProvider();
            private final MappingProvider mappingProvider = new JacksonMappingProvider();

            @Override
            public JsonProvider jsonProvider() {
                return jsonProvider;
            }

            @Override
            public MappingProvider mappingProvider() {
                return mappingProvider;
            }

            @Override
            public Set<Option> options() {
                return EnumSet.noneOf(Option.class);
            }
        });
//        DocumentContext doc = JsonPath.parse(this.getClass().getClassLoader().getResourceAsStream("nashorn.js")); //InvalidJsonException
//        TestSuit testSuit = doc.read("$", TestSuit.class);

        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");

        nashorn.eval(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("nashorn.js")));//ScriptException->ParserException

        Object obj = nashorn.eval("JSON.stringify(testSuit)");

        System.out.println(obj);


    }
 
Example #25
Source Project: conductor   Author: Netflix   File: ParametersUtils.java    License: Apache License 2.0 5 votes vote down vote up
public Map<String, Object> replace(Map<String, Object> input, Object json) {
    Object doc;
    if (json instanceof String) {
        doc = JsonPath.parse(json.toString());
    } else {
        doc = json;
    }
    Configuration option = Configuration.defaultConfiguration().addOptions(Option.SUPPRESS_EXCEPTIONS);
    DocumentContext documentContext = JsonPath.parse(doc, option);
    return replace(input, documentContext, null);
}
 
Example #26
Source Project: james-project   Author: apache   File: HttpClient.java    License: Apache License 2.0 5 votes vote down vote up
public void post(String requestBody) throws Exception {
    response = Request.Post(baseUri(mainStepdefs.jmapServer).setPath("/jmap").build())
        .addHeader("Authorization", userStepdefs.authenticate(userStepdefs.getConnectedUser()).asString())
        .addHeader("Accept", org.apache.http.entity.ContentType.APPLICATION_JSON.getMimeType())
        .bodyString(requestBody, org.apache.http.entity.ContentType.APPLICATION_JSON)
        .execute()
        .returnResponse();
    jsonPath = JsonPath.using(Configuration.defaultConfiguration()
        .addOptions(Option.SUPPRESS_EXCEPTIONS))
        .parse(response.getEntity().getContent());
}
 
Example #27
Source Project: pentaho-kettle   Author: pentaho   File: FastJsonReader.java    License: Apache License 2.0 5 votes vote down vote up
private void setDefaultPathLeafToNull( boolean value ) {
  if ( value != this.defaultPathLeafToNull ) {
    this.defaultPathLeafToNull = value;
    if ( !this.defaultPathLeafToNull ) {
      this.jsonConfiguration = deleteOptionFromConfiguration( this.jsonConfiguration, Option.DEFAULT_PATH_LEAF_TO_NULL );
    }
  }
}
 
Example #28
Source Project: vividus   Author: vividus-framework   File: JsonPathUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Set<Option> options()
{
    return EnumSet.noneOf(Option.class);
}
 
Example #29
Source Project: centraldogma   Author: line   File: Jackson.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Set<Option> options() {
    return jsonPathCfg.getOptions();
}
 
Example #30
Source Project: karate   Author: intuit   File: JsonUtils.java    License: MIT License 4 votes vote down vote up
@Override
public Set<Option> options() {
    return EnumSet.noneOf(Option.class);
}