Reputation: 3769
I use Retrofit from Square as my REST Client for communications in Android. I discovered that some of server side services do not serialize well our objects. How to serilize only internal fields of an object?
Like in this example, how to convert this class:
public class Car {
private String plate;
private String carName;
}
Into this JSON representation (ignore cases):
{
"plate":"abc123",
"carname":"BMW"
}
NOT into this:
{
"car":
{
"plate":"abc123",
"carname":"BMW"
}
}
I actually have to do this kind of serialization for some classes and standard serialization for others.
Upvotes: 0
Views: 45
Reputation: 158
When you make the request using retrofit, send each part of the Car object as an @Query (for @GET requests) and as an @Field (for @POST requests.)
Example request
@FormUrlEncoded
@POST("www.foo.bar/example.json")
ExampleResponse getExample(@Field("plate") Car.plate,
@Field("carname") Car.carname);
Using @Body
@POST("www.foo.bar/example.json")
ExampleResponse getExample(@Body Car car);
(using @POST with @Body does not require @FormUrlEncoded)
Upvotes: 1