Skip to content
This repository was archived by the owner on Jul 29, 2026. It is now read-only.

JSONType_serializer

高铁 edited this page Jul 1, 2016 · 1 revision

在1.2.14版本之后,fastjson支持通过JSONType配置定制序列化的ObjectSerializer。使用如下

@JSONType(serializer=ModelSerializer.class)
publicstaticclassModel {
publicintid;
}
publicstaticclassModelSerializerimplementsObjectSerializer {
@Overridepublicvoidwrite(JSONSerializerserializer, Objectobject, ObjectfieldName, TypefieldType,
intfeatures) throwsIOException {
Modelmodel = (Model) object;
SerializeWriterout = serializer.getWriter();
out.writeFieldValue('{', "ID", model.id);
out.write('}');
}
}

使用

Modelmodel = newModel();
model.id = 1001;
Stringtext = JSON.toJSONString(model);
Assert.assertEquals("{\"ID\":1001}", text);

Clone this wiki locally