1111import java .nio .file .Paths ;
1212import java .util .*;
1313import java .util .Map .Entry ;
14+ import java .util .stream .Collectors ;
15+ import java .util .stream .StreamSupport ;
1416
1517public class JsonSettingsFile implements ISettingsFile {
1618
@@ -34,9 +36,9 @@ public Object getValue(String jsonPath) {
3436 private Object getEnvValueOrDefault (String jsonPath , boolean throwIfEmpty ) {
3537 String envVar = getEnvValue (jsonPath );
3638 JsonNode node = getJsonNode (jsonPath , throwIfEmpty && envVar == null );
37- return ! node .isMissingNode ()
38- ? castEnvOrDefaulValue ( node , envVar )
39- : envVar ;
39+ return node .isMissingNode ()
40+ ? envVar
41+ : castEnvOrDefaultValue ( node , envVar ) ;
4042 }
4143
4244 /**
@@ -46,7 +48,7 @@ private Object getEnvValueOrDefault(String jsonPath, boolean throwIfEmpty) {
4648 * @param envVar value got from environment variable
4749 * @return Value, casted to specific type.
4850 */
49- private Object castEnvOrDefaulValue (JsonNode node , String envVar ) {
51+ private Object castEnvOrDefaultValue (JsonNode node , String envVar ) {
5052 if (node .isBoolean ()) {
5153 return envVar == null ? node .asBoolean () : Boolean .parseBoolean (envVar );
5254 } else if (node .isLong ()) {
@@ -55,6 +57,8 @@ private Object castEnvOrDefaulValue(JsonNode node, String envVar) {
5557 return envVar == null ? node .asInt () : Integer .parseInt (envVar );
5658 } else if (node .isDouble ()) {
5759 return envVar == null ? node .asDouble () : Double .parseDouble (envVar );
60+ } else if (node .isObject ()) {
61+ return envVar == null ? node .toString () : envVar ;
5862 } else {
5963 return envVar == null ? node .asText () : envVar ;
6064 }
@@ -66,29 +70,31 @@ private String getEnvValue(String jsonPath) {
6670 if (envVar != null ) {
6771 Logger .getInstance ().debug (String .format ("***** Using variable passed from environment %1$s=%2$s" , key , envVar ));
6872 }
69-
7073 return envVar ;
7174 }
7275
7376 @ Override
7477 public List <String > getList (String jsonPath ) {
75- List <String > list = new ArrayList <>() ;
78+ List <String > list ;
7679 String envVar = getEnvValue (jsonPath );
7780 if (envVar != null ) {
78- Arrays .stream (envVar .split ("," )).forEach (element -> list .add (element .trim ()));
81+ list = Arrays .stream (envVar .split ("," ))
82+ .map (String ::trim )
83+ .collect (Collectors .toList ());
7984 } else {
80- getJsonNode (jsonPath ).elements ().forEachRemaining (node -> list .add (node .asText ()));
85+ Spliterator <JsonNode > spliterator = Spliterators .spliteratorUnknownSize (getJsonNode (jsonPath ).elements (), Spliterator .ORDERED );
86+ list = StreamSupport .stream (spliterator , false )
87+ .map (JsonNode ::asText )
88+ .collect (Collectors .toList ());
8189 }
82-
8390 return list ;
8491 }
8592
8693 @ Override
8794 public Map <String , Object > getMap (String jsonPath ) {
88- Iterator <Entry <String , JsonNode >> iterator = getJsonNode (jsonPath ).fields ();
89- final Map <String , Object > result = new HashMap <>();
90- iterator .forEachRemaining (entry -> result .put (entry .getKey (), getValue (jsonPath + "/" + entry .getKey ())));
91- return result ;
95+ Spliterator <Entry <String , JsonNode >> spliterator = Spliterators .spliteratorUnknownSize (getJsonNode (jsonPath ).fields (), Spliterator .ORDERED );
96+ return StreamSupport .stream (spliterator , false )
97+ .collect (Collectors .toMap (Entry ::getKey , entry -> getValue (jsonPath + "/" + entry .getKey ())));
9298 }
9399
94100 private JsonNode getJsonNode (String jsonPath ) {
0 commit comments