/Java-JSON-Object-Mapper

Utility to map JSON to Java objects. Is an Object Mapper between Java and JSON.

Primary LanguageJava

Java JSON Object Mapper Build Status

The JSON Object Mapper will hydrate an entity from a JSON string.

Maven

Repository

<repositories>
    <repository>
        <id>scottbyrns-snapshots</id>
        <url>https://github.com/scottbyrns/Scottbyrns-Maven-Repo/raw/master/snapshots</url>
    </repository>
</repositories>

Dependency

<dependency>
    <groupId>com.scottbyrns.utilities</groupId>
    <artifactId>json-object-mapper</artifactId>
    <version>1.1.3-SNAPSHOT</version>
</dependency>

Declaring our Entity.

/**
 * Example entity for testing the JSON object mapper.
 */
public class GeoLocation {
    private double latitude;
    private double longitude;

    public void setLatitude (double latitude) {
        this.latitude = latitude;
    }

    public double getLatitude () {
        return latitude;
    }

    public void setLongitude (double longitude) {
        this.longitude = longitude;
    }

    public double getLongitude () {
        return longitude;
    }
}

Mapping our JSON to our Entity.

GeoLocation location = JSONObjectMapper.mapJSONStringToEntity(
        "{ \"latitude\": 46.0231, \"longitude\": -116.1239 }",
        GeoLocation.class
);

Note: The properties of your entity class must match the JSON keys.

Exceptions

The JSONObjectMapper can throw one of two exceptions:

InvalidJSONStringException

catch (InvalidJSONStringException e) {
          // The JSON string was invalid.
}

FatalMappingException

catch (FatalMappingException e) {
          // The entity was not compatible.
}

Support or Contact

Having trouble with the JSON Object Mapper? Generate the documentation with JavaDoc or contact me, (@scottbyrns) directly and I'll help you sort it out.