Class LevelJson

java.lang.Object
com.codename1.gaming.platformer.LevelJson

public final class LevelJson extends Object

A small JSON reader and writer for level files, shared by the app and the server.

Parsing is com.codename1.io.JSONParser's, with one difference: numbers are read from their text straight into fixed point (Fx#parse) as Integers, never through double, so "speed": 1.5 means exactly the same thing on every runtime -- the server replays a run with these values and must reach the hash the device did. Objects become Map (in document order), arrays List, and true, false and null what one would expect.

  • Method Details

    • parse

      public static Object parse(String text)

      Parses JSON text.

      Parameters
      • text: the JSON
      Returns

      a Map for an object, a List for an array

      Throws
      • IllegalArgumentException: when the text is neither, or a number is out of fixed-point range
    • quote

      public static void quote(StringBuilder sb, String s)

      Writes a JSON string literal.

      Parameters
      • sb: the output
      • s: the string
    • fx

      public static int fx(Map<String,Object> m, String key, int def)

      A number field as fixed point.

      Parameters
      • m: the object
      • key: the field
      • def: the value when absent, fixed point
      Returns

      the field, fixed point

    • integer

      public static int integer(Map<String,Object> m, String key, int def)

      A number field as a whole number (the fixed-point value floored).

      Parameters
      • m: the object
      • key: the field
      • def: the value when absent
      Returns

      the field

    • str

      public static String str(Map<String,Object> m, String key, String def)

      A string field.

      Parameters
      • m: the object
      • key: the field
      • def: the value when absent
      Returns

      the field

    • obj

      public static Map<String,Object> obj(Map<String,Object> m, String key)

      An object field.

      Parameters
      • m: the object
      • key: the field
      Returns

      the nested object, or null

    • list

      public static List<Object> list(Map<String,Object> m, String key)

      An array field.

      Parameters
      • m: the object
      • key: the field
      Returns

      the list, or an empty list