Jump to content
Sign in to follow this  
DavidOne

Encoding accented char in JSON format

Recommended Posts

Hello,

I am working with Delphi 11.1.

Inspired by a Stephen Ball article I am using a TJsonTextWriter to create a RADServer endpoint response the contains data from a master-detail data relationship.

To write field value in the Json text response I am using the following code:

 

 if ((ADataSet.Fields[FieldIdx] is TDateField) or (ADataSet.Fields[FieldIdx] is TSQLTimeStampField)) then
      Writer.WriteValue(ADataSet.Fields[FieldIdx].AsDateTime)
    else
      Writer.WriteValue(ADataSet.Fields[FieldIdx].AsString);

 

The problem I am facing off is about accented characters like: à.

When I retrieve data from the endpoint I receive the following error:

 

Project Test.exe raised exception class EEncodingError with message 'No mapping for the Unicode character exists in the target multi-byte code page'.

 

If the there are no accented chars in the field content I can read the resulted Json without any problems.

 

How should I managed to encode correctly accented chars?

 

Thank you,

Davide

Share this post


Link to post
1 hour ago, Der schöne Günther said:

As far as I understand, not the DB layer (TFields and stuff) is throwing the exception, but your Writer is, correct?

 

If that's the case, have you seen its StringEscapeHandling-property?

Thank you,

setting the StringEscapeHandling to EscapeNonAscii does the magic!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×