Recent

Author Topic: Convert JSON WideString to TJSONStringType  (Read 663 times)

PizzaProgram

  • Jr. Member
  • **
  • Posts: 53
  • ...developing Delphi apps since 25 years.
Convert JSON WideString to TJSONStringType
« on: May 12, 2022, 09:39:55 pm »
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  [Select][+][-]
  1. const
  2.   c_json : WideString =
  3.   '{'#13#10+
  4.     '"someData": {'#13#10+
  5. ...
  6. '"name": "Tétel 1",'#13#10+
  7. '}';
  8. ...
  9.  
  10. function TMyClaims.GetAsString: TJSONStringType;
  11. var w : WideString;
  12. begin
  13.     w := c_json ; // normally it's WideString variable, this is only for testing
  14.     Result := UTF8Encode(w);
  15.     //ShowMessage( Result );
  16. end;

Result gives back strange chars: "Tétel.." becomes "Tétel.."


I need this, because GetAsString is called by :
Code: Pascal  [Select][+][-]
  1. function TJWTSigner.GetSignInputString(aJWT: TJWT): UTF8String;
  2. begin
  3.   Result:=aJWT.JOSE.AsEncodedString+'.'+aJWT.Claims.AsEncodedString
  4. end;  
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
x86_64-win64 --Win7 PRO 64bit HUN

balazsszekely

  • Guest
Re: Convert JSON WideString to TJSONStringType
« Reply #1 on: May 13, 2022, 06:50:26 am »
Hungarian is ISO_8859_1, please try this:
Code: Pascal  [Select][+][-]
  1. uses LazUTF8, LConvEncoding;  
  2.  
  3. function GetAsString: TJSONStringType;
  4. var
  5.   w : WideString;
  6.   UTF8: UTF8String;
  7. begin
  8.     w := c_json ; // normally it's WideString variable, this is only for testing
  9.     UTF8 := UTF16ToUTF8(w);
  10.     Result := UTF8ToISO_8859_1(UTF8);
  11. end;  

PascalDragon

  • Hero Member
  • *****
  • Posts: 5469
  • Compiler Developer
Re: Convert JSON WideString to TJSONStringType
« Reply #2 on: May 13, 2022, 10:25:45 am »
Code: Pascal  [Select][+][-]
  1. const
  2.   c_json : WideString =
  3.   '{'#13#10+
  4.     '"someData": {'#13#10+
  5. ...
  6. '"name": "Tétel 1",'#13#10+
  7. '}';
  8. ...
  9.  
  10. function TMyClaims.GetAsString: TJSONStringType;
  11. var w : WideString;
  12. begin
  13.     w := c_json ; // normally it's WideString variable, this is only for testing
  14.     Result := UTF8Encode(w);
  15.     //ShowMessage( Result );
  16. end;

Result gives back strange chars: "Tétel.." becomes "Tétel.."

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?

 

TinyPortal © 2005-2018