jsonecode doesn't support complex doubles

조회 수: 33 (최근 30일)
Alexander DeWitt
Alexander DeWitt . 2022년 2월 22일
댓글: Alexander DeWitt . 2022년 2월 22일
After a few error messages about encoding objects of class double, I figuered out that my highlevel structs had a field that contianed complex doubles, and therefore couldn't be encoded. So, I was wondering:
Is there a particular reason that complex doubles aren't supported by jsonecode?
If the reason isn't a lack of general consenus on default encoding of complex doubles in JSON, what is the convention?

채택된 답변

Ive J
Ive J 2022년 2월 22일
Here says:
"JSON has no standard way to represent complex numbers, so there is no way to test for them in JSON Schema."
Here also, you'll find more:
You have two (maybe more?) options:
x.a = 1 + 2i;
% 1st
ans = '{"a":{"mwcomplex":true,"mwdata":[1,2],"mwsize":[1,1],"mwtype":"double"}}'
% 2nd
y = x;
y.a = string(y.a);
ans = '{"a":"1+2i"}'
% error
Error using jsonencode
Unable to encode objects of class double as JSON-formatted text.
  댓글 수: 1
Alexander DeWitt
Alexander DeWitt 2022년 2월 22일
Thanks for the complete answer. I was moving in that direction, but I'm glad I checked and got the full background information.

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

추가 답변 (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