Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class LevelJson

  1. Object
  2. LevelJson

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.

Methods

public static Object parse(String text)Parses JSON text.
public static void quote(StringBuilder sb, String s)Writes a JSON string literal.
public static int fx(Map<String, Object> m, String key, int def)A number field as fixed point.
public static int integer(Map<String, Object> m, String key, int def)A number field as a whole number (the fixed-point value floored).
public static String str(Map<String, Object> m, String key, String def)A string field.
public static Map<String, Object> obj(Map<String, Object> m, String key)An object field.
public static List<Object> list(Map<String, Object> m, String key)An array field.

Inherited methods

Method details

parse

public static Object parse(String text)
Parses JSON text.

Parameters

text String
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 StringBuilder
the output
s String
the string

fx

public static int fx(Map<String, Object> m, String key, int def)
A number field as fixed point.

Parameters

m Map<String, Object>
the object
key String
the field
def int
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 Map<String, Object>
the object
key String
the field
def int
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 Map<String, Object>
the object
key String
the field
def String
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 Map<String, Object>
the object
key String
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 Map<String, Object>
the object
key String
the field

Returns

the list, or an empty list