Gson entity class to deserialize json

In gson benchmark over process the performance is close to Jackson, Jackson doubt safety and performance stability.

I had encountered continuous fastjson multiple bug, he finally gave up, put into the embrace of Jackson; but Jackson api to use more configuration.

Api is simple enough, some scenes my json is a simple matter to convert json from each other (serialization and de-serialization) between the entities.

gson the api is relatively simple, is the origin of this article, the following anti-serialization code.

@SerializedName used to indicate the entity json inconsistent with the field name issue.

import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/***
 * ASR server status
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ASRServerStatus {


    /***
     * The number of currently idle worker
     */
    @SerializedName("num_workers_available")
    private Integer availWorkers;


    // The following fields are added up at the python subsequent collection server, the interface returns the current data are not yet related fields

    /***
     * The number of tasks have been processed
     */
    @SerializedName("num_requests_processed")
    private Integer processedRequests;

    /***
     * Server Load 1 minute
     */
    private Float load1;

    /***
     * Server Load 5 minutes
     */
    private Float load5;

    /***
     * Server Load 15 minutes
     */
    private Float load15;


    private Float totalMem;

    /***
     * Cached/Shared Memory
     */
    private Float sharedMem;

    /***
     * Obtained by calculation by the formula: total-buff-used
     */
    private Float availMem;

    private Float usedMem;
}
import com.google.gson.Gson;
import com.netmarch.web.open.ws.ASRServerStatus;
import org.junit.Test;

public  class TestGson {
    @Test
    public void deserial()
    {
        String json ="{\"num_workers_available\": 9, \"num_requests_processed\": 61}";
        Gson gson = new Gson ();
        ASRServerStatus status = gson.fromJson(json, ASRServerStatus.class);
        System.out.println(status);
    }
}

result:

 

Guess you like

Origin www.cnblogs.com/passedbylove/p/11934157.html