GWT 2.1.1 has very good framework - RequestFactory with all the EntityProxy and stuff.
I am looking for a way to serialize runtime instances that implement EntityProxy for debugging and logging etc. I do not care for format as long as it human readable. To be more specific I would like to have something like the provided by Apache Commons Lang ReflectionToStringBuilder May be there is some way to use the JSON serialization mechanics that GWT has inside? if yes how to make it a bit more readable?
im开发者_Go百科port org.apache.commons.lang.builder.ReflectionToStringBuilder;
String stringRep = ReflectionToStringBuilder.toString(this);
There are at least 2 solutions:
First: Based on the idea by Thomas Broyer
public static String toString(EntityProxy entityProxy)
{
DefaultProxyStore store = new DefaultProxyStore();
Swap.requestFactory.getSerializer(store).serialize(entityProxy);
return store.encode();
}
Which produce something like this:
{"V":"211","P":{"1@2@biz.daich.swap.shared.dto.UserAccountProxy":{"O":"PERSIST","R":"2","Y":1,"T":"biz.daich.swap.shared.dto.UserAccountProxy","P":{"id":null,"items":null,"channelId":null,"lastActive":1296194777916,"name":null,"emailAddress":"test@example.com","lastReported":1296194777916,"lastLoginOn":1296194777916}}}}
Second: Based on the AutoBean framework
public static String toJson(EntityProxy entityProxy)
{
return AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(entityProxy)).getPayload();
}
Which produce string like
{"emailAddress":"test@example.com","lastActive":1296194777916,"lastLoginOn":1296194777916,"lastReported":1296194777916}
The second is just what I need - it more readable in log.
I haven't tried it but have a look at RequestFactory#getSerializer, there's some sample code in the javadoc for the ProxySerializer.
If using the method toJson(EntityProxy entityProxy)
change this to
toJson(BaseProxy proxy)
and then you can log Value and Entity Proxy objects.
精彩评论