private static Object toJson ( Object obj , JsonBuilderFactory factory ) { if ( obj instanceof JsonObject ) { return ( ( JsonObject ) obj ) . toJson ( factory . createJsonBuilder ( ) ) ; } else if ( obj . getClass ( ) . isArray ( ) ) { List < Object > r = new ArrayList <> ( ) ; int len = Array . getLength ( obj ) ; for ( int i = 0 ; i < len ; ++ i ) { Object ao = Array . get ( obj , i ) ; r . add ( toJson ( ao , factory ) ) ; } return r ; } else if ( obj instanceof Collection ) { Collection < ? > coll = ( Collection < ? > ) obj ; List < Object > r = new ArrayList <> ( ) ; for ( Object ao : coll ) { r . add ( toJson ( ao , factory ) ) ; } return r ; } else if ( obj instanceof Map ) { Map < ? , ? > m = ( Map < ? , ? > ) obj ; Map < String , Object > r = new LinkedHashMap <> ( ) ; for ( Map . Entry < ? , ? > e : m . entrySet ( ) ) { String key = toJson ( e . getKey ( ) , factory ) . toString ( ) ; Object value = toJson ( e . getValue ( ) , factory ) ; r . put ( key , value ) ; } return r ; } return obj ; }
Converts an object to a JSON object