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

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

问题描述

一些JSON序列化程序返回一个空字符串数据字段的 null

  {
searchtext:null,
moretext:包含东西,
bookdate:1377468000000,
empid:12345,
listtype:1
}

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

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

返回一个包含文本的字符串的对象, null'



显然这是因为SuperObject不关心引号(searchtext:a searchtext:a相同的结果。



在我潜水之前进入980行标记器例程,有没有任何一个解决方案?



我正在考虑(或者/):




  • 将空数据域保留在JSON对象之外


  • 返回一个空字符串




如果所有其他失败我仍然可以做

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

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



BTW我正在经历这里提到的行为,但在SuperObject代码的另一部分,所以解决方法不做

解决方案

下载部分的官方1.2.4 ZIP文件 http://code.google.com/p/superobject/downloads/list 日期,但 http://code.google.com/p/superobject/source/br owse 有更新,直到2012年10月。



如果你去最后一个URL,点击下载zip 您可以检索它们。



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



如果您省略了字符串值的引号,代码仍然是宽恕:

  {
bookdate:1377554400000,
searchtext:a,
listtype:1
}
pre>

但现在处理特殊情况

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

好像是

  {
bookdate:1377554400000,
searchtext:
listtype:1
}

  {
bookdate:1377554400000,
listtype:1
}

[不要不小心输入 nil NULL ]



此版本支持直到VER230(Delphi XE2)[注意,官方1.2.4甚至没有编译更新的Delphi版本],所以对于更高版本的Delphi,你必须修补编译器指令。



它还修复了以下内容:




  • 当浮点值恰好具有一个确切的整数值时,JSON将具有一个结尾期:



    {floatingpointvalue:4.}



    现在修正了:



    {floatingpointvalue :4}


  • 切换到日期时间转换中的日期时间转换中的错误在leapyears中的夏令时 - 是的,模糊。

    是由 {$ IFDEF WINDOWSNT_COMPATIBILITY}包围的代码段中的错误




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



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;标准外部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天全站免登陆