@@ -98,7 +98,7 @@ public static String url_utf8(String in){
9898 * @return rounded double as string or empty
9999 */
100100 public static String smartRound (Object val , boolean round_to_int ){
101- double v = obj2Double (val );
101+ double v = obj2DoubleOrDefault (val , Double . NEGATIVE_INFINITY );
102102 if (v == Double .NEGATIVE_INFINITY ){
103103 System .err .println (DateTime .getLogDate () + " ERROR - Converters.java smartRound(..) FAILED! with: " + val .toString ());
104104 return "" ;
@@ -124,52 +124,56 @@ public static String smartRound(Object val, boolean round_to_int){
124124 }
125125
126126 /**
127- * Convert a string to a long. If it fails the long will be -1. Best used for System.time checks.
128- * @param in - string in long-format to convert
129- * @return long value or -1. If -1 is important to you DON'T use this method
127+ * Convert an unknown object to String or return null.
128+ * @param in - object to convert
129+ * @param def - default
130+ * @return String or default
130131 */
131- public static long str2Long ( String in ){
132+ public static String obj2StringOrDefault ( Object in , String def ){
132133 try {
133- return Long . parseLong ( in );
134+ return in . toString ( );
134135 } catch (Exception e ){
135- return - 1 ;
136+ return def ;
136137 }
137138 }
138139 /**
139140 * Convert an unknown object that holds a double (real double or string double) to double.
140141 * @param in - string to convert, must be in double format
141- * @return double value or NEGATIVE_INFINITY. If NEGATIVE_INFINITY is important to you DON'T use this method
142+ * @param def - default
143+ * @return double value or default
142144 */
143- public static double obj2Double (Object in ){
145+ public static double obj2DoubleOrDefault (Object in , Double def ){
144146 try {
145147 return Double .parseDouble ((String .valueOf (in )));
146148 } catch (Exception e ){
147- return Double . NEGATIVE_INFINITY ;
149+ return def ;
148150 }
149151 }
150152 /**
151153 * Convert an unknown object that holds a double (real double or string double) to long.
152154 * @param in - string to convert, must be in double format
153- * @return long value or -1. If -1 is important to you DON'T use this method
155+ * @param def - default
156+ * @return long value or default
154157 */
155- public static long obj2Long (Object in ){
158+ public static long obj2LongOrDefault (Object in , Long def ){
156159 try {
157160 return (long ) Double .parseDouble ((String .valueOf (in )));
158161 } catch (Exception e ){
159- return - 1 ;
162+ return def ;
160163 }
161164 }
162165 /**
163166 * Convert an unknown object that holds an integer/double (real type or as string) to an integer.
164167 * Removes all non-numbers (except .,+-) in the string!
165168 * @param in - string to convert, must be in double format
166- * @return int value or -1. If -1 is important to you DON'T use this method or check if it was there before
169+ * @param def - default
170+ * @return int value or def
167171 */
168- public static int obj2Int (Object in ){
172+ public static int obj2IntOrDefault (Object in , Integer def ){
169173 try {
170174 return (int ) Double .parseDouble ((String .valueOf (in ).replaceAll ("[^\\ d\\ .,\\ -\\ +]" , "" )));
171175 } catch (Exception e ){
172- return - 1 ;
176+ return def ;
173177 }
174178 }
175179
@@ -372,6 +376,18 @@ public static Map<String, String> json2HashMapStrStr(JSONObject jsonObject) {
372376 public static Map <String , Object > json2HashMap (JSONObject jsonObject ) {
373377 return object2HashMapStrObj (jsonObject );
374378 }
379+ /**
380+ * Add content of JSONObject to a Map converting all values to String.
381+ * @param jsonSource - source JSONObject
382+ * @param targetMap - target Map (non null!)
383+ */
384+ @ SuppressWarnings ("unchecked" )
385+ public static void addJsonToMapAsStrings (JSONObject jsonSource , Map <String , String > targetMap ){
386+ for (Object entry : jsonSource .entrySet ()) {
387+ Map .Entry <String , Object > entryObj = (Map .Entry <String , Object >) entry ;
388+ targetMap .put (entryObj .getKey (), entryObj .getValue ().toString ());
389+ }
390+ }
375391
376392 /**
377393 * Makes an unchecked (cause you can't check it, can you?) cast from Object to HashMap<String, Object>.
0 commit comments