Why does jsonencode parser does not remove exponential notations when format longG is used?

조회 수: 26 (최근 30일)
I would like to save a structured data in a JSON file. There are various structured array elements that has integer number that needs to be represented without any exponents in the notation.
The following is an example commands that formats correctly as per my requirement on command window. But formats with exponent notation after saving to a json file.
Example:
% Command Window output
>> clear all
>> format long g
>> university.department.EEE.studentID = {122323259857567}
university =
struct with fields:
department: [1x1 struct]
>> university.department.EEE.studentID
ans =
1x1 cell array
{[122323259857567]}
% When I write it to JSON File the cell array now saves the above studentID with exponents
>>jsonData = jsonencode(university);
saveFileName = [filePath, '/', 'student1.json'];
fileId = fopen(saveFileName, 'w');
if fileId== -1, error('Cannot create JSON file');end
fwrite(fileId, jsonData, 'char');
fclose(fileId);
% student1.json now has :
% {
% "department":
% "EEE": {
% studentID: {[1.223232598575670e+14]}
% studentName: "XYZ YUI"
%
% }
% "ECE":{
% studentID: {[1.223232598575670e+14]}
% studentName: "sdf YUI"
%
% }
% "CS":{
% studentID: {[1.223232598575670e+14]}
% studentName: "ABC YUI"
% }
% }
%
%

채택된 답변

Guillaume
Guillaume 2019년 9월 17일
편집: Guillaume 2019년 9월 17일
The straightforward answer to your question is simple. format has nothing to do with jsonencode, it only affects display at the matlab command prompt. I'm not sure why you thought it would affect jsonencode. Nowhere does it say that.
As for your problem, the json specs does not specify how numbers should be encoded as text in the json, so matlab is perfectly within its right to encode the way it does. Note that 122323259857567 and 1.223232598575670e+14 are exactly the same number, so a conformant decoder that can decode numbers of that magnitude (not guaranteed but very likely) will decode them properly.
Now, there is a way to force matlab to write integers less than 18446744073709551615 without using scientific notation by converting them to uint64, but again, there is nothing wrong with the way matlab currently encodes the numbers.
>> university.department.EEE.studentID = uint64(122323259857567); %not sure why you were using a cell array
>> jsonencode(university)
ans =
'{"department":{"EEE":{"studentID":122323259857567}}}'
  댓글 수: 2
Kiran Prakash
Kiran Prakash 2019년 9월 17일
uint64() was exactly what I was looking for.
Thanks for taking time to explain. That was helpful. By the way, I am using it in a cell array as because I have multiple IDs associated to the same student and I am storing them as array of ints.
I would be interested to know how a conformant decoder would read it. I will update here if it doesn't read correctly here.
Thanks again.
Liam Healy
Liam Healy 2020년 8월 16일
I have the same problem; the receiving API will not accept "e" in numbers. I understand this is legitimate JSON encoding, so there must be an error on the receiving API, but I have no influence on it and have to accept it as it is. For my case, it is a large float that is being written, uint64 works to a good enough approximation, but I could well encounter cases where it isn't, so I'd like to write floats without using "e".
So, I would like to make slight modification to OP's question. Is there a way to specify the format that jsonencode uses for numbers, specifically to never use scientific (exponential) notation?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 JSON Format에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by