Page 1 of 1
SpiderBasic JSON UTF-8 encoding
Posted: Fri Mar 20, 2015 8:48 pm
by the.weavster
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.
Re: SpiderBasic JSON UTF-8 encoding
Posted: Tue Mar 24, 2015 8:24 am
by Fred
Everything should be UTF-8 internally in SpiderBasic, if not please fill a bug report with a small snippet

Re: SpiderBasic JSON UTF-8 encoding
Posted: Tue Mar 24, 2015 11:48 am
by the.weavster
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.
Re: SpiderBasic JSON UTF-8 encoding
Posted: Tue Mar 24, 2015 12:12 pm
by Fred
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).
Re: SpiderBasic JSON UTF-8 encoding
Posted: Tue Mar 24, 2015 2:53 pm
by the.weavster
Would this work?:
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
If not how do you pass a UTF-8 string to HTTPRequest() in PureBasic?
Thanks.
Re: SpiderBasic JSON UTF-8 encoding
Posted: Tue Mar 24, 2015 2:58 pm
by Fred
There is no HttpRequest() in PB, or I missed something ?
Re: SpiderBasic JSON UTF-8 encoding
Posted: Tue Mar 24, 2015 4:16 pm
by the.weavster
Fred wrote:There is no HttpRequest() in PB
Well... what else have you got to do today?
