Class Mappers
java.lang.Object
com.codename1.mapping.Mappers
public final class Mappers
extends java.lang.Object
Public entry point for the build-time JSON / XML mapping framework.
@Mapped classes get a generated mapper at build time. The generated
mapper's static initializer self-registers with this registry. The
registry stays empty until something triggers each generated class's
<clinit>:
- iOS / Android -- the build server probes the project zip for
cn1app.MapperBootstrap, and when present splices anew cn1app.MapperBootstrap();into the per-build application stub beforeDisplay.init. That constructor references every generated mapper, triggering their static initializers. - JavaSE simulator + desktop --
JavaSEPort#postInitcallsClass.forName("cn1app.MapperBootstrap")so the registry is populated on the same boundary. Classloading is the legitimate path here: JavaSE runs unobfuscated. - Unit tests / manual init -- application code can call
Mappers.register(...)directly to install a hand-written mapper for a class the build can't annotate.
Typical use after init:
String json = Mappers.toJson(user);
User u = Mappers.fromJson(json, User.class);
String xml = Mappers.toXml(user);
User u = Mappers.fromXml(xml, User.class);
The registry is keyed on getClass().getName() so it survives ParparVM
rename and R8 obfuscation: both the registration site and the lookup
site see the same renamed name within a single execution. The map keys
are never persisted, so the renaming has no observable effect on
behavior.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TfromJson(java.io.Reader json, java.lang.Class<T> type) Parses JSON read from aReader(file, network response, ...) without fully buffering it into a String first.static <T> TInverse of#toJson.static <T> TfromXml(java.io.Reader xml, java.lang.Class<T> type) Parses XML read from aReaderwithout fully buffering it first.static <T> TInverse of#toXml.static <T> Mapper<T> get(java.lang.Class<T> type) Looks up the mapper fortype(bytype.getName()) or null when none is registered.static <T> voidInstallsmapperundermapper.type().getName().static StringtoJson(java.lang.Object instance) Serializesinstanceto JSON.static StringtoXml(java.lang.Object instance) Serializesinstanceto XML.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
register
Installsmapperundermapper.type().getName(). The generated per-class mapper's static initializer calls this; hand-written mappers for classes outside the build's annotation scan call it explicitly. -
get
Looks up the mapper fortype(bytype.getName()) or null when none is registered. -
toJson
Serializesinstanceto JSON. ThrowsIllegalStateExceptionwhen no mapper is registered for its concrete class; that always points at a missing@Mappedannotation or a build that ran without the process-annotations Mojo. -
fromJson
Inverse of#toJson. Parses the JSON text and hands the resulting Map to the registered mapper. -
fromJson
public static <T> T fromJson(java.io.Reader json, java.lang.Class<T> type) Parses JSON read from aReader(file, network response, ...) without fully buffering it into a String first. -
toXml
Serializesinstanceto XML. -
fromXml
Inverse of#toXml. Parses the XML text and hands the resulting Element to the registered mapper. -
fromXml
public static <T> T fromXml(java.io.Reader xml, java.lang.Class<T> type) Parses XML read from aReaderwithout fully buffering it first.
-