Object serialization feature tuning

29 views
Skip to first unread message

Сергей Нелюбин

unread,
Oct 30, 2019, 1:07:32 PM10/30/19
to Agrest Framework User Group
Hello, Andrus!

Can i customize serialization settings for agrest output?

For example, i want disable serialize nullable attributes.


Andrus Adamchik

unread,
Oct 31, 2019, 2:49:57 AM10/31/19
to Agrest Framework User Group
Hi Sergey,

There are a few ways to customize aspects of JSON encoding. Here is one that may be applicable in your case. Since Agrest 3.3 there is an injectable ValueEncoders service that maintains a collection of common property encoders per value type as well as a generic fallback encoder, and ValueEncodersProvider that builds an instance of that class. You can create a provider subclass, and bind it via a custiom Module:

AgBuilder.builder(runtime).module(binder -> {
binder.bind(ValueEncoders.class).toProvider(MyValueEncodersProvider.class);
})
.build();

In MyValueEncodersProvider you may override "createValueEncoders" method to decorate all the preloaded encoders to skip null values.

Andrus

Сергей Нелюбин

unread,
Oct 31, 2019, 6:19:14 AM10/31/19
to Agrest Framework User Group
Hi Andrus,

It worked!

But what about calculated attributes (built by AgEntityOverlay) ?
If i return null at method addAttribute, null value serialized.



четверг, 31 октября 2019 г., 9:49:57 UTC+3 пользователь Andrus Adamchik написал:

Andrus Adamchik

unread,
Oct 31, 2019, 6:33:08 AM10/31/19
to Agrest Framework User Group
This is odd. AgEntityOverlay attributes are handled the same way as any regular attributes. Could be a bug (?)

Are you using Agrest 3.3? Also are you attaching the overlay per request, or load it to Agrest runtime on startup?

Andrus
> --
> You received this message because you are subscribed to the Google Groups "Agrest Framework User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to agrest-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/agrest-user/1a8e910b-0271-401b-af64-72b4c97f282a%40googlegroups.com.

Сергей Нелюбин

unread,
Oct 31, 2019, 7:12:00 AM10/31/19
to Agrest Framework User Group
Yes, version 3.3, load Overlay on startup.

I make wrapper for defaultEncoder only

protected ValueEncoders createValueEncoders(Map<Class<?>, Encoder> encoders, Encoder defaultEncoder) {
return super.createValueEncoders(encoders, new GenericEncoderExt( defaultEncoder ));
}

Map<Class<?>, Encoder> encoders contains only Date/Time Encoders


четверг, 31 октября 2019 г., 13:33:08 UTC+3 пользователь Andrus Adamchik написал:
> To unsubscribe from this group and stop receiving emails from it, send an email to agrest-user+unsubscribe@googlegroups.com.

Сергей Нелюбин

unread,
Oct 31, 2019, 7:17:56 AM10/31/19
to Agrest Framework User Group
And further, my overlay is Map type
This is probably the reason, because genericEncoder applies to simple attributes, not collections



четверг, 31 октября 2019 г., 14:12:00 UTC+3 пользователь Сергей Нелюбин написал:

Сергей Нелюбин

unread,
Oct 31, 2019, 7:35:09 AM10/31/19
to Agrest Framework User Group
Can i override ListEncoder behavior ?


четверг, 31 октября 2019 г., 14:17:56 UTC+3 пользователь Сергей Нелюбин написал:

Andrus Adamchik

unread,
Nov 4, 2019, 1:12:03 AM11/4/19
to Agrest Framework User Group
Yeah, Agrest makes a distinction between "values" and "entities" when creating JSON. Entities result in JSON trees, and hence require recursive descent during encoding. Can't say whether this will work with your Map structure or not, but to customize entity encoding, you'd use EntityEncoderFilter (as opposed to ValueEncoders). EntityEncoderFilters can also be registered either once for the entire Ag stack or per request.

Andrus
Reply all
Reply to author
Forward
0 new messages