RTC Forums
November 28, 2024, 07:12:38 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: RTC JSON in the browser  (Read 4502 times)
Dany
RTC License++
*****
Posts: 69


« on: October 18, 2013, 04:39:02 PM »

Hello!

I use...

Code:
          DelphiDataSetToRtc(myDataSet, lRtcDataSet);
          Server.Write(lRtcDataSet.toJSON);

...a very nice setup.

Then at the web browser (the client in this case) i access the /dsfields and if i find the type "WM" i use javascripts atob function to decode from base64. I can then use the strings in the page, for example by assigning object.innerHtml. This works like a charm.

The problem is when i need to produce a tag in my JS code. Like so:

Code:
  innerHTML = '<a href="#" whatever="' + decodedMemoStringAsAbove + '">' + decodedMemoStringAsAbove + '</a>';

when i inspect the resulting element in the browser the string inside "" after whatever has charcode zero at every other character (the link text itself displays properly).

If myDataset's field property IsBlob is false i get a sting that can be used in both ways.

I know that the DelphiDataSetToRtc is just an example and i could change that to only use base64 (NewByteStream) for fields that are truly binary. However, this would entail digging into the implementation of toJSON too and i rather do not.

Q: So what have i done wrong? The page is utf-8 and i'm using Delphi XE2 on the backend side. I suspect this could be something internal to the browser but i'd like to understand why this happens before i write a JS snippet to remove the zeroes which would be unorthodox i think. Furthermore the servers functions (returning JSON) could be used as an API in the future and in that case any "special" stuff should be rooted out.

Thank you so much in advance,

/Dany

I can also add that i've been googling frantically and only found one other post indicating a similar problem but without any good answers.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: October 18, 2013, 05:59:59 PM »

When you encode a normal String in JSON, all Unicode characters will be specially encoded and you will get the same String on both ends. But if you send a BLOB, then raw content of that BLOB will be stored using base64 encoding. And if that BLOB contained a WIDE MEMO with Unicode characters ("WM" type), then the content will be stored using UTF-16 encoding, which means that each character will take exactly 2 bytes. That is where your zeroes after each character come from.

This 2-byte character storage is NOT done by the RTC SDK, though. The RTC SDK simply takes what is inside the BLOB stream and encodes it using base64 encoding.

To solve your problem, you can either change your JavaScript code to handle base64 encoded "WM" fields as UTF-16, or write your own function based on the "DelphiDataSetToRtc" function (copy it to your own unit and give it a new name) and make your implementation use "asText" instead of "asBytestream" for fields of type ftWideMemo, then change your JavaScript code to read "WM" field as plain UTF-8 Strings instead of base64 encoded UTF-16 Strings.

Best Regards,
Danijel Tkalcec
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 17 queries.