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 File: EnrichIntegrationJsonPathTestCase.java    From micro-integrator with 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 File: FastJsonReader.java    From pentaho-kettle with 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 #3
Source File: PropertiesBuilder.java    From querqy with 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 #4
Source File: TestSchemaValidator.java    From carbon-apimgt with 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 #5
Source File: JsonFunctions.java    From calcite with 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 #6
Source File: LightServer.java    From light with 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 #7
Source File: EnrichIntegrationJsonPathTestCase.java    From product-ei with 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 #8
Source File: ForEachnativeJSONTestCase.java    From product-ei with 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 #9
Source File: JsonPathStartupHookProvider.java    From light-4j with 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 File: MaskTest.java    From light-4j with 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 #11
Source File: JSONIO.java    From pom-manipulation-ext with 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 #12
Source File: TestCaseLoaderTest.java    From interface-test with 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 #13
Source File: InterfaceTestApplication.java    From interface-test with 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 #14
Source File: ServiceIntegrationTest.java    From tutorials with 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 #15
Source File: JSONRecordFactory.java    From rmlmapper-java with 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 #16
Source File: JsonFunctions.java    From Quicksql with 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 #17
Source File: FastJsonReader.java    From hop with 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 #18
Source File: CloudFoundryAcceptanceTest.java    From spring-cloud-app-broker with 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 #19
Source File: HttpClient.java    From james-project with 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 #20
Source File: JsonPathTestRoute.java    From camel-quarkus with 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 #21
Source File: FieldLevelEncryption.java    From client-encryption-java with 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 #22
Source File: FastJsonReader.java    From hop with 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 #23
Source File: CreateIndexMigration.java    From elasticsearch-migration with 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 #24
Source File: MApplication.java    From HaoReader with 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 #25
Source File: ParametersUtils.java    From conductor with 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 File: FastJsonReader.java    From pentaho-kettle with 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 #27
Source File: NashornTest.java    From interface-test with 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 #28
Source File: JsonExtractKeyTransformFunction.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Option> options() {
  return ImmutableSet.of(Option.SUPPRESS_EXCEPTIONS);
}
 
Example #29
Source File: JsonExtractScalarTransformFunction.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Option> options() {
  return ImmutableSet.of(Option.SUPPRESS_EXCEPTIONS);
}
 
Example #30
Source File: JsonPathProvider.java    From bender with Apache License 2.0 4 votes vote down vote up
@Override
public Set<Option> options() {
  return EnumSet.of(Option.SUPPRESS_EXCEPTIONS, Option.DEFAULT_PATH_LEAF_TO_NULL);
}