Class Json
JSON for server-side binaries: the core's strict reader and a streaming writer.
Reading is com.codename1.io.JSONParser#parseStrict, the same code the device runs (JSONParser is shared with the backend). This class once carried a parser of its own because JSONParser reached com.codename1.processing.Result and, through it, the XML DOM; that dependency is gone. Writing stays here because it streams into a ByteSink and honours Writable and Encoder, which the core writer has no notion of.
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. Anything malformed -- trailing content, a raw control character in a string, a number outside the JSON grammar or a long, nesting deeper than 512 -- throws IOException.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceWrites values of one type as JSON, shared by every request that answers with that type.static interfaceA value that knows how to write itself as JSON. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TReads a document already parsed byparse(String)-- or a member of one -- intotype, as Jackson'sconvertValuedoes.static ObjectParses any JSON value.static MapparseObject(String json) Parses a JSON object.static <T> TReads a JSON document intotype, as Jackson'sreadValuedoes.static <T> List<T> Reads a JSON array ofelement, as Jackson'sreadValue(json, new TypeReference<List<T>>() {})does.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
Parses any JSON value. Throws IOException on anything malformed.- Throws:
IOException
-
read
Reads a JSON document into
type, as Jackson'sreadValuedoes.Release release = Json.read(body, Release.class);An application class is read by the codec the build generated for it -- the one a route taking it as a
@RequestBodyuses, so@JsonProperty,@JsonIgnoreand the rest mean the same thing here. The build writes a codec for every class literal passed to this method,readList(String, Class)andconvert(Object, Class)in the application's code, so name the class literally: aClassthat arrives in a variable is read only if some other use made the build write its codec, and is refused withIllegalStateExceptionotherwise.String, the boxed numbers,Boolean,Map,ListandObjectneed no codec.- Parameters:
json- the documenttype- what to read it into- Returns:
- the value, or null for a JSON
null - Throws:
IOException- whenjsonis not JSONIllegalArgumentException- when the document does not fittype; the message names where, as$.items[2].due
-
readList
Reads a JSON array ofelement, as Jackson'sreadValue(json, new TypeReference<List<T>>() {})does.- Parameters:
json- the document, an arrayelement- what each element is read into; seeread(String, Class)- Returns:
- the elements, in order
- Throws:
IOException- whenjsonis not JSONIllegalArgumentException- when it is not an array, or an element does not fitelement
-
convert
Reads a document already parsed byparse(String)-- or a member of one -- intotype, as Jackson'sconvertValuedoes. Seeread(String, Class).- Parameters:
tree- whatparse(String)returned, or part of ittype- what to read it into- Returns:
- the value, or null for null
- Throws:
IllegalArgumentException- whentreedoes not fittype
-
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.
-