boonproject / boon

Simple opinionated Java for the novice to expert level Java Programmer. Low Ceremony. High Productivity.
http://richardhightower.github.io/site/Boon/Welcome.html
Apache License 2.0
520 stars 102 forks source link

Boon.toJson / Boon.toPrettyJson ignores getters #303

Closed Nothing4You closed 9 years ago

Nothing4You commented 9 years ago

Currently it seems like boon just completely ignores possibly available getters for properties. This prevents lazy loading of information, e.g. initializing an object with an id and loading info like name later when it's required to reduce load.

RichardHightower commented 9 years ago

Yes and no.

Yes it does if you use Boon.toJson and/or Boon.toPrettyJson.

Those are quick and easy. They do use fields. They will use the getters if they don't find the field.

But it does support getters if you use the JsonBuilder gak to configure serialization.

I will search doc for instructions and if I can't find it then I will write it up.

It supports. I am 99.999 percent sure.

On Thu, Feb 19, 2015 at 5:42 AM, Richard Schwab notifications@github.com wrote:

Currently it seems like boon just completely ignores possibly available getters for properties. This prevents lazy loading of information, e.g. initializing an object with an id and loading info like name later when it's required to reduce load.

— Reply to this email directly or view it on GitHub https://github.com/boonproject/boon/issues/303.

Rick Hightower (415) 968-9037 Profile http://www.google.com/profiles/RichardHightower

Nothing4You commented 9 years ago

Thanks, this works for me:

final JsonSerializer serializer = new JsonSerializerFactory().usePropertiesFirst().create();
response.getWriter().println(serializer.serialize(result));

The important part is .usePropertiesFirst(). Found a reference for the factory here and looked at the methods of the factory.

However, is there any way I could get pretty JSON from this? Would I have to parseJson this and then toPrettyJson it?

Nothing4You commented 9 years ago
final JsonSerializer serializer = new JsonSerializerFactory().usePropertiesFirst().create();
response.getWriter().println(Boon.toPrettyJson(Boon.fromJson(serializer.serialize(result).toString())));

works, but it'd be nice if I wouldn't have to parse and serialize it twice.

RichardHightower commented 9 years ago

Good work.