jsonecode doesn't support complex doubles

16 visualizaciones (últimos 30 días)
Alexander DeWitt
Alexander DeWitt el 22 de Feb. de 2022
Comentada: Alexander DeWitt el 22 de Feb. de 2022
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?
Thanks,

Respuesta aceptada

Ive J
Ive J el 22 de Feb. de 2022
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
mps.json.encode(x)
ans = '{"a":{"mwcomplex":true,"mwdata":[1,2],"mwsize":[1,1],"mwtype":"double"}}'
% 2nd
y = x;
y.a = string(y.a);
jsonencode(y)
ans = '{"a":"1+2i"}'
% error
jsonencode(x)
Error using jsonencode
Unable to encode objects of class double as JSON-formatted text.
  1 comentario
Alexander DeWitt
Alexander DeWitt el 22 de Feb. de 2022
Thanks for the complete answer. I was moving in that direction, but I'm glad I checked and got the full background information.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by