当我使用 TJson.ObjectToJsonString 将对象序列化为 json 时,如何动态忽略属性
我有这个类:
unit untPerson;
interface
type TPerson = class
private
fName : string;
fEmail : string;
fAge : integer;
published
property Name : string read fName write fName;
property Email : string read fEmail write fEmail;
property Age : integer read fAge write fAge;
end;
implementation
end.
我需要使用此代码序列化为 Json
TJson.ObjectToJsonString(objPerson, []);
但如果等于 0,我需要跳过 Age。
if objPerson.Age = 0 then
result := '{"name":"Lucas", "email":"[email protected]"}'
else
result := '{"name":"Lucas", "email":"[email protected]", "age":30}';
我该怎么办?
if objPerson.Age = 0 then
result := '{"name":"Lucas", "email":"[email protected]"}'
else
result := '{"name":"Lucas", "email":"[email protected]", "age":30}';
I have this class:
unit untPerson;
interface
type TPerson = class
private
fName : string;
fEmail : string;
fAge : integer;
published
property Name : string read fName write fName;
property Email : string read fEmail write fEmail;
property Age : integer read fAge write fAge;
end;
implementation
end.
And i need to serialize to Json using this code
TJson.ObjectToJsonString(objPerson, []);
But i need to skip Age if equal 0.
if objPerson.Age = 0 then
result := '{"name":"Lucas", "email":"[email protected]"}'
else
result := '{"name":"Lucas", "email":"[email protected]", "age":30}';
How Can I Do?
if objPerson.Age = 0 then
result := '{"name":"Lucas", "email":"[email protected]"}'
else
result := '{"name":"Lucas", "email":"[email protected]", "age":30}';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法使用以下代码动态忽略属性:
TJson.ObjectToJsonString(objPerson, []);
。您可以通过自定义
TJSONInterceptor
放置空字符串,就像 Remy Lebeau 在评论中所说的那样(https://en.delphipraxis.net/topic/6155-tjson-suppressemptyvalues-for-empty-integer-double-and-class-fields/),但要将其从 json 中排除,您需要输入选项 joIgnoreEmptyStrings :TJson.ObjectToJsonString(objPerson, [joIgnoreEmptyStrings]);。另一种方法是:使用自定义转换器(
TJsonConverter
的后代),但问题是在这种情况下您不能使用TJson.ObjectToJsonString
,因为它已预编译专门使用TJSONConverter
类创建JSONMarshal
的代码,无法覆盖:TJSONMarshal.Create(TJSONConverter.Create,真,CFRegConverters)
;。因此,您需要重新实现所有调用链TJSON.ObjectToJsonString -> ObjectToJsonValue -> TJSONConverters.GetJSONMarshaler -> TJSONMarshal.Create(TJSONConverter.Create, true, CFRegConverters)
;。之后您必须使用这个自定义实现。最简单的方法 - 将自定义方法直接添加到类中:
在这种情况下 - 您可以使用任何自定义逻辑,但相同的代码:
TJson.ObjectToJsonString(objPerson, [])
;.will 无法正常工作。您必须使用这些新方法。最后 – 您可以尝试找到一些其他 3rd 方 JSON 序列化器。
You can't dynamically ignore properties by using code:
TJson.ObjectToJsonString(objPerson, []);
.You can put empty string by custom
TJSONInterceptor
, like Remy Lebeau says in comments(https://en.delphipraxis.net/topic/6155-tjson-suppressemptyvalues-for-empty-integer-double-and-class-fields/), but to exclude it from json you need to put optionjoIgnoreEmptyStrings
:TJson.ObjectToJsonString(objPerson, [joIgnoreEmptyStrings]);
.One more way to do it: use custom converter (descendant of
TJsonConverter
), but problem is that you can’t useTJson.ObjectToJsonString
in this case, because it have precompiled code with creatingJSONMarshal
using specificallyTJSONConverter
class without possibility of overriding:TJSONMarshal.Create(TJSONConverter.Create, true, CFRegConverters)
;. So you need to reimplement all chain of callTJSON.ObjectToJsonString -> ObjectToJsonValue -> TJSONConverters.GetJSONMarshaler -> TJSONMarshal.Create(TJSONConverter.Create, true, CFRegConverters)
;. And after that you must use this custom implementation.Easiest way – to add custom method directly to class:
In this case – you can use any custom logic, but the same code:
TJson.ObjectToJsonString(objPerson, [])
;.will does not work correctly. You must use these new methods.And last – you can try to find some other 3rd party JSON serializers.
作为替代解决方案, mORMot 有这个菱形,并且您始终可以使用 ObjectToJson 以集中方式非常快速地序列化任何 TObject:
请在 令人惊叹的文档
As an alternative solution, mORMot have this diamond, and you can always use ObjectToJson to serialize very fast any TObject in a centralized way:
Please, find further details in the amazing documentation