33import com .bookmap .addons .broadcasting .api .view .listeners .LiveEventListener ;
44import com .bookmap .api .rpc .server .EventLoop ;
55import com .bookmap .api .rpc .server .data .outcome .BroadcastingEvent ;
6- import com .google .gson .JsonObject ;
7-
8- import java .lang .reflect .Field ;
9- import java .util .*;
10- import java .util .concurrent .ConcurrentHashMap ;
6+ import com .bookmap .api .rpc .server .utils .JsonUtil ;
117
128/**
139 * The listener that is passed to BrAPI.
@@ -17,7 +13,6 @@ public class EventListener implements LiveEventListener {
1713
1814 private final EventLoop eventLoop ;
1915 private final String generatorName ;
20- private final Map <Class <?>, Field []> classToFields = new ConcurrentHashMap <>();
2116 private final FilterListener filterListener ;
2217
2318 public EventListener (EventLoop eventLoop , String generatorName , FilterListener filterListener ) {
@@ -34,43 +29,12 @@ public void giveEvent(Object o) {
3429 }
3530 String event ;
3631 try {
37- event = convertEventToJSON (o );
32+ event = JsonUtil . convertObjectToJsonString (o );
3833 }
3934 catch (IllegalAccessException e ) {
4035 throw new RuntimeException ("Error during parsing event object" , e );
4136 }
4237 eventLoop .pushEvent (new BroadcastingEvent (generatorName , event ));
4338 }
4439 }
45-
46- private String convertEventToJSON (Object o ) throws IllegalAccessException {
47- JsonObject jsonObject = new JsonObject ();
48- Class <?> clazz = o .getClass ();
49- if (classToFields .containsKey (clazz )) {
50- Field [] fields = classToFields .get (clazz );
51- for (Field field : fields ) {
52- field .setAccessible (true );
53- String fieldName = field .getName ();
54- Object fieldValue = field .get (o );
55- jsonObject .addProperty (fieldName , fieldValue .toString ());
56- }
57- } else {
58- Field [] fields = clazz .getDeclaredFields ();
59- List <Field > fieldNames = new ArrayList <>();
60- for (Field field : fields ) {
61- field .setAccessible (true );
62-
63- String fieldName = field .getName ();
64- if (fieldName .equals ("serialVersionUID" )) {
65- continue ;
66- }
67- fieldNames .add (field );
68- Object fieldValue = field .get (o );
69- jsonObject .addProperty (fieldName , fieldValue .toString ());
70- }
71- classToFields .put (clazz , fieldNames .toArray (new Field [0 ]));
72- }
73-
74- return jsonObject .toString ();
75- }
7640}
0 commit comments