本文整理汇总了Java中com.alibaba.fastjson.serializer.ObjectSerializer.write方法的典型用法代码示例。如果您正苦于以下问题:Java ObjectSerializer.write方法的具体用法?Java ObjectSerializer.write怎么用?Java ObjectSerializer.write使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.alibaba.fastjson.serializer.ObjectSerializer
的用法示例。
在下文中一共展示了ObjectSerializer.write方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test_objectWriteTest
import com.alibaba.fastjson.serializer.ObjectSerializer; //导入方法依赖的package包/类
public void test_objectWriteTest() throws Exception {
ObjectSerializer serializer = SerializeConfig.getGlobalInstance().getObjectWriter(Model.class);
JSONSerializer jsonSerializer = new JSONSerializer();
serializer.write(jsonSerializer, null, "a", Model.class, 0);
String text = jsonSerializer.out.toString();
assertEquals("null", text);
}
示例2: handle
import com.alibaba.fastjson.serializer.ObjectSerializer; //导入方法依赖的package包/类
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();
}
}
示例3: write
import com.alibaba.fastjson.serializer.ObjectSerializer; //导入方法依赖的package包/类
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);
}
}