public static Object toBean ( JSONObject jsonObject ) { if ( jsonObject == null || jsonObject . isNullObject ( ) ) { return null ; } DynaBean dynaBean = null ; JsonConfig jsonConfig = new JsonConfig ( ) ; Map props = JSONUtils . getProperties ( jsonObject ) ; dynaBean = JSONUtils . newDynaBean ( jsonObject , jsonConfig ) ; for ( Iterator entries = jsonObject . names ( jsonConfig ) . iterator ( ) ; entries . hasNext ( ) ; ) { String name = ( String ) entries . next ( ) ; String key = JSONUtils . convertToJavaIdentifier ( name , jsonConfig ) ; Class type = ( Class ) props . get ( name ) ; Object value = jsonObject . get ( name ) ; try { if ( ! JSONUtils . isNull ( value ) ) { if ( value instanceof JSONArray ) { dynaBean . set ( key , JSONArray . toCollection ( ( JSONArray ) value ) ) ; } else if ( String . class . isAssignableFrom ( type ) || Boolean . class . isAssignableFrom ( type ) || JSONUtils . isNumber ( type ) || Character . class . isAssignableFrom ( type ) || JSONFunction . class . isAssignableFrom ( type ) ) { dynaBean . set ( key , value ) ; } else { dynaBean . set ( key , toBean ( ( JSONObject ) value ) ) ; } } else { if ( type . isPrimitive ( ) ) { // assume assigned default value log . warn ( "Tried to assign null value to " + key + ":" + type . getName ( ) ) ; dynaBean . set ( key , JSONUtils . getMorpherRegistry ( ) . morph ( type , null ) ) ; } else { dynaBean . set ( key , null ) ; } } } catch ( JSONException jsone ) { throw jsone ; } catch ( Exception e ) { throw new JSONException ( "Error while setting property=" + name + " type" + type , e ) ; } } return dynaBean ; }
Creates a JSONDynaBean from a JSONObject .