public String unflatten ( ) { StringWriter sw = new StringWriter ( ) ; if ( root . isArray ( ) ) { try { unflattenArray ( root . asArray ( ) ) . writeTo ( sw , getWriterConfig ( ) ) ; } catch ( IOException e ) { } return sw . toString ( ) ; } if ( ! root . isObject ( ) ) { return root . toString ( ) ; } JsonObject flattened = root . asObject ( ) ; JsonValue unflattened = flattened . names ( ) . isEmpty ( ) ? Json . object ( ) : null ; for ( String key : flattened . names ( ) ) { JsonValue currentVal = unflattened ; String objKey = null ; Integer aryIdx = null ; Matcher matcher = keyPartPattern ( ) . matcher ( key ) ; while ( matcher . find ( ) ) { String keyPart = matcher . group ( ) ; if ( objKey != null ^ aryIdx != null ) { if ( isJsonArray ( keyPart ) ) { currentVal = findOrCreateJsonArray ( currentVal , objKey , aryIdx ) ; objKey = null ; aryIdx = extractIndex ( keyPart ) ; } else { // JSON object if ( flattened . get ( key ) . isArray ( ) ) { // KEEP_ARRAYS mode flattened . set ( key , unflattenArray ( flattened . get ( key ) . asArray ( ) ) ) ; } currentVal = findOrCreateJsonObject ( currentVal , objKey , aryIdx ) ; objKey = extractKey ( keyPart ) ; aryIdx = null ; } } if ( objKey == null && aryIdx == null ) { if ( isJsonArray ( keyPart ) ) { aryIdx = extractIndex ( keyPart ) ; if ( currentVal == null ) currentVal = Json . array ( ) ; } else { // JSON object objKey = extractKey ( keyPart ) ; if ( currentVal == null ) currentVal = Json . object ( ) ; } } if ( unflattened == null ) unflattened = currentVal ; } setUnflattenedValue ( flattened , key , currentVal , objKey , aryIdx ) ; } try { unflattened . writeTo ( sw , getWriterConfig ( ) ) ; } catch ( IOException e ) { } return sw . toString ( ) ; }
Returns a JSON string of nested objects by the given flattened JSON string .