The original link: http://www.dubby.cn/detail.html?id=9071

Field named

  • @JsonPropertyYou can specify the name of the field (you can also specify that the field needs to participate in serialization and deserialization).
    • @JsonProperty.value: Specifies the field name
    • @JsonProperty.index: Specifies the order, the write data format is based on the order (JSON is not this data format)
    • @JsonProperty.defaultValue: Default value. Note: This property is not currently used by core and data-bind; Prepare some extension modules for use.

Field contains

  • @JsonAutoDetect: Defines the default field inclusion rules
  • @JsonIgnore: Ignores a specified field:
    • Modifier fields, any one of the setters and getters, you add everything
      • Unless you use@JsonPropertyModifier, which can be implemented by ignoring only serialization or deserialization
  • @JsonIgnoreProperties: Decorates a class, specifying that a list of fields should be ignored or that unknown fields should be ignored
  • @JsonIgnoreType: decorates a class, ignoring fields of the specified type
  • @JsonInclude: Can define whether null values participate in (de-serialization)

Field documents, metadata

  • @JsonPropertyDescription:2.3 Support for configuring human reading interpretations for fields
    • This annotation is not used by core and data-bind, but is mainly used by the JSON Schema Generator module

Deserialization and serialization details

  • @JsonFormat: For Date/Time fields, you can specify a format
  • @JsonUnwrapped: Specifies that a field (of type POJO) should be serialized into a flat rather than a nested object that will be wrapped into an object when deserialized
  • @JsonView: Views can be defined

@jsonunwrapped (prefix = “pre”)

public class MyValue {
    public String name;
    JsonUnwrapped(prefix = "pre_", suffix = "_suf")
    public MyValue myValue;
    public int age;
    public Date date;
}
Copy the code

Serialization results:

{"name":"Yang jeong"."pre_name_suf":null,"pre_age_suf": 0."pre_date_suf":null,"age": 24,"date":"2017-12-09"}
Copy the code

JsonView simple explanation:

public class JsonViewTest {

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();

        String json = "{\"username\":\"dubby.cn\",\"password\":\"123456\"}"; / / deserialization, use the view User User = objectMapper. ReaderWithView (User. UserWithoutPassword. Class). ForType (User. The class). ReadValue (json);  System.out.println(user); user.password ="xxxx"; / / serialization, using views String result1 = objectMapper. WriterWithView (User. UserWithoutPassword. Class). WriteValueAsString (User); System.out.println(result1); String result2 = objectMapper.writerWithView(User.UserWithPassword.class).writeValueAsString(user); System.out.println(result2); } } class User { @JsonView({UserWithoutPassword.class}) public String username; @JsonView({UserWithPassword.class}) public String password; public interface UserWithPassword extends UserWithoutPassword { } public interface UserWithoutPassword { } @Override public StringtoString() {
        return "User{" +
                "username='" + username + '\'' + ", password='" + password + '\'' + '}'; }}Copy the code

Deserialization details

  • @JacksonInject: Indicates that the value of a field is injected, not extracted from JSON
  • @JsonAnySetter: modifies a two-argument method. Any fields that are present in JSON but not in the object are passed to this method as (key,value)
  • @JsonCreator: Previous articleCustom constructorIntroduced the
  • @JsonSetterIs:@JsonPropertyAlternative notes to
  • @JsonEnumDefaultValue: If an undefined enumeration value is encountered during deserialization, the default enumeration is assigned

JsonAnySetter simple explanation:

public class JsonAnySetterTest {

    public static void main(String[] args) throws IOException {
        String json = "{\"username\":\"dubby.cn\",\"password\":\"123456\",\"x-key\":\"xxx-value\",\"y-key\":\"yyy-value\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        Data data = objectMapper.readValue(json, Data.class);
        System.out.println(data);
    }

}

class Data {

    public String username;

    public String password;

    public String other;

    @JsonAnySetter
    public void anySetter(String a, String b) {
        if (other == null) {
            other = "";
        }

        other += a;
        other += ",";
        other += b;
        other += ";";
    }

    @Override
    public String toString() {
        return "Data{" +
                "username='" + username + '\'' + ", password='" + password + '\'' + ", other='" + other + '\' ' +
                '} '; }}Copy the code

Output:

Data{username='dubby.cn', password='123456', other='x-key,xxx-value; y-key,yyy-value; '}
Copy the code

@jsonEnumDefaultValue

public class EnumTest {

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE, true);

        String json = "{\"myEnum\":\"V4\"}";
        EnumData enumData = objectMapper.readValue(json, EnumData.class);
        System.out.println(enumData);
    }

}

class EnumData {
    public MyEnum myEnum;
    @Override
    public String toString() {
        return "EnumData{" +
                "myEnum=" + myEnum +
                '} ';
    }
}

enum MyEnum {
    V1, V2, V3, @JsonEnumDefaultValue Default;
}
Copy the code

Output:

EnumData{myEnum=Default}
Copy the code

Serialization details

  • @JsonAnyGetter: Decorates a method, returnsMapThe return value of this method is serialized into the (key,value) form
  • @JsonGetter:@JsonPropertAlternative notes to
  • @JsonPropertyOrder: Determines the serialization order
  • @JsonRawValue: The fields to be decorated appear “exactly”, without escaping or decorating, and without double quotes
  • @JsonValue: Specifies the serialized output value
  • @JsonRootName: Uses the specified value as the root of the JSON, provided thatSerializationFeature.WRAP_ROOT_VALUEIt’s already open

JsonAnyGetter simple explanation:

public class JsonAnyGetterTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        AnyGetterData data = new AnyGetterData();
        data.data = "http://dubby.cn";
        System.out.println(objectMapper.writeValueAsString(data));
    }
}

class AnyGetterData {

    public String data;

    @JsonAnyGetter
    public Map other() {
        Map<String, String> map = new HashMap<>();
        map.put("key1"."value1");
        map.put("key2"."value2");
        map.put("key3"."value3");
        returnmap; }}Copy the code

Output:

{"data":"http://dubby.cn"."key1":"value1"."key2":"value2"."key3":"value3"}
Copy the code

@jsonPropertyOrder

public class JsonPropertyOrderTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonPropertyOrderData data = new JsonPropertyOrderData();
        data.name1 = "value1";
        data.name2 = "value3";
        data.name3 = "value4";
        System.out.println(objectMapper.writeValueAsString(data));
    }
}

@JsonPropertyOrder(value = {"name2"."name3"."name1"})
class JsonPropertyOrderData {
    public String name1;
    public String name2;
    public String name3;
}
Copy the code

Output:

{"name2":"value3"."name3":"value4"."name1":"value1"}
Copy the code

@jsonValue

public class JsonValueTest { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new  ObjectMapper(); String json ="{\"name2\":\"value3\",\"name3\":\"value4\",\"name1\":\"value1\"}";
        JsonValueData data = objectMapper.readValue(json, JsonValueData.class);
        System.out.println(data.toString());
        System.out.println(objectMapper.writeValueAsString(data));
    }
}
class JsonValueData {
    public String name1;
    public String name2;
    public String name3;
    @JsonValue
    public String other() {
        return name1+name2+name3;
    }

    @Override
    public String toString() {
        return "JsonValueData{" +
                "name1='" + name1 + '\'' + ", name2='" + name2 + '\'' + ", name3='" + name3 + '\' ' +
                '} '; }}Copy the code

Output:

JsonValueData{name1='value1', name2='value3', name3='value4'}
"value1value3value4"
Copy the code

@jsonRootName

public class JsonRootNameTest {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
        RootData data = new RootData();
        data.name1 = "value1";
        data.name2 = "value2";
        data.name3 = "value3";
        System.out.println(objectMapper.writeValueAsString(data));
    }
}
@JsonRootName(value = "root")
class RootData {
    public String name1;
    public String name2;
    public String name3;
}
Copy the code

Output:

{"root": {"name1":"value1"."name2":"value2"."name3":"value3"}}
Copy the code