@@ -155,23 +155,13 @@ private static void encryptPayloadPath(DocumentContext payloadContext, String js
155155 }
156156
157157 // Add encrypted data and encryption fields at the given JSON path
158- Object outJsonObject = readOrCreateOutObject (payloadContext , jsonPathOut );
159- jsonProvider .setProperty (outJsonObject , config .ivFieldName , ivValue );
160- jsonProvider .setProperty (outJsonObject , config .encryptedKeyFieldName , encryptedKeyValue );
161- jsonProvider .setProperty (outJsonObject , config .encryptedValueFieldName , encryptedValue );
162- addEncryptionCertificateFingerprint (outJsonObject , config );
163- addEncryptionKeyFingerprint (outJsonObject , config );
164- addOaepPaddingDigestAlgorithm (outJsonObject , config );
165-
166- if (!"$" .equals (jsonPathOut )) {
167- payloadContext .set (jsonPathOut , outJsonObject );
168- } else {
169- // Add keys one by one
170- Collection <String > propertyKeys = new ArrayList <>(jsonProvider .getPropertyKeys (outJsonObject ));
171- for (String key : propertyKeys ) {
172- payloadContext .put (jsonPathOut , key , jsonProvider .getMapValue (outJsonObject , key ));
173- }
174- }
158+ checkOrCreateOutObject (payloadContext , jsonPathOut );
159+ payloadContext .put (jsonPathOut , config .ivFieldName , ivValue );
160+ payloadContext .put (jsonPathOut , config .encryptedKeyFieldName , encryptedKeyValue );
161+ payloadContext .put (jsonPathOut , config .encryptedValueFieldName , encryptedValue );
162+ addEncryptionCertificateFingerprint (payloadContext , jsonPathOut , config );
163+ addEncryptionKeyFingerprint (payloadContext , jsonPathOut , config );
164+ addOaepPaddingDigestAlgorithm (payloadContext , jsonPathOut , config );
175165 }
176166
177167 private static void decryptPayloadPath (DocumentContext payloadContext , String jsonPathIn , String jsonPathOut ,
@@ -212,7 +202,7 @@ private static void decryptPayloadPath(DocumentContext payloadContext, String js
212202 // Add decrypted data at the given JSON path
213203 String decryptedValue = new String (decryptedValueBytes , StandardCharsets .UTF_8 );
214204 decryptedValue = sanitizeJson (decryptedValue );
215- readOrCreateOutObject (payloadContext , jsonPathOut );
205+ checkOrCreateOutObject (payloadContext , jsonPathOut );
216206 addDecryptedDataToPayload (payloadContext , decryptedValue , jsonPathOut );
217207
218208 // Remove the input object if now empty
@@ -222,14 +212,14 @@ private static void decryptPayloadPath(DocumentContext payloadContext, String js
222212 }
223213 }
224214
225- private static Object readOrCreateOutObject (DocumentContext context , String jsonPathOutString ) {
215+ private static void checkOrCreateOutObject (DocumentContext context , String jsonPathOutString ) {
226216 Object outJsonObject = readJsonObject (context , jsonPathOutString );
227217 if (null != outJsonObject ) {
228- // Return the existing object
229- return outJsonObject ;
218+ // Object already exists
219+ return ;
230220 }
231221
232- // Path does not exist: if parent exists we create a new object under the parent
222+ // Path does not exist: if parent exists then we create a new object under the parent
233223 String parentJsonPath = getParentJsonPath (jsonPathOutString );
234224 Object parentJsonObject = readJsonObject (context , parentJsonPath );
235225 if (parentJsonObject == null ) {
@@ -238,7 +228,6 @@ private static Object readOrCreateOutObject(DocumentContext context, String json
238228 outJsonObject = jsonPathConfig .jsonProvider ().createMap ();
239229 String elementKey = getJsonElementKey (jsonPathOutString );
240230 context .put (parentJsonPath , elementKey , outJsonObject );
241- return outJsonObject ;
242231 }
243232
244233 private static Object readJsonElement (DocumentContext context , String jsonPathString ) {
@@ -349,46 +338,43 @@ private static String getJsonElementKey(String jsonPathString) {
349338 throw new IllegalStateException (String .format ("Unable to find object key for '%s'" , jsonPathString ));
350339 }
351340
352- private static void addEncryptionCertificateFingerprint (Object jsonObject , FieldLevelEncryptionConfig config ) throws GeneralSecurityException {
341+ private static void addEncryptionCertificateFingerprint (DocumentContext payloadContext , String jsonPathOut , FieldLevelEncryptionConfig config ) throws GeneralSecurityException {
353342 if (isNullOrEmpty (config .encryptionCertificateFingerprintFieldName )) {
354343 // Nothing to add
355344 return ;
356345 }
357- JsonProvider jsonProvider = jsonPathConfig .jsonProvider ();
358346 String providedCertificateFingerprintValue = config .encryptionCertificateFingerprint ;
359347 if (!isNullOrEmpty (providedCertificateFingerprintValue )) {
360- jsonProvider . setProperty ( jsonObject , config .encryptionCertificateFingerprintFieldName , providedCertificateFingerprintValue );
348+ payloadContext . put ( jsonPathOut , config .encryptionCertificateFingerprintFieldName , providedCertificateFingerprintValue );
361349 } else {
362350 byte [] certificateFingerprintBytes = sha256digestBytes (config .encryptionCertificate .getEncoded ());
363351 String certificateFingerprintValue = encodeBytes (certificateFingerprintBytes , config .fieldValueEncoding );
364- jsonProvider . setProperty ( jsonObject , config .encryptionCertificateFingerprintFieldName , certificateFingerprintValue );
352+ payloadContext . put ( jsonPathOut , config .encryptionCertificateFingerprintFieldName , certificateFingerprintValue );
365353 }
366354 }
367355
368- private static void addEncryptionKeyFingerprint (Object jsonObject , FieldLevelEncryptionConfig config ) throws GeneralSecurityException {
356+ private static void addEncryptionKeyFingerprint (DocumentContext payloadContext , String jsonPathOut , FieldLevelEncryptionConfig config ) throws GeneralSecurityException {
369357 if (isNullOrEmpty (config .encryptionKeyFingerprintFieldName )) {
370358 // Nothing to add
371359 return ;
372360 }
373- JsonProvider jsonProvider = jsonPathConfig .jsonProvider ();
374361 String providedKeyFingerprintValue = config .encryptionKeyFingerprint ;
375362 if (!isNullOrEmpty (providedKeyFingerprintValue )) {
376- jsonProvider . setProperty ( jsonObject , config .encryptionKeyFingerprintFieldName , providedKeyFingerprintValue );
363+ payloadContext . put ( jsonPathOut , config .encryptionKeyFingerprintFieldName , providedKeyFingerprintValue );
377364 } else {
378365 byte [] keyFingerprintBytes = sha256digestBytes (config .encryptionCertificate .getPublicKey ().getEncoded ());
379366 String keyFingerprintValue = encodeBytes (keyFingerprintBytes , config .fieldValueEncoding );
380- jsonProvider . setProperty ( jsonObject , config .encryptionKeyFingerprintFieldName , keyFingerprintValue );
367+ payloadContext . put ( jsonPathOut , config .encryptionKeyFingerprintFieldName , keyFingerprintValue );
381368 }
382369 }
383370
384- private static void addOaepPaddingDigestAlgorithm (Object jsonObject , FieldLevelEncryptionConfig config ) {
371+ private static void addOaepPaddingDigestAlgorithm (DocumentContext payloadContext , String jsonPathOut , FieldLevelEncryptionConfig config ) {
385372 if (isNullOrEmpty (config .oaepPaddingDigestAlgorithmFieldName )) {
386373 // Nothing to add
387374 return ;
388375 }
389- JsonProvider jsonProvider = jsonPathConfig .jsonProvider ();
390376 String oaepDigestAlgorithm = config .oaepPaddingDigestAlgorithm .replace ("-" , "" );
391- jsonProvider . setProperty ( jsonObject , config .oaepPaddingDigestAlgorithmFieldName , oaepDigestAlgorithm );
377+ payloadContext . put ( jsonPathOut , config .oaepPaddingDigestAlgorithmFieldName , oaepDigestAlgorithm );
392378 }
393379
394380 private static IvParameterSpec generateIv () throws GeneralSecurityException {
0 commit comments