The PHP json_decode() command expects a UTF-8 encoded string, how do I create a JSON object encoded as a UTF-8 string to pass to HTTPRequest() in a way that works with both SpiderBasic and PureBasic?
Thanks.
SpiderBasic JSON UTF-8 encoding
Re: SpiderBasic JSON UTF-8 encoding
Everything should be UTF-8 internally in SpiderBasic, if not please fill a bug report with a small snippet
-
- Posts: 222
- Joined: Sat Mar 01, 2014 3:02 pm
Re: SpiderBasic JSON UTF-8 encoding
Thanks Fred,
It might be an idea to edit the ComposeJSON() command reference in the SpiderBasic help file as it's a little misleading at the moment.
Could we have a StringifyJSON(#JSON) command that returns a UTF-8 string in both SpiderBasic and PureBasic so we don't have to do any CompilerIf stuff ourselves?
UTF-8 seems to be the standard for the XMLHttpRequest.send() method so I guess it's what we should be using when we send data with the HTTPRequest() command.
It might be an idea to edit the ComposeJSON() command reference in the SpiderBasic help file as it's a little misleading at the moment.
Could we have a StringifyJSON(#JSON) command that returns a UTF-8 string in both SpiderBasic and PureBasic so we don't have to do any CompilerIf stuff ourselves?
UTF-8 seems to be the standard for the XMLHttpRequest.send() method so I guess it's what we should be using when we send data with the HTTPRequest() command.
Re: SpiderBasic JSON UTF-8 encoding
I don't think we can, as in PureBasic UTF-8 isn't a string format, so it would have to be a memory buffer. ComposeJSON() returns a string in both PB an SB and if you want to use it, you want to use it as UTF-8 in PB, you need a conversion command. We plan to add command like ToUTF8() or somethinng similar to ease the thing. In SB, it's already UTF-8 for now, but it's internal implementation (that said, I don't think it will change).
-
- Posts: 222
- Joined: Sat Mar 01, 2014 3:02 pm
Re: SpiderBasic JSON UTF-8 encoding
Would this work?:
If not how do you pass a UTF-8 string to HTTPRequest() in PureBasic?
Thanks.
Code: Select all
Procedure.s StringifyJSON(json)
CompilerIf #PB_Compiler_OS = #PB_OS_Web
ProcedureReturn ComposeJSON(json)
CompilerElse
CompilerIf #PB_Compiler_Unicode = 1
nEncoding = #PB_Unicode
CompilerElse
nEncoding = #PB_Ascii
CompilerEndIf
txt.s = ComposeJSON(json)
*mem = AllocateMemory(StringByteLength(txt,#PB_UTF8)+1)
PokeS(*mem,txt,-1,#PB_UTF8)
out.s = PeekS(*mem,-1,nEncoding)
FreeMemory(*mem)
ProcedureReturn out.s
CompilerEndIf
EndProcedure
Thanks.
Re: SpiderBasic JSON UTF-8 encoding
There is no HttpRequest() in PB, or I missed something ?
-
- Posts: 222
- Joined: Sat Mar 01, 2014 3:02 pm
Re: SpiderBasic JSON UTF-8 encoding
Well... what else have you got to do today?Fred wrote:There is no HttpRequest() in PB