ralfstx / minimal-json

A fast and small JSON parser and writer for Java
MIT License
732 stars 185 forks source link

Support converting POJO => JSON #63

Closed evandrix closed 8 years ago

evandrix commented 8 years ago

right now, the conversion seems to be just only one-way, i.e. JSON string to custom Java object (JsonValue, JsonObject, ...)

ralfstx commented 8 years ago

You can create JSON from your POJOs, but you have to do it yourself. Here's an example how to turn a simple POJO into JSON:

String toJson(Release release) {
  return new JsonObject()
    .add("version", release.getVersion())
    .add("date", formatDate(release.getDate()))
    .toString();
}

The reason minimal-json doesn't magically convert POJOs is that it aims at being

A fast and minimal JSON parser and writer for Java. It's not an object mapper ...

Mapping POJOs to JSON is not a trivial task. As an example, there's more than one method of formatting a date in JSON, a mapper would either have to guess offer some kind of configuration. There are object mappers out there (GSON for example) that provide all those things.