Forum > General

Convert JSON WideString to TJSONStringType

(1/1)

PizzaProgram:
I need to sign a JSON string with JOSE JWS (RSA) algorithm.
This will be a DLL, so to interchange data (with Delphi7) the only safe way is to call the functions with WideString variables.

This way:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  c_json : WideString =  '{'#13#10+    '"someData": {'#13#10+ ...'"name": "Tétel 1",'#13#10+'}'; ... function TMyClaims.GetAsString: TJSONStringType;var w : WideString;begin    w := c_json ; // normally it's WideString variable, this is only for testing    Result := UTF8Encode(w);    //ShowMessage( Result ); end;
Result gives back strange chars: "Tétel.." becomes "Tétel.."


I need this, because GetAsString is called by :

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TJWTSigner.GetSignInputString(aJWT: TJWT): UTF8String;begin  Result:=aJWT.JOSE.AsEncodedString+'.'+aJWT.Claims.AsEncodedStringend;  in the fpjwt.pp unit Line: 379.

Here is a full example I've changed to fit for "pure JSON", not as TClaim payload:
 https://gitlab.com/freepascal.org/fpc/source/-/blob/main/packages/fcl-web/examples/jwt/signrs256.lpr

balazsszekely:
Hungarian is ISO_8859_1, please try this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses LazUTF8, LConvEncoding;   function GetAsString: TJSONStringType;var  w : WideString;  UTF8: UTF8String;begin    w := c_json ; // normally it's WideString variable, this is only for testing    UTF8 := UTF16ToUTF8(w);    Result := UTF8ToISO_8859_1(UTF8);end;  

PascalDragon:

--- Quote from: PizzaProgram on May 12, 2022, 09:39:55 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  c_json : WideString =  '{'#13#10+    '"someData": {'#13#10+ ...'"name": "Tétel 1",'#13#10+'}'; ... function TMyClaims.GetAsString: TJSONStringType;var w : WideString;begin    w := c_json ; // normally it's WideString variable, this is only for testing    Result := UTF8Encode(w);    //ShowMessage( Result ); end;
Result gives back strange chars: "Tétel.." becomes "Tétel.."
--- End quote ---

Would you please try Result := TJSONStringType(w) instead of using UTF8Encode? (The compiler will do the conversion automatically)
How do you check the value of the result? Using the commented ShowMessage?

Navigation

[0] Message Index

Go to full version