필터 지우기
필터 지우기

Json enconding and decoding for Matlab classes

조회 수: 13 (최근 30일)
Adrián Lascurain
Adrián Lascurain 2023년 6월 7일
편집: Adrián Lascurain 2023년 6월 14일
Hello , I'm currently defining a new class and I would like to encrypt (using AES) my object and all the information it contains, for this I used the jsonencode Matlab instruction for creating a character vector that is able to be encrypted, this already worked but when I decrypt the object and try to reconstruct it as my original object using Jsondecode it becomes an structure and not the class type that was defined.
So I would like to know if is there someway (method or instruction) to decode this into my original class type or shall I reconstruct the object using the data contained in the structure?
Thanks in advanced.

채택된 답변

Lakshay Rose
Lakshay Rose 2023년 6월 14일
편집: Lakshay Rose 2023년 6월 14일
Hi Adrián Lascurain,
As per my understanding you are trying to encode an object using “jsonencode” and then decode it using “jsondecode” and are getting the return output as a structure.
“jsonencode” function in MATLAB comes with the limitation that when objects are encoded, they don’t preserve their original structure.
To solve this problem there is a workaround of creating your own function which converts your decoded structure back to the object of your desired class.
You can refer to the below documentation to learn more about usage and limitations of “jsonencode” and “jsondecode” functions in MATLAB –
  댓글 수: 1
Adrián Lascurain
Adrián Lascurain 2023년 6월 14일
편집: Adrián Lascurain 2023년 6월 14일
Thank you so much Rose, I'm gonna try creating this function and compare it with another that I already found, this a way to preserve the object original structure (methods and properties) and I will let the solution here in the case that someone want to implement something similar.
Matlab has 2 build in functions (undocumented) that can help with this problem
  • getByteStreamFromArray(object) - This convert all the object into an uint8 row vector (this can be encrypted easily)
  • getArrayFromByteStream(RowVector) - This converts the resulting uint8 row vector into the original object
I've made some tests and it seems to work but the resulting vector is larger than the one you get using jsonencode so in my case it increases time for encrypting the data.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by