package com.tcl.ah.module.serverinfo.data;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class Wrapper {
    @JsonProperty("code")
    public int code;
    @JsonProperty("data")
    public MyData data;

    public static Wrapper parse(String jsonStr) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,false);
        mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE,false);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true);
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        return mapper.readValue(jsonStr,new TypeReference<Wrapper>() {});
    }

    @Override
    public String toString() {
        return "Wrapper{" +
                "code=" + code +
                ", data=" + data +
                '}';
    }

    public static class MyData {
        @JsonProperty("errMsg")
        public String errMsg;
        @JsonProperty("dataStr")
        public String dataStr;

        @Override
        public String toString() {
            return "Data{" +
                    "errMsg='" + errMsg + '\'' +
                    ", dataStr='" + dataStr + '\'' +
                    '}';
        }
    }
}
