Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class Json
- Object
- Json
A self-contained JSON reader/writer for server-side binaries.
Deliberately not com.codename1.io.JSONParser: that class reaches com.codename1.processing.Result, which reaches com.codename1.xml.Element, so reusing it would link an XML DOM into every server binary to parse a request body. The mapping package has the same problem (Mapper imports Element).
Values map as: object to LinkedHashMap (insertion-ordered so a round trip is stable), array to ArrayList, string to String, number to Double or Long, true/false to Boolean, null to null.
Nested types
interface Json.Writable | A value that knows how to write itself as JSON. |
Methods
public static Map parseObject(String json)
throws IOException | Parses a JSON object. |
public static Object parse(String json)
throws IOException | |
public static String write(Object value) | |
public static void write(Object value, ByteSink out) | Writes JSON as UTF-8 bytes straight into a reusable buffer. |
public static void writeString(String value, ByteSink out) | One JSON string, escaped, straight into out. |
public static void writeValue(Object value, ByteSink out) | One JSON value of unknown type, for the cases a generated codec cannot resolve statically (an unmodelled java.* type, a heterogeneous collection). |
Inherited methods
Method details
parseObject
public static Map parseObject(String json)
throws IOExceptionThrows
parse
public static Object parse(String json)
throws IOExceptionThrows
write
public static String write(Object value)write
public static void write(Object value, ByteSink out)Writes JSON as UTF-8 bytes straight into a reusable buffer.
The String-returning form above builds a StringBuilder, grows its char[] several times, copies it into a String and then encodes that to bytes – four allocations for a document the server is about to write to a socket and discard. Measured on a small JSON response, that chain was most of the per-request allocation once the response head had been dealt with, and it kept the collector’s run-ahead cap firing.
Same output as write(Object), byte for byte.
writeString
public static void writeString(String value, ByteSink out)One JSON string, escaped, straight into out.
Public because generated code calls it. A codec that knows its field types
at build time emits a direct call here instead of putting the value in a Map
and letting write rediscover its type at run time.
writeValue
public static void writeValue(Object value, ByteSink out)