Java Code Examples for com.jayway.jsonpath.spi.json.JsonProvider

The following examples show how to use com.jayway.jsonpath.spi.json.JsonProvider. These examples are extracted from open source projects. 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
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: client-encryption-java   Source File: FieldLevelEncryption.java    License: MIT License 6 votes vote down vote up
private static void addDecryptedDataToPayload(DocumentContext payloadContext, String decryptedValue, String jsonPathOut) {
    JsonProvider jsonProvider = jsonPathConfig.jsonProvider();
    Object decryptedValueJsonElement = jsonEngine.parse(decryptedValue);

    if (!jsonEngine.isJsonObject(decryptedValueJsonElement)) {
        // Array or primitive: overwrite
        payloadContext.set(jsonPathOut, decryptedValueJsonElement);
        return;
    }

    // Object: merge
    int length = jsonProvider.length(decryptedValueJsonElement);
    Collection<String> propertyKeys = (0 == length) ? Collections.<String>emptyList() : jsonProvider.getPropertyKeys(decryptedValueJsonElement);
    for (String key : propertyKeys) {
        payloadContext.delete(jsonPathOut + "." + key);
        payloadContext.put(jsonPathOut, key, jsonProvider.getMapValue(decryptedValueJsonElement, key));
    }
}
 
Example 3
@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 4
Source Project: interface-test   Source 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 5
Source Project: interface-test   Source 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 6
Source Project: light-4j   Source 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 7
Source Project: light-4j   Source 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 8
Source Project: product-ei   Source 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 9
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 10
Source Project: carbon-apimgt   Source 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 11
Source Project: light   Source 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 12
Source Project: client-encryption-java   Source File: FieldLevelEncryption.java    License: MIT License 5 votes vote down vote up
private static Object readAndDeleteJsonKey(DocumentContext context, String objectPath, Object object, String key) {
    if (null == key) {
        // Do nothing
        return null;
    }
    JsonProvider jsonProvider = jsonPathConfig.jsonProvider();
    Object value = jsonProvider.getMapValue(object, key);
    context.delete(objectPath + "." + key);
    return value;
}
 
Example 13
Source Project: interface-test   Source 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 14
Source Project: vividus   Source File: JsonPathUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public JsonProvider jsonProvider()
{
    return jacksonJsonProvider;
}
 
Example 15
Source Project: client-encryption-java   Source File: JsonOrgJsonEngine.java    License: MIT License 4 votes vote down vote up
@Override
public JsonProvider getJsonProvider() {
    return jsonProvider;
}
 
Example 16
Source Project: client-encryption-java   Source File: GsonJsonEngine.java    License: MIT License 4 votes vote down vote up
@Override
public JsonProvider getJsonProvider() {
    return jsonProvider;
}
 
Example 17
Source Project: client-encryption-java   Source File: JsonSmartJsonEngine.java    License: MIT License 4 votes vote down vote up
@Override
public JsonProvider getJsonProvider() {
    return jsonProvider;
}
 
Example 18
Source Project: client-encryption-java   Source File: JsonEngine.java    License: MIT License 4 votes vote down vote up
protected boolean isJsonPrimitive(Object jsonElement) {
    JsonProvider jsonProvider = getJsonProvider();
    return !jsonProvider.isMap(jsonElement) && !jsonProvider.isArray(jsonElement);
}
 
Example 19
Source Project: client-encryption-java   Source File: JettisonJsonEngine.java    License: MIT License 4 votes vote down vote up
@Override
public JsonProvider getJsonProvider() {
    return jsonProvider;
}
 
Example 20
Source Project: client-encryption-java   Source File: JacksonJsonEngine.java    License: MIT License 4 votes vote down vote up
@Override
public JsonProvider getJsonProvider() {
    return jsonProvider;
}
 
Example 21
Source Project: client-encryption-java   Source File: FieldLevelEncryption.java    License: MIT License 4 votes vote down vote up
private static void decryptPayloadPath(DocumentContext payloadContext, String jsonPathIn, String jsonPathOut,
                                       FieldLevelEncryptionConfig config, FieldLevelEncryptionParams params) throws GeneralSecurityException, EncryptionException {

    JsonProvider jsonProvider = jsonPathConfig.jsonProvider();
    Object inJsonObject = readJsonObject(payloadContext, jsonPathIn);
    if (inJsonObject == null) {
        // Nothing to decrypt
        return;
    }

    // Read and remove encrypted data and encryption fields at the given JSON path
    Object encryptedValueJsonElement = readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.encryptedValueFieldName);
    if (jsonEngine.isNullOrEmptyJson(encryptedValueJsonElement)) {
        // Nothing to decrypt
        return;
    }

    if (!config.useHttpPayloads() && params == null) {
        throw new IllegalStateException("Encryption params have to be set when not stored in HTTP payloads!");
    }

    if (params == null) {
        // Read encryption params from the payload
        Object oaepDigestAlgorithmJsonElement = readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.oaepPaddingDigestAlgorithmFieldName);
        String oaepDigestAlgorithm = jsonEngine.isNullOrEmptyJson(oaepDigestAlgorithmJsonElement) ? config.oaepPaddingDigestAlgorithm : jsonEngine.toJsonString(oaepDigestAlgorithmJsonElement);
        Object encryptedKeyJsonElement = readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.encryptedKeyFieldName);
        Object ivJsonElement = readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.ivFieldName);
        readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.encryptionCertificateFingerprintFieldName);
        readAndDeleteJsonKey(payloadContext, jsonPathIn, inJsonObject, config.encryptionKeyFingerprintFieldName);
        params = new FieldLevelEncryptionParams(jsonEngine.toJsonString(ivJsonElement), jsonEngine.toJsonString(encryptedKeyJsonElement), oaepDigestAlgorithm, config);
    }

    // Decrypt data
    byte[] encryptedValueBytes = decodeValue(jsonEngine.toJsonString(encryptedValueJsonElement), config.fieldValueEncoding);
    byte[] decryptedValueBytes = decryptBytes(params.getSecretKey(), params.getIvSpec(), encryptedValueBytes);

    // Add decrypted data at the given JSON path
    String decryptedValue = new String(decryptedValueBytes, StandardCharsets.UTF_8);
    decryptedValue = sanitizeJson(decryptedValue);
    checkOrCreateOutObject(payloadContext, jsonPathOut);
    addDecryptedDataToPayload(payloadContext, decryptedValue, jsonPathOut);

    // Remove the input if now empty
    Object inJsonElement  = readJsonElement(payloadContext, jsonPathIn);
    if (0 == jsonProvider.length(inJsonElement) && !"$".equals(jsonPathIn)) {
        payloadContext.delete(jsonPathIn);
    }
}
 
Example 22
Source Project: centraldogma   Source File: Jackson.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public JsonProvider jsonProvider() {
    return jsonPathCfg.jsonProvider();
}
 
Example 23
Source Project: karate   Source File: JsonUtils.java    License: MIT License 4 votes vote down vote up
@Override
public JsonProvider jsonProvider() {
    return jsonProvider;
}
 
Example 24
Source Project: bender   Source File: JsonPathProvider.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public JsonProvider jsonProvider() {
  return jsonProvider;
}
 
Example 25
Source Project: lucene-solr   Source File: PackageUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static Configuration jsonPathConfiguration() {
  MappingProvider provider = new JacksonMappingProvider();
  JsonProvider jsonProvider = new JacksonJsonProvider();
  Configuration c = Configuration.builder().jsonProvider(jsonProvider).mappingProvider(provider).options(com.jayway.jsonpath.Option.REQUIRE_PROPERTIES).build();
  return c;
}
 
Example 26
Source Project: sparql-generate   Source File: ITER_JSONListKeys.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public JsonProvider jsonProvider() {
    return jsonProvider;
}
 
Example 27
Source Project: sparql-generate   Source File: ITER_CBOR.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public JsonProvider jsonProvider() {
    return jsonProvider;
}
 
Example 28
Source Project: irontest   Source File: IronTestApplication.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void initialize(Bootstrap<IronTestConfiguration> bootstrap) {
    bootstrap.addCommand(new UpgradeCommand());

    bootstrap.addBundle(new AssetsBundle("/assets/app", "/ui", "index.htm", "ui"));
    bootstrap.addBundle(new AssetsBundle("/META-INF/resources/webjars", "/ui/lib", null, "lib"));
    bootstrap.addBundle(new AssetsBundle("/assets/mockserver", "/ui/mockserver", "mockserver.htm", "mockserver"));
    bootstrap.addBundle(new AssetsBundle("/assets/common", "/ui/common", null, "common"));
    bootstrap.addBundle(jaxWsBundle);
    bootstrap.addBundle(new MultiPartBundle());
    bootstrap.addBundle(new ViewBundle<IronTestConfiguration>(){
        @Override
        public Map<String, Map<String, String>> getViewConfiguration(IronTestConfiguration config) {
            return config.getViewRendererConfiguration();
        }
    });
    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);
        }
    });

    //  configure the Jackson ObjectMapper used by JAX-RS (Jersey)
    ObjectMapper objectMapper = bootstrap.getObjectMapper();
    objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
    IronTestUtils.addMixInsForWireMock(objectMapper);
}
 
Example 29
public void init() {
	logger.info("init " + this);

	chrono=new Chrono();

	Configuration.setDefaults(new Configuration.Defaults() {

		private final JsonProvider jsonProvider = new GsonJsonProvider();
		private final MappingProvider mappingProvider = new GsonMappingProvider();

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

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

		@Override
		public Set<Option> options() {
			return EnumSet.noneOf(Option.class);
		}

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

	try {

		logger.debug("loading file " + fileSetJson);

		if (hasNewVersion()||!fileSetJson.exists() || fileSetJson.length() == 0 || getBoolean(FORCE_RELOAD)) {
			logger.info("Downloading "+version + " datafile");
			URLTools.download(URL_JSON_ALL_SETS_ZIP, fileSetJsonTemp);
			FileTools.unZipIt(fileSetJsonTemp,fileSetJson);
			FileTools.saveFile(fversion,version);
			setProperty(FORCE_RELOAD, "false");
		}
		Chrono chr = new Chrono();
		chr.start();
		logger.debug(this + " : parsing db file");
		ctx = JsonPath.parse(fileSetJson);
		logger.debug(this + " : parsing OK in " + chr.stop()+"s");
		
	} catch (Exception e1) {
		logger.error(e1);
	}
}
 
Example 30
Source Project: metron   Source File: JSONMapParser.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void configure(Map<String, Object> config) {
  setReadCharset(config);
  String strategyStr = (String) config.getOrDefault(MAP_STRATEGY_CONFIG, MapStrategy.DROP.name());
  mapStrategy = MapStrategy.valueOf(strategyStr);
  overrideOriginalString = (Boolean) config.getOrDefault(OVERRIDE_ORIGINAL_STRING, false);
  if (config.containsKey(JSONP_QUERY)) {
    typeRef = new TypeRef<List<Map<String, Object>>>() { };
    jsonpQuery = (String) config.get(JSONP_QUERY);

    if (!StringUtils.isBlank(jsonpQuery) && config.containsKey(WRAP_JSON)) {
      Object wrapObject = config.get(WRAP_JSON);
      if (wrapObject instanceof String) {
        wrapJson = Boolean.valueOf((String)wrapObject);
      } else if (wrapObject instanceof Boolean) {
        wrapJson = (Boolean) config.get(WRAP_JSON);
      }
      String entityName = (String)config.get(WRAP_ENTITY_NAME);
      if (!StringUtils.isBlank(entityName)) {
        wrapEntityName = entityName;
      }
    }

    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.of(Option.SUPPRESS_EXCEPTIONS);
      }
    });

    if (CacheProvider.getCache() == null) {
      CacheProvider.setCache(new LRUCache(100));
    }
  }
}