Yii Framework Forum: Rest API: return Strings only - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rest API: return Strings only

#1 User is offline   Stefano Mtangoo 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 349
  • Joined: 02-January 12
  • Location:Dar es Salaam, Tanzania

Posted 26 April 2017 - 02:13 AM

I have a problem with REST API. The return value consists of Integers and Numbers. I know that is common sense but I want all my fields in JSON to be strings not their natural value. Is there a setting to disable converting values to their natural type and remain strings only?

TIA
And God said, Let there be science...and it was so! Forum coded in Yii2 here, and its github repo here
0

#2 User is offline   Patrick Jones 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 495
  • Joined: 14-July 12
  • Location:Berlin

Posted 26 April 2017 - 04:51 AM

See "Controlling JSON output" here:
http://www.yiiframew...ata-serializing

JSON_NUMERIC_CHECK might help:
http://php.net/manua...n.constants.php
Freelance developer for hire - send me a message!
0

#3 User is offline   Stefano Mtangoo 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 349
  • Joined: 02-January 12
  • Location:Dar es Salaam, Tanzania

Posted 26 April 2017 - 06:48 AM

View PostPatrick Jones, on 26 April 2017 - 04:51 AM, said:

See "Controlling JSON output" here:
http://www.yiiframew...ata-serializing

JSON_NUMERIC_CHECK might help:
http://php.net/manua...n.constants.php


Thanks a lot. I use AR for this specific case and docs there says

Quote

When returning data from a database using the DAO database layer all data will be represented as strings, which is not always the expected result especially numeric values should be represented as numbers in JSON. When using the ActiveRecord layer for retrieving data from the database, the values for numeric columns will be converted to integers when data is fetched from the database in yii\db\ActiveRecord::populateRecord().


I hope that means it automagically adds the JSON_NUMERIC_CHECK to encode options (though not explicitly said I think it is implied). So will overriding the encodeOptions help in this case? I mean something like this, will it work?

'response' => [
	'formatters' => [
    	\yii\web\Response::FORMAT_JSON => [
        	'class' => 'yii\web\JsonResponseFormatter', 
        	'encodeOptions' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, //No JSON_NUMERIC_CHECK here
    	],
	],
],

And God said, Let there be science...and it was so! Forum coded in Yii2 here, and its github repo here
0

#4 User is offline   Patrick Jones 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 495
  • Joined: 14-July 12
  • Location:Berlin

Posted 26 April 2017 - 08:53 AM

View PostStefano Mtangoo, on 26 April 2017 - 06:48 AM, said:

So will overriding the encodeOptions help in this case? I mean something like this, will it work?


Don't know, but sounds worth a try ;-)
Freelance developer for hire - send me a message!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users