Class 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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA value that knows how to write itself as JSON. -
Method Summary
Modifier and TypeMethodDescriptionstatic Objectstatic MapparseObject(String json) Parses a JSON object.static Stringstatic voidWrites JSON as UTF-8 bytes straight into a reusable buffer.static voidwriteString(String value, ByteSink out) One JSON string, escaped, straight intoout.static voidwriteValue(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).
-
Method Details
-
parseObject
Parses a JSON object. Throws IOException on anything malformed.- Throws:
IOException
-
parse
- Throws:
IOException
-
write
-
write
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
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(Object)rediscover its type at run time. -
writeValue
One JSON value of unknown type, for the cases a generated codec cannot resolve statically (an unmodelled java.* type, a heterogeneous collection). The generated code uses the typed calls wherever it can and falls back here only where it must.
-