Annotation Type JsonProperty


@Retention(CLASS) @Target({FIELD,PARAMETER}) public @interface JsonProperty

Names a field in the JSON form, and says how a body may spell it.

The default JSON key is the field name; value lets a field map to snake_case or any other spelling without touching the Java identifier. aliases are further names a body may use for the same member -- an old spelling a client still sends -- and are only ever read: the value is always written under value. required makes a body that leaves the member out a refused body, naming the member; an explicit null is still a value, so it is accepted (and refused for a primitive, which cannot hold it).

On a parameter of a JsonCreator constructor or factory it names the member that argument is read from, since a compiled class does not keep its parameter names.

The server's generated codecs read it too, so a class shared between an app and its backend has one JSON form on both sides.

  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Other keys a body may carry the value under.
    boolean
    Whether a body that leaves the member out is refused.
    The JSON key; empty for the field's own name.
  • Element Details

    • value

      String value
      The JSON key; empty for the field's own name.
      Default:
      ""
    • aliases

      String[] aliases
      Other keys a body may carry the value under. Read only, never written.
      Default:
      {}
    • required

      boolean required
      Whether a body that leaves the member out is refused.
      Default:
      false