本文整理汇总了Java中com.jayway.jsonpath.Configuration类的典型用法代码示例。如果您正苦于以下问题:Java Configuration类的具体用法?Java Configuration怎么用?Java Configuration使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Configuration类属于com.jayway.jsonpath包,在下文中一共展示了Configuration类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: evaluateJsonPathToString
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
/**
* Evaluates provided JSONPath expression into the {@link String}
*
* @param evalExpression Expression to evaluate
* @param jsonSource JSON source
* @return Evaluation result
*/
public static String evaluateJsonPathToString(String evalExpression,
String jsonSource) {
try {
List<Object> parsedData =
JsonPath.
using(Configuration.
defaultConfiguration().
addOptions(
ALWAYS_RETURN_LIST,
SUPPRESS_EXCEPTIONS)).
parse(jsonSource).
read(evalExpression);
return collectionIsNotEmpty(parsedData) ?
String.valueOf(
parsedData.
get(FIRST_ELEMENT)) :
EMPTY;
} catch (Exception e) {
LOG.warning(COMMON_EVAL_ERROR_MESSAGE);
}
return EMPTY;
}
示例2: assertPaths
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
private void assertPaths(JsonNode node, boolean shouldExist, String... paths) {
Object document = Configuration.defaultConfiguration().jsonProvider().parse(node.toString());
for (String path : paths) {
try {
Object object = JsonPath.read(document, path);
if (isIndefinite(path)) {
Collection c = (Collection) object;
assertThat(c.isEmpty()).isNotEqualTo(shouldExist);
} else if (!shouldExist) {
Assert.fail("Expected path not to be found: " + path);
}
} catch (PathNotFoundException e) {
if (shouldExist) {
Assert.fail("Path not found: " + path);
}
}
}
}
示例3: before
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
/**
* Override to set up your specific external resource.
*/
@Override
public void before() {
saveDefaults();
Configuration.setDefaults(new 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);
}
});
}
示例4: restoreDefaults
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
private void restoreDefaults() {
if (!this.hadDefaults) {
return;
}
Configuration.setDefaults(new Defaults() {
@Override
public JsonProvider jsonProvider() {
return jsonProvider;
}
@Override
public MappingProvider mappingProvider() {
return mappingProvider;
}
@Override
public Set<Option> options() {
return options;
}
});
}
示例5: setDefaults
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
public static void setDefaults() {
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);
}
});
}
示例6: map
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Override
public <T> T map(final Object source, final Class<T> targetType, final Configuration configuration) {
if (source == null) {
return null;
}
if (targetType.isAssignableFrom(source.getClass())) {
return (T) source;
}
try {
if (targetType.isAssignableFrom(ArrayList.class) && configuration.jsonProvider().isArray(source)) {
int length = configuration.jsonProvider().length(source);
@SuppressWarnings("rawtypes")
ArrayList list = new ArrayList(length);
for (Object o : configuration.jsonProvider().toIterable(source)) {
list.add(o);
}
return (T) list;
}
} catch (Exception e) {
}
throw new MappingException("Cannot convert a " + source.getClass().getName() + " to a " + targetType
+ " use Tapestry's TypeCoercer instead.");
}
开发者ID:osswangxining,项目名称:another-rule-based-analytics-on-spark,代码行数:25,代码来源:TapestryMappingProvider.java
示例7: map
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Override
public <T> T map(Object source, Class<T> targetType, Configuration configuration) {
if(source == null){
return null;
}
if (targetType.isAssignableFrom(source.getClass())) {
return (T) source;
}
try {
if(!configuration.jsonProvider().isMap(source) && !configuration.jsonProvider().isArray(source)){
return factory.call().getMapper(targetType).convert(source);
}
String s = configuration.jsonProvider().toJson(source);
return (T) JSONValue.parse(s, targetType);
} catch (Exception e) {
throw new MappingException(e);
}
}
开发者ID:osswangxining,项目名称:another-rule-based-analytics-on-spark,代码行数:20,代码来源:JsonSmartMappingProvider.java
示例8: personToJsonString
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Test
public void personToJsonString() {
Person duke = new Person("Duke", LocalDate.of(1995, 5, 23));
duke.setPhoneNumbers(
Arrays.asList(
new PhoneNumber(HOME, "100000"),
new PhoneNumber(OFFICE, "200000")
)
);
Jsonb jsonMapper = JsonbBuilder.create();
String json = jsonMapper.toJson(duke);
LOG.log(Level.INFO, "converted json result: {0}", json);
String name = JsonPath.parse(json).read("$.name");
assertEquals("Duke", name);
Configuration config = Configuration.defaultConfiguration()
.jsonProvider(new GsonJsonProvider())
.mappingProvider(new GsonMappingProvider());
TypeRef<List<String>> typeRef = new TypeRef<List<String>>() {
};
List<String> numbers = JsonPath.using(config).parse(json).read("$.phoneNumbers[*].number", typeRef);
assertEquals(Arrays.asList("100000", "200000"), numbers);
}
示例9: SelectJsonPath
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Inject
public SelectJsonPath(ObjectMapper objectMapper) {
configuration = Configuration.builder()
.options(Option.SUPPRESS_EXCEPTIONS)
.jsonProvider(new JacksonJsonNodeJsonProvider(objectMapper))
.build();
jsonParam = ParameterDescriptor.type("json", JsonNode.class).description("A parsed JSON tree").build();
// sigh generics and type erasure
//noinspection unchecked
pathsParam = ParameterDescriptor.type("paths",
(Class<Map<String, String>>) new TypeLiteral<Map<String, String>>() {}.getRawType(),
(Class<Map<String, JsonPath>>) new TypeLiteral<Map<String, JsonPath>>() {}.getRawType())
.transform(inputMap -> inputMap
.entrySet().stream()
.collect(toMap(Map.Entry::getKey, e -> JsonPath.compile(e.getValue()))))
.description("A map of names to a JsonPath expression, see http://jsonpath.com")
.build();
}
示例10: afterPropertiesSet
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Override
public void afterPropertiesSet() throws Exception {
Configuration.setDefaults(new Configuration.Defaults() {
private final JsonProvider jsonProvider = new JacksonJsonProvider();
@Override
public JsonProvider jsonProvider() {
return jsonProvider;
}
@Override
public Set<Option> options() {
return EnumSet.noneOf(Option.class);
}
@Override
public MappingProvider mappingProvider() {
return new JacksonMappingProvider();
}
});
}
示例11: testParseString
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Test
public void testParseString () throws IOException
{
BasicConfigurator.configure ();
String f = "../tests/data/complex1.json";
File file = new File (f.replace ('/', File.separatorChar));
String str = Utils.getString (file);
JsonPathProvider provider = new JsonPathProvider ();
Configuration pathConfig = Configuration.defaultConfiguration ().jsonProvider (provider);
JsonPath path = JsonPath.compile ("$.strange");
JsonValue value = path.read (str, pathConfig);
// we cannot directly compare the output since the attribute ordering
// can vary.
Assert.assertEquals ("{\"id\":5555,\"price\":[1,2,3],\"customer\":\"john...\"}".length (), provider.toJson (value).length ());
}
示例12: testBson
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Test
public void testBson () throws IOException
{
BasicConfigurator.configure ();
String f = "../tests/data/data1.bson";
File file = new File (f.replace ('/', File.separatorChar));
JsonPathProvider provider = new JsonPathProvider ();
Configuration pathConfig = Configuration.defaultConfiguration ().jsonProvider (provider);
JsonPath path = JsonPath.compile ("$..A");
JsonProvider p = new CookJsonProvider ();
HashMap<String, Object> readConfig = new HashMap<String, Object> ();
readConfig.put (CookJsonProvider.FORMAT, CookJsonProvider.FORMAT_BSON);
readConfig.put (CookJsonProvider.ROOT_AS_ARRAY, Boolean.TRUE);
JsonReaderFactory rf = p.createReaderFactory (readConfig);
JsonReader reader = rf.createReader (new FileInputStream (file));
JsonStructure obj = reader.read ();
reader.close ();
JsonValue value = path.read (obj, pathConfig);
Assert.assertEquals ("[1,3,5,7]", provider.toJson (value));
}
示例13: hello
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Test
public void hello() throws URISyntaxException, IOException {
JsonProvider jsonProvider = Configuration.defaultConfiguration().jsonProvider();
String recordId = "2022320/3F61C612ED9C42CCB85E533B4736795E8BDC7E77";
String jsonString = readContent("general/td-idf-response.json");
assertEquals("{", jsonString.substring(0,1));
TfIdfExtractor extractor = new TfIdfExtractor(new EdmOaiPmhXmlSchema());
FieldCounter<Double> results = extractor.extract(jsonString, recordId);
assertEquals(6, results.size());
assertEquals(new Double(0.0017653998874690505), results.get("dc:title:avg"));
assertEquals(new Double(0.008826999437345252), results.get("dc:title:sum"));
assertEquals(new Double(0), results.get("dcterms:alternative:avg"));
assertEquals(new Double(0), results.get("dcterms:alternative:sum"));
assertEquals(new Double(0), results.get("dc:description:avg"));
assertEquals(new Double(0), results.get("dc:description:sum"));
}
示例14: hello
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
@Test
public void hello() throws URISyntaxException, IOException {
String jsonString = FileUtils.readFirstLine("general/test.json");
Object jsonDocument = Configuration.defaultConfiguration().jsonProvider().parse(jsonString);
for (JsonBranch collectionBranch : schema.getCollectionPaths()) {
Object rawCollection = null;
try {
rawCollection = JsonPath.read(jsonDocument, collectionBranch.getJsonPath());
} catch (PathNotFoundException e) {}
if (rawCollection != null) {
if (rawCollection instanceof JSONArray) {
JSONArray collection = (JSONArray)rawCollection;
collection.forEach(
node -> {
processNode(node, collectionBranch.getChildren());
}
);
} else {
processNode(rawCollection, collectionBranch.getChildren());
}
}
}
}
示例15: processCustomEvent
import com.jayway.jsonpath.Configuration; //导入依赖的package包/类
private Event processCustomEvent(ReadContext readContext) {
Configuration conf = Configuration.defaultConfiguration();
Event event = new Event(attributesSize);
Object[] data = event.getData();
Object childObject = readContext.read(DEFAULT_ENCLOSING_ELEMENT);
readContext = JsonPath.using(conf).parse(childObject);
for (MappingPositionData mappingPositionData : this.mappingPositions) {
int position = mappingPositionData.getPosition();
Object mappedValue;
try {
mappedValue = readContext.read(mappingPositionData.getMapping());
if (mappedValue == null) {
data[position] = null;
} else {
data[position] = attributeConverter.getPropertyValue(mappedValue.toString(),
streamAttributes.get(position).getType());
}
} catch (PathNotFoundException e) {
if (failOnMissingAttribute) {
log.error("Json message " + childObject.toString() +
" contains missing attributes. Hence dropping the message.");
return null;
}
data[position] = null;
}
}
return event;
}