private static < T extends MPBase > JsonObject generatePayload ( HttpMethod httpMethod , T resource ) { JsonObject payload = null ; if ( httpMethod . equals ( HttpMethod . POST ) || ( httpMethod . equals ( HttpMethod . PUT ) && resource . _lastKnownJson == null ) ) { payload = MPCoreUtils . getJsonFromResource ( resource ) ; } else if ( httpMethod . equals ( HttpMethod . PUT ) ) { JsonObject actualJson = MPCoreUtils . getJsonFromResource ( resource ) ; Type mapType = new TypeToken < Map < String , Object > > ( ) { } . getType ( ) ; Gson gson = new Gson ( ) ; Map < String , Object > oldMap = gson . fromJson ( resource . _lastKnownJson , mapType ) ; Map < String , Object > newMap = gson . fromJson ( actualJson , mapType ) ; MapDifference < String , Object > mapDifferences = Maps . difference ( oldMap , newMap ) ; payload = new JsonObject ( ) ; for ( Map . Entry < String , MapDifference . ValueDifference < Object > > entry : mapDifferences . entriesDiffering ( ) . entrySet ( ) ) { if ( entry . getValue ( ) . rightValue ( ) instanceof LinkedTreeMap ) { JsonElement jsonObject = gson . toJsonTree ( entry . getValue ( ) . rightValue ( ) ) . getAsJsonObject ( ) ; payload . add ( entry . getKey ( ) , jsonObject ) ; } else { if ( entry . getValue ( ) . rightValue ( ) instanceof Boolean ) { payload . addProperty ( entry . getKey ( ) , ( Boolean ) entry . getValue ( ) . rightValue ( ) ) ; } else if ( entry . getValue ( ) . rightValue ( ) instanceof Number ) { payload . addProperty ( entry . getKey ( ) , ( Number ) entry . getValue ( ) . rightValue ( ) ) ; } else { payload . addProperty ( entry . getKey ( ) , entry . getValue ( ) . rightValue ( ) . toString ( ) ) ; } } } for ( Map . Entry < String , Object > entry : mapDifferences . entriesOnlyOnRight ( ) . entrySet ( ) ) { if ( entry . getValue ( ) instanceof Boolean ) { payload . addProperty ( entry . getKey ( ) , ( Boolean ) entry . getValue ( ) ) ; } else if ( entry . getValue ( ) instanceof Number ) { payload . addProperty ( entry . getKey ( ) , ( Number ) entry . getValue ( ) ) ; } else { payload . addProperty ( entry . getKey ( ) , entry . getValue ( ) . toString ( ) ) ; } } } return payload ; }
Transforms all attributes members of the instance in a JSON String . Only for POST and PUT methods . POST gets the full object in a JSON object . PUT gets only the differences with the last known state of the object .