com.alibaba.fastjson.serializer.SerialContext Java Examples
The following examples show how to use
com.alibaba.fastjson.serializer.SerialContext.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MyJavaBeanSerializer.java From dpCms with Apache License 2.0 | 5 votes |
@Override public boolean writeReference(JSONSerializer serializer, Object object, int fieldFeatures) { SerialContext context = serializer.getContext(); { if (context != null && SerializerFeature.isEnabled(context.getFeatures(), fieldFeatures, SerializerFeature.DisableCircularReferenceDetect)) { return false; } } if (!serializer.containsReference(object)) { return false; } SerialContext parentCtx = context; while ((parentCtx = parentCtx.getParent()) != null) { Object parentObj = parentCtx.getObject(); if (object.equals(parentObj)) { serializer.writeNull(); return true; } } return false; }
Example #2
Source File: MyListSerializer.java From dpCms with Apache License 2.0 | 5 votes |
private void handle(JSONSerializer serializer, ObjectSerializer itemSerializer, SerialContext context, Object object, Object fieldName, Type elementType, Object item) throws IOException { serializer.println(); if (item != null) { itemSerializer = serializer.getObjectWriter(item.getClass()); SerialContext itemContext = new SerialContext(context, object, fieldName, 0, 0); serializer.setContext(itemContext); itemSerializer.write(serializer, item, fieldName, elementType, 0); } else { serializer.getWriter().writeNull(); } }
Example #3
Source File: MyCollectionSerializer.java From dpCms with Apache License 2.0 | 4 votes |
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException { SerializeWriter out = serializer.getWriter(); if (object == null) { if (out.isEnabled(SerializerFeature.WriteNullListAsEmpty)) { out.write("[]"); } else { out.writeNull(); } return; } Type elementType = null; if (serializer.isEnabled(SerializerFeature.WriteClassName)) { if (fieldType instanceof ParameterizedType) { ParameterizedType param = (ParameterizedType) fieldType; elementType = param.getActualTypeArguments()[0]; } } Collection<?> collection = (Collection<?>) object; SerialContext context = serializer.getContext(); serializer.setContext(context, object, fieldName, 0); if (serializer.isEnabled(SerializerFeature.WriteClassName)) { if (HashSet.class == collection.getClass()) { out.append("Set"); } else if (TreeSet.class == collection.getClass()) { out.append("TreeSet"); } } try { int i = 0; out.append('['); for (Object item : collection) { if (i++ != 0) { out.append(','); } if (item == null) { out.writeNull(); continue; } Class<?> clazz = item.getClass(); if (clazz == Integer.class) { out.writeInt(((Integer) item).intValue()); continue; } if (clazz == Long.class) { out.writeLong(((Long) item).longValue()); if (out.isEnabled(SerializerFeature.WriteClassName)) { out.write('L'); } continue; } ObjectSerializer itemSerializer = serializer.getObjectWriter(clazz); itemSerializer.write(serializer, item, i - 1, elementType, 0); } out.append(']'); } finally { serializer.setContext(context); } }