-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport 2.x] [Maintenance-2930] Review use of JSON Flattener (#3782)
Backport 87de7e2 from #3674. --------- Signed-off-by: Prabhas Kurapati <prabhask@berkeley.edu> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Signed-off-by: Darshit Chanpura <dchanp@amazon.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Darshit Chanpura <dchanp@amazon.com>
- Loading branch information
1 parent
d7b10bb
commit 47c977b
Showing
4 changed files
with
472 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/org/opensearch/security/support/JsonFlattener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.security.support; | ||
|
||
import java.io.IOException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
|
||
import org.opensearch.core.common.Strings; | ||
import org.opensearch.security.DefaultObjectMapper; | ||
|
||
public class JsonFlattener { | ||
|
||
public static Map<String, Object> flattenAsMap(String jsonString) { | ||
try { | ||
final TypeReference<Map<String, Object>> typeReference = new TypeReference<>() { | ||
}; | ||
final Map<String, Object> jsonMap = DefaultObjectMapper.objectMapper.readValue(jsonString, typeReference); | ||
final Map<String, Object> flattenMap = new LinkedHashMap<>(); | ||
flattenEntries("", jsonMap.entrySet(), flattenMap); | ||
return flattenMap; | ||
} catch (final IOException ioe) { | ||
throw new IllegalArgumentException("Unparseable json", ioe); | ||
} | ||
} | ||
|
||
private static void flattenEntries(String prefix, final Iterable<Map.Entry<String, Object>> entries, final Map<String, Object> result) { | ||
if (!Strings.isNullOrEmpty(prefix)) { | ||
prefix += "."; | ||
} | ||
|
||
for (final Map.Entry<String, Object> e : entries) { | ||
flattenElement(prefix.concat(e.getKey()), e.getValue(), result); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static void flattenElement(String prefix, final Object source, final Map<String, Object> result) { | ||
if (source instanceof Iterable) { | ||
flattenCollection(prefix, (Iterable<Object>) source, result); | ||
} | ||
if (source instanceof Map) { | ||
flattenEntries(prefix, ((Map<String, Object>) source).entrySet(), result); | ||
} | ||
result.put(prefix, source); | ||
} | ||
|
||
private static void flattenCollection(String prefix, final Iterable<Object> objects, final Map<String, Object> result) { | ||
int counter = 0; | ||
for (final Object o : objects) { | ||
flattenElement(prefix + "[" + counter + "]", o, result); | ||
counter++; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.