22
33import com .genexus .diagnostics .LogLevel ;
44import com .genexus .diagnostics .core .ILogger ;
5- import com .google .gson .Gson ;
6- import com .google .gson .JsonElement ;
7- import com .google .gson .JsonParser ;
8- import com .google .gson .reflect .TypeToken ;
95import org .apache .logging .log4j .Level ;
106import org .apache .logging .log4j .LogManager ;
117import org .apache .logging .log4j .MarkerManager ;
1612import org .apache .logging .log4j .core .config .Configuration ;
1713import org .apache .logging .log4j .layout .template .json .JsonTemplateLayout ;
1814import org .apache .logging .log4j .message .MapMessage ;
15+ import org .json .JSONArray ;
16+ import org .json .JSONObject ;
1917
20- import java .lang .reflect .Type ;
2118import java .util .ArrayList ;
2219import java .util .LinkedHashMap ;
2320import java .util .List ;
@@ -255,7 +252,7 @@ private void writeTextFormat(String message, int logLevel, Object data, boolean
255252 Map <String , Object > mapMessage = new LinkedHashMap <>();
256253
257254 if (data == null || (data instanceof String && "null" .equals (data .toString ()))) {
258- mapMessage .put (dataKey , ( Object ) null );
255+ mapMessage .put (dataKey , JSONObject . NULL );
259256 } else if (data instanceof String && isJson ((String ) data )) { // JSON Strings
260257 mapMessage .put (dataKey , jsonStringToMap ((String )data ));
261258 } else {
@@ -266,7 +263,7 @@ private void writeTextFormat(String message, int logLevel, Object data, boolean
266263 mapMessage .put (STACKTRACE_KEY , getStackTraceAsList ());
267264 }
268265
269- String json = new Gson (). newBuilder (). serializeNulls (). create (). toJson ( mapMessage );
266+ String json = new JSONObject ( mapMessage ). toString ( );
270267 String format = "{} - {}" ;
271268 log .log (getLogLevel (logLevel ), format , message , json );
272269 }
@@ -313,21 +310,28 @@ private static String fromObjectToString(Object value) {
313310 res = (String ) value ;
314311 } else if (value instanceof Number || value instanceof Boolean ) {
315312 res = value .toString ();
316- } else if (value instanceof Map || value instanceof List ) {
317- res = new Gson ().toJson (value );
313+ } else if (value instanceof Map ) {
314+ res = new JSONObject ((Map <?, ?>) value ).toString ();
315+ } else if (value instanceof List ) {
316+ res = new JSONArray ((List <?>) value ).toString ();
318317 } else {
319318 // Any other object → serialize as JSON
320- res = new Gson (). toJson (value );
319+ res = JSONObject . quote (value . toString () );
321320 }
322321 return res ;
323322 }
324323
325- private static boolean isJson (String input ) {
324+ private static boolean isJson (String str ) {
326325 try {
327- JsonElement json = JsonParser .parseString (input );
328- return json .isJsonObject () || json .isJsonArray ();
329- } catch (Exception e ) {
330- return false ;
326+ new JSONObject (str );
327+ return true ;
328+ } catch (Exception e1 ) {
329+ try {
330+ new JSONArray (str );
331+ return true ;
332+ } catch (Exception e2 ) {
333+ return false ;
334+ }
331335 }
332336 }
333337
@@ -339,13 +343,6 @@ private static List<String> getStackTraceAsList() {
339343 return stackTraceLines ;
340344 }
341345
342- // Convert a JSON String to Map<String, Object>
343- private static Map <String , Object > jsonStringToMap (String jsonString ) {
344- Gson gson = new Gson ();
345- Type type = new TypeToken <Map <String , Object >>(){}.getType ();
346- return gson .fromJson (jsonString , type );
347- }
348-
349346 private static boolean isJsonLogFormat () {
350347 LoggerContext context = (LoggerContext ) LogManager .getContext (false );
351348 Configuration config = context .getConfiguration ();
@@ -361,4 +358,38 @@ private static boolean isJsonLogFormat() {
361358 return false ;
362359 }
363360
361+ public static Map <String , Object > jsonStringToMap (String jsonString ) {
362+ JSONObject jsonObject = new JSONObject (jsonString );
363+ return toMap (jsonObject );
364+ }
365+
366+ private static Map <String , Object > toMap (JSONObject jsonObject ) {
367+ Map <String , Object > map = new LinkedHashMap <>();
368+ for (String key : jsonObject .keySet ()) {
369+ Object value = jsonObject .get (key );
370+ map .put (key , convert (value ));
371+ }
372+ return map ;
373+ }
374+
375+ private static List <Object > toList (JSONArray array ) {
376+ List <Object > list = new ArrayList <>();
377+ for (int i = 0 ; i < array .length (); i ++) {
378+ Object value = array .get (i );
379+ list .add (convert (value ));
380+ }
381+ return list ;
382+ }
383+
384+ private static Object convert (Object value ) {
385+ if (value instanceof JSONObject ) {
386+ return toMap ((JSONObject ) value );
387+ } else if (value instanceof JSONArray ) {
388+ return toList ((JSONArray ) value );
389+ } else if (value .equals (JSONObject .NULL )) {
390+ return null ;
391+ } else {
392+ return value ;
393+ }
394+ }
364395}
0 commit comments