SuperObject 无法处理空字符串 [英] SuperObject cannot handle null string

查看:39
本文介绍了SuperObject 无法处理空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

某些 JSON 序列化程序为空字符串数据字段返回 null,例如

<代码>{搜索文本":空,"moretext": "包含一些东西",预订日期":1377468000000,empid":12345,列表类型":1}

我正在使用 SuperObject 创建一个 ISuperObject:

varFJSONRequest:ISuperObject;然后FJSONRequest := SO(Request.Content);//网络服务请求

这将返回一个带有包含文本 'null' 的字符串的对象.

显然这是因为 SuperObject 不关心引号("searchtext": a 给出与 "searchtext": "a" 相同的结果).

在我深入研究 980 行标记器例程之前,有人有解决方案吗?

我正在考虑(或/或):

  • 将空数据字段从 JSON 对象中删除

  • 返回一个空字符串

如果一切都失败了,我仍然可以做到

FJSONRequest := SO(StringReplace(Request.Content,': null,',':,',[rfReplaceAll]));

因为我只需要处理来自我们的一个开发人员的应用程序的请求,但这并不是万无一失的.
(不,他不能抑制 null 因为 Mono 处理他的数据合同的方式存在错误.)

顺便说一句,我正在体验此处提到的行为,但在 SuperObject 代码的另一部分中,因此该解决方法不起作用.

解决方案

下载部分的官方 1.2.4 ZIP 文件 http://code.google.com/p/superobject/downloads/list 日期为 2010 年,但 http://code.google.com/p/superobject/source/browse 更新至 2012 年 10 月.

如果您转到最后一个 URL 并点击 Download zip,您可以检索它们.

这些更新的文件让您可以使用特殊情况null.

如果省略字符串值周围的引号,代码仍然是宽容的":

<代码>{预订日期":1377554400000,搜索文本":一个,列表类型":1}

但它现在处理特殊情况

<代码>{预订日期":1377554400000,搜索文本":空,列表类型":1}

好像是

<代码>{预订日期":1377554400000,搜索文本":,列表类型":1}

<代码>{预订日期":1377554400000,列表类型":1}

[不要不小心输入nilNULL]

此版本支持到 VER230 (Delphi XE2) [请注意,'官方' 1.2.4 甚至无法在更新的 Delphi 版本上编译],因此对于更高版本的 Delphi,您必须修补编译器指令.

它还修复了以下问题:

  • 当浮点值碰巧有一个精确的整数值时,JSON 会有一个尾随句点:

    { "floatingpointvalue": 4. }

    现在已修复:

    { "floatingpointvalue": 4 }

  • 在闰年切换到夏令时前后发生的日期时间转换错误 - 是的,晦涩难懂.
    {$IFDEF WINDOWSNT_COMPATIBILITY}

  • 包围的代码部分出现错误

请注意,这仍然是默认定义的,我建议您禁用定义,例如使用 {.$IFDEF WINDOWSNT_COMPATIBILITY} [现在谁需要在 Windows NT 上运行?],它让操作系统处理日期时间转换:

{$ELSE}函数 TzSpecificLocalTimeToSystemTime(lpTimeZoneInformation:PTimeZoneInformation;lpLocalTime, lpUniversalTime: PSystemTime): BOOL;标准调用;外部'kernel32.dll';函数 SystemTimeToTzSpecificLocalTime(lpTimeZoneInformation:PTimeZoneInformation;lpUniversalTime, lpLocalTime: PSystemTime): BOOL;标准调用;外部'kernel32.dll';{$ENDIF}

Some JSON serializers return null for an empty string datafield, e.g.

{
 "searchtext": null,
 "moretext": "contains something",
 "bookdate": 1377468000000,
 "empid": 12345,
 "listtype": 1
}

I'm using SuperObject to create a ISuperObject:

var
  FJSONRequest: ISuperObject; 
then
  FJSONRequest := SO(Request.Content);   // Webservice request

This returns an object with a string containing the text 'null'.

Obviously this is because SuperObject does not care about the quotes ("searchtext": a gives the same results as "searchtext": "a").

Before I dive into the 980-line tokenizer routine, does any one have a solution?

I'm thinking along the lines (either/or):

  • leave the null datafield out of the JSON object

  • return an empty string

If all else fails I could still do

FJSONRequest := SO(StringReplace(Request.Content,': null,',':,',[rfReplaceAll]));  

because I only need to handle requests coming from an app from one of our developers, but that's not foolproof.
(No, he cannot suppress the null because there's a bug in the way Mono handles his datacontract.)

BTW I'm experiencing exactly the behaviour mentioned here, but in another part of the SuperObject code, so that workaround does not do the job.

解决方案

The official 1.2.4 ZIP file in the download section http://code.google.com/p/superobject/downloads/list dates from 2010, but the individual files in http://code.google.com/p/superobject/source/browse have updates up until Oct 2012.

If you go to that last URL and click on Download zip you can retrieve them.

These updated files let you use the special case null.

The code is still 'forgiving' if you omit the quotes around the string value:

{
 "bookdate": 1377554400000,
 "searchtext": a,
 "listtype": 1
}

but it now handles the special case

{
 "bookdate": 1377554400000,
 "searchtext": null,
 "listtype": 1
}

as if it was

{
 "bookdate": 1377554400000,
 "searchtext": ,
 "listtype": 1
}

or

{
 "bookdate": 1377554400000,
 "listtype": 1
}

[Do not accidentally type nil or NULL]

This release supports up until VER230 (Delphi XE2) [Note that the 'official' 1.2.4 did not even compile on more recent Delphi versions], so for later versions of Delphi you'll have to patch the compiler directives.

It also fixes the following:

  • When an floating point value happened to have an exact integer value, the JSON would have a trailing period:

    { "floatingpointvalue": 4. }

    This is now fixed:

    { "floatingpointvalue": 4 }

  • Errors in datetime conversions happening around the switch to daylight savings time in leapyears - yes, obscure.
    There was an error in the code section surrounded by {$IFDEF WINDOWSNT_COMPATIBILITY}

Note that this is still defined by default, I suggest you disable the define , e.g. with {.$IFDEF WINDOWSNT_COMPATIBILITY} [who needs to run on Windows NT nowadays?], which lets the OS handle datetime conversions:

{$ELSE}
function TzSpecificLocalTimeToSystemTime(
  lpTimeZoneInformation: PTimeZoneInformation;
  lpLocalTime, lpUniversalTime: PSystemTime): BOOL; stdcall; external 'kernel32.dll';

function SystemTimeToTzSpecificLocalTime(
  lpTimeZoneInformation: PTimeZoneInformation;
  lpUniversalTime, lpLocalTime: PSystemTime): BOOL; stdcall; external 'kernel32.dll';
{$ENDIF}

这篇关于SuperObject 无法处理空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆