# Class Mappers

java.lang.Object
com.codename1.mapping.Mappers

public final class Mappers extends 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 a `new cn1app.MapperBootstrap();` into the per-build application stub before `Display.init`. That constructor references every generated mapper, triggering their static initializers.
- JavaSE simulator + desktop -- `JavaSEPort#postInit` calls `Class.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 Type
  Method
  Description
  `static <T> T`
  `fromJson(Reader json, Class<T> type)`
  Parses JSON read from a `Reader` (file, network response, ...) without fully buffering it into a String first.
  `static <T> T`
  `fromJson(String json, Class<T> type)`
  Inverse of `#toJson`.
  `static <T> T`
  `fromXml(Reader xml, Class<T> type)`
  Parses XML read from a `Reader` without fully buffering it first.
  `static <T> T`
  `fromXml(String xml, Class<T> type)`
  Inverse of `#toXml`.
  `static <T> Mapper<T>`
  `get(Class<T> type)`
  Looks up the mapper for `type` (by `type.getName()`) or null when none is registered.
  `static <T> void`
  `register(Mapper<T> mapper)`
  Installs `mapper` under `mapper.type().getName()`.
  `static String`
  `toJson(Object instance)`
  Serializes `instance` to JSON.
  `static String`
  `toXml(Object instance)`
  Serializes `instance` to XML.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### register

    public static <T> void register(Mapper<T> mapper)
    Installs `mapper` under `mapper.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

    public static <T> Mapper<T> get(Class<T> type)
    Looks up the mapper for `type` (by `type.getName()`) or null when none is registered.

  - ### toJson

    public static String toJson(Object instance)
    Serializes `instance` to JSON. Throws `IllegalStateException` when no mapper is registered for its concrete class; that always points at a missing `@Mapped` annotation or a build that ran without the process-annotations Mojo.

  - ### fromJson

    public static <T> T fromJson(String json, Class<T> type)
    Inverse of `#toJson`. Parses the JSON text and hands the resulting Map to the registered mapper.

  - ### fromJson

    public static <T> T fromJson(Reader json, Class<T> type)
    Parses JSON read from a `Reader` (file, network response, ...) without fully buffering it into a String first.

  - ### toXml

    public static String toXml(Object instance)
    Serializes `instance` to XML.

  - ### fromXml

    public static <T> T fromXml(String xml, Class<T> type)
    Inverse of `#toXml`. Parses the XML text and hands the resulting Element to the registered mapper.

  - ### fromXml

    public static <T> T fromXml(Reader xml, Class<T> type)
    Parses XML read from a `Reader` without fully buffering it first.