1 package net.minidev.json.reader;
2
3 import java.io.IOException;
4
5 import net.minidev.asm.Accessor;
6 import net.minidev.asm.BeansAccess;
7 import net.minidev.json.JSONObject;
8 import net.minidev.json.JSONStyle;
9 import net.minidev.json.JSONUtil;
10
11 public class BeansWriterASM implements JsonWriterI<Object> {
12 public <E> void writeJSONString(E value, Appendable out, JSONStyle compression) throws IOException {
13 try {
14 Class<?> cls = value.getClass();
15 boolean needSep = false;
16 @SuppressWarnings("rawtypes")
17 BeansAccess fields = BeansAccess.get(cls, JSONUtil.JSON_SMART_FIELD_FILTER);
18 out.append('{');
19 for (Accessor field : fields.getAccessors()) {
20 @SuppressWarnings("unchecked")
21 Object v = fields.get(value, field.getIndex());
22 if (v == null && compression.ignoreNull())
23 continue;
24 if (needSep)
25 out.append(',');
26 else
27 needSep = true;
28 String key = field.getName();
29 JSONObject.writeJSONKV(key, v, out, compression);
30 }
31 out.append('}');
32 } catch (IOException e) {
33 throw e;
34 }
35 }
36 }
37