SpiderBasic JSON UTF-8 encoding

Just starting out? Need help? Post your questions and find answers here.
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

SpiderBasic JSON UTF-8 encoding

Post 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.
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: SpiderBasic JSON UTF-8 encoding

Post by Fred »

Everything should be UTF-8 internally in SpiderBasic, if not please fill a bug report with a small snippet :)
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: SpiderBasic JSON UTF-8 encoding

Post 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.
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: SpiderBasic JSON UTF-8 encoding

Post 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).
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: SpiderBasic JSON UTF-8 encoding

Post 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.
Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: SpiderBasic JSON UTF-8 encoding

Post by Fred »

There is no HttpRequest() in PB, or I missed something ?
the.weavster
Posts: 220
Joined: Sat Mar 01, 2014 3:02 pm

Re: SpiderBasic JSON UTF-8 encoding

Post by the.weavster »

Fred wrote:There is no HttpRequest() in PB
Well... what else have you got to do today? ;)
Post Reply