Class Mappers
java.lang.Object
com.codename1.mapping.Mappers
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> TParses JSON read from aReader(file, network response, ...) without fully buffering it into a String first.static <T> TInverse of#toJson.static <T> TParses XML read from aReaderwithout fully buffering it first.static <T> TInverse of#toXml.static <T> Mapper<T> Looks up the mapper fortype(bytype.getName()) or null when none is registered.static <T> voidInstallsmapperundermapper.type().getName().static StringSerializesinstanceto JSON.static StringSerializesinstanceto XML.
-
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
-
toJson
-
fromJson
-
fromJson
-
toXml
-
fromXml
-
fromXml
-