0

All the data exported is formatted with a string. Enclosed with quotes.

I'd like excluded_id to be numeric. The current state is the following.

[
 {
  "pending_id": "1234",
  "excluded_id": "111",
}]

I'd like the format to be the following.

[
 {
  "pending_id": 1234,
  "excluded_id": 111,
}]

The JSON service is implemented as a REST plugin. The normalizer is the following.

use Drupal\serialization\Normalizer\NormalizerBase;

class CBIBondTableDataNormalizer extends NormalizerBase {
  protected $supportedInterfaceOrClass = 'Drupal\cbi_dms_data\data\model\CBIBondTableResultData';

  public function normalize($object, $format = NULL, array $context = []) {
   return $object->normalize();
  }
}

With the normalizer.

class MyTableResultData implements MyTableResultType {

  public $row = [];

  public function __construct() {}

  public function addSqlResult($object) {
    $result = new CBIBondTableResult($object);
    $this->row[] = $result;
  }

  public function normalize() {
    $output = [];
    foreach ($this->row as $currentRow) {
      $output[] = (object) $currentRow;
    }
    return $output;
  }
}

I've tried setting the type in MyTableResultData.

public int $PendingID;

This doesn't work as PHP 7 doesn't allow something to be an integer or null.

1 Answer 1

0

Converting the value to integer helps

$int_value = intval($value);
if (strval($int_value) == $value) {
  $value = $int_value;
}
$this->$key_camel = $value;

I wrote it this way as I was concerned with null. Doesn't seem to be a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.