public String encode(String decoded){
  JsonObject object=new JsonObject(decoded);
  String payloadString;
  JsonObject payloadObject=object.getJsonObject("payloadObject");
  if (payloadObject != null) {
    payloadString=payloadObject.encode();
  }
 else {
    payloadString=object.getString("payloadString");
  }
  if (payloadString != null) {
    object.put(PAYLOAD,payloadString.getBytes(Charset.forName(UTF_8)));
    object.remove("payloadString");
    object.remove("payloadObject");
  }
  int length=0;
  if (object.containsKey(PAYLOAD)) {
    length=object.getBinary(PAYLOAD).length;
  }
  JsonArray newHeaders=new JsonArray();
  for (  Object headerObj : object.getJsonArray("headers")) {
    JsonArray header=(JsonArray)headerObj;
    String key=header.getString(0);
    if (key.equalsIgnoreCase("content-length")) {
      JsonArray contentLengthHeader=new JsonArray();
      contentLengthHeader.add("Content-Length");
      contentLengthHeader.add(Integer.toString(length));
      newHeaders.add(contentLengthHeader);
    }
 else {
      newHeaders.add(header);
    }
  }
  object.put("headers",newHeaders);
  return object.toString();
}

public String encode ( String decoded ) { JsonObject object = new JsonObject ( decoded ) ; String payloadString ; JsonObject payloadObject = object . getJsonObject ( "payloadObject" ) ; if ( payloadObject != null ) { payloadString = payloadObject . encode ( ) ; } else { payloadString = object . getString ( "payloadString" ) ; } if ( payloadString != null ) { object . put ( PAYLOAD , payloadString . getBytes ( Charset . forName ( UTF_8 ) ) ) ; object . remove ( "payloadString" ) ; object . remove ( "payloadObject" ) ; } int length = 0 ; if ( object . containsKey ( PAYLOAD ) ) { length = object . getBinary ( PAYLOAD ) . length ; } JsonArray newHeaders = new JsonArray ( ) ; for ( Object headerObj : object . getJsonArray ( "headers" ) ) { JsonArray header = ( JsonArray ) headerObj ; String key = header . getString ( 0 ) ; if ( key . equalsIgnoreCase ( "content-length" ) ) { JsonArray contentLengthHeader = new JsonArray ( ) ; contentLengthHeader . add ( "Content-Length" ) ; contentLengthHeader . add ( Integer . toString ( length ) ) ; newHeaders . add ( contentLengthHeader ) ; } else { newHeaders . add ( header ) ; } } object . put ( "headers" , newHeaders ) ; return object . toString ( ) ; }
encode the payload from a payloadstring or payloadobjet .
