Class JsonCodec

java.lang.Object
com.codename1.backend.JsonCodec

public final class JsonCodec extends Object

What the JSON codecs the build generates call: typed reads that refuse a value with a message naming where it was, and the date form.

A controller that returns or accepts one of the application's own classes -- an entity, a DTO -- gets a codec written for that class at build time, the way Jackson would map it at run time: its fields by name, @JsonProperty renaming one and @JsonIgnore leaving one out. The codec calls these methods, so nothing here is looked up by name or reflected on, and a server with no such route never links this class.

A refused value is an IllegalArgumentException whose message says where in the body it was and what was expected -- $.items[2].due: expected a number or an ISO-8601 date, got true -- which the generated router answers with 400.

  • Field Details

    • MAX_DEPTH

      public static final int MAX_DEPTH
      How deep a codec follows one object into another before it refuses: past any real document, and short of the stack a cycle between two objects would otherwise exhaust.
      See Also:
  • Method Details

    • enter

      public static JsonCodec.Path enter(JsonCodec.Path at, String name, int index)
      The path of member name or element index of at, or at itself when neither is given -- where a codec is when it starts reading an object or an array.
    • mismatch

      public static IllegalArgumentException mismatch(JsonCodec.Path at, String name, int index, String expected, Object got)
      The refusal of got at member name or element index of at; pass null and -1 for at itself.
    • tooDeep

      public static IllegalStateException tooDeep(String type)
      Refuses an object nested past MAX_DEPTH, which in a response means a cycle: two objects that reach each other through their fields.
    • readLong

      public static long readLong(Object json, JsonCodec.Path at, String name, int index, long min, long max)
      A whole number between min and max. A number with a fraction is refused rather than cut short.
    • readDouble

      public static double readDouble(Object json, JsonCodec.Path at, String name, int index)
      Any number.
    • readBoolean

      public static boolean readBoolean(Object json, JsonCodec.Path at, String name, int index)
      true or false.
    • readChar

      public static char readChar(Object json, JsonCodec.Path at, String name, int index)
      A string of one character.
    • readString

      public static String readString(Object json, JsonCodec.Path at, String name, int index)
      A string, or null.
    • readBytes

      public static byte[] readBytes(Object json, JsonCodec.Path at, String name, int index)
      Base64 text, or null.
    • readDate

      public static Date readDate(Object json, JsonCodec.Path at, String name, int index)
      Milliseconds since the epoch -- the form writeDate(Date, ByteSink) writes and the app's mapper reads -- or an ISO-8601 date, with or without a time and an offset; one without an offset is UTC. Null stays null.
    • writeDate

      public static void writeDate(Date value, ByteSink out)
      A date as milliseconds since the epoch, as Jackson writes one by default and as the app's generated mapper reads it back.