我使用 JSONKit 来连接一个ASP之间code /德code JSON .NET RESTful服务。
I am using JSONKit to encode/decode JSON between an ASP.NET RESTful service.
由服务使用的日期格式谈到的此处,看起来像:
The date format used by the service is talked about here and looks like:
"\/Date(1198908717056)\/"问题是,当JSONKit处理一个字符串,它看起来像这样最终的结果看起来是这样上面,它避开反斜杠:
The problem is that when JSONKit processes a string that looks like above it escapes the backslash so the end result looks like this:
"\\/Date(1198908717056)\\/"该JSON规范说,你可以选择逃避正斜杠(/),所以应该JSONKit间preT在\\ /,而不要逃避反斜杠。
有谁知道从何时其次是像上面的ASP.NET JSON日期格式的情况下,正斜杠转义反斜线办法prevent JSONKit?
Does anyone know of a way to prevent JSONKit from escaping the backslash when it is followed by a forward slash like the case above for ASP.NET JSON date formats?
推荐答案编辑:忘记了previous答案。正如约翰所提到的,它可能是不正确的,有副作用。约翰的致力于一个变化实现所谓的选项 JKSerializeOptionEscapeForwardSlashes 这应该解决您的问题。
Forget the previous answer. As John mentioned, it’s probably incorrect and have side effects. John’s committed a change that implements an option called JKSerializeOptionEscapeForwardSlashes which should solve your problem.
尽管在JSONKit解析器似乎处理 \\ / ,它看起来像发电机没有。在 jk_en code_add_atom_to_buffer():
Even though the parser in JSONKit seems to handle \/, it looks like the generator doesn’t. In jk_encode_add_atom_to_buffer():
if(JK_EXPECT_F(utf8String[utf8Idx] >= 0x80U)) { encodeState->atIndex = startingAtIndex; goto slowUTF8Path; }这是一个非ASCII字符,进入 slowUTF8Path 。
if(JK_EXPECT_F(utf8String[utf8Idx] < 0x20U))这是一个控制字符(如 \\ n 或 \\ t ),逃吧。
if(JK_EXPECT_F(utf8String[utf8Idx] == '\"') || JK_EXPECT_F(utf8String[utf8Idx] == '\\')) { encodeState->stringBuffer.bytes.ptr[encodeState->atIndex++] = '\\'; }这是一个双引号或反斜杠,逃避它 - 而这就是错误,因为它没有考虑到 \\ /
我已经修补JSONKit.m,使其执行以下操作:
I’ve patched JSONKit.m so that it does the following:
if(JK_EXPECT_F(utf8String[utf8Idx]) == '\\' && JK_EXPECT_F(utf8String[utf8Idx+1]) == '/') { encodeState->stringBuffer.bytes.ptr[encodeState->atIndex++] = '\\'; encodeState->stringBuffer.bytes.ptr[encodeState->atIndex++] = '/'; utf8Idx++; } else if(JK_EXPECT_F(utf8String[utf8Idx] == '\"') || JK_EXPECT_F(utf8String[utf8Idx] == '\\')) { encodeState->stringBuffer.bytes.ptr[encodeState->atIndex++] = '\\'; } else encodeState->stringBuffer.bytes.ptr[encodeState->atIndex++] = utf8String[utf8Idx];和我的测试程序正确生成的JSON片段为您的字符串:
and my test program correctly generates the JSON fragment for your string:
NSString *test = @"\\/Date(1198908717056)\\/"; NSLog(@"%@", [test JSONString]);输出:
"\/Date(1198908717056)\/"如果没有我的补丁,该程序的输出:
Without my patch, the program outputs:
"\\/Date(1198908717056)\\/"不过,我建议你文件,JSONKit 的bug报告。约翰肯定是解决这个问题的最佳人选,而且JSONKit是过于优化了我在这个补丁的信心;我不是在所有熟悉JSONKit。随意给他参考这个帖子。
That said, I recommend you file a bug report with JSONKit. John is certainly the best person to fix this and JSONKit is far too optimised for me to have confidence in this patch; I’m not familiar with JSONKit at all. Feel free to refer him to this post.
更多推荐
如何从ASP.NET JSON日期格式转义反斜线prevent JSONKit?
发布评论