1

Is there a way to get CakePHP 2.8 JSON views to encode numbers as numbers instead of strings?

I would like to avoid creating /json/*.ctp view files with echo json_encode($data, JSON_NUMERIC_CHECK) if at all possible and have the standard _serialize method below encode numbers properly by default.

public function index(){
    $items = $this->Paginator->paginate();

     $this->set(array(
        'items' => $items,
        '_serialize' => array('items')
    ));
}
caitlin
  • 2,769
  • 4
  • 29
  • 65
  • Are the item fields actual numbers to begin with? Sometime when they come from the database you might actually get strings instead. – laurent Oct 04 '16 at 14:52
  • You might want to take a look at a similar question: http://stackoverflow.com/a/6608413/4073621. – Koen Oct 04 '16 at 14:53
  • Yes, the fields are numbers in the DB. I know about `JSON_NUMERIC_CHECK` but I was wondering if there was a way to turn that on without using it in a view file. – caitlin Oct 04 '16 at 14:54
  • can you please specify the output of $member_forms as JSON_NUMERIC_CHECK will convert ` '+33123456789'), JSON_NUMERIC_CHECK); ?>` to {"phone_number":33123456789} – Rohit Ailani Oct 04 '16 at 15:42
  • The numbers in this case are of type `int`. – caitlin Oct 04 '16 at 16:46

0 Answers0