json encode/decode

8 Ansichten (letzte 30 Tage)
Jacob Sonne
Jacob Sonne am 23 Mai 2022
Bearbeitet: Jacob Sonne am 23 Mai 2022
I am trying to seriaze/deserialize a model using json but a run into problem. Basically my problem is that the json encoded 1xN arrays get decoded into Nx1 arrays which compactly can be reproduced like this
>> a = [1,2,3];
>> assert( isequal(a, jsondecode(jsonencode(a))))
Assertion failed.
This was quite unexpected to me. A more expanded version of the problem goes like this
>> a = [1,2,3]
a =
1 2 3
>> s = jsonencode(a)
s =
'[1,2,3]'
>> jsondecode(s)
ans =
1
2
3
What I want is this i.e. encode/decode is reversible something like this
>> jsondecode(s)
ans = [1,2,3]
Please bear with me as I am new to MatLab but I do not underderstand this behavior. Can anyone please elaborate on this and guide me to how to obtain the result that I want? I think this is the same issue as is raised here Vector dimesions are different before encoding json and after decoding json - (mathworks.com) but I would like to avoid traversing my nested data structure and transposing all arrays.
  1 Kommentar
Jacob Sonne
Jacob Sonne am 23 Mai 2022
just for completeness encoding and decoding the transposed array behaves as I would expect
>> a = [1,2,3]';
>> assert( isequal(a, jsondecode(jsonencode(a))))
Likewise for a two dimensional
>> a = [1,2,3; 4,5,6];
>> assert( isequal(a, jsondecode(jsonencode(a))))

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Jan
Jan am 23 Mai 2022
Bearbeitet: Jan am 23 Mai 2022
Does the encode of Matlab's production server help?
a = [1,2,3]
a = 1×3
1 2 3
j1 = jsonencode(a)
j1 = '[1,2,3]'
a1 = jsondecode(j1)
a1 = 3×1
1 2 3
j2 = mps.json.encode(a)
j2 = '[[1,2,3]]'
a2 = mps.json.decode(j2)
a2 = 1×3
1 2 3
If mps is not available on your computer, store the data in a struct and use an ugly workaround:
S.a = [1,2,3];
S = IncludeSize(S);
j = jsonencode(S);
S2 = jsondecode(j);
S2 = struct with fields:
a: [3×1 double] size_: [1×1 struct]
S2 = RestoreSize(S2)
S2 = struct with fields:
a: [1 2 3]
function S = IncludeSize(S)
F = fieldnames(S);
for k = 1:numel(F)
aF = F{k};
data = S.(F{k});
if isnumeric(data) % Maybe: && isrow(data)
S.size_.(aF) = size(data);
end
end
end
function S = RestoreSize(S)
F = fieldnames(S.size_);
for k = 1:numel(F)
aF = F{k};
siz = S.size_.(aF);
S.(aF) = reshape(S.(aF), siz(:).');
end
S = rmfield(S, 'size_');
end
What a pity that json does not save the dimensions exhaustively.
  1 Kommentar
Jacob Sonne
Jacob Sonne am 23 Mai 2022
Bearbeitet: Jacob Sonne am 23 Mai 2022
thanks @Jan. Unfortunately I don't have access to Matlab's production server mps. I might try out your tomorrow but I find it odd that the basic encode/decode cannot come back to it's original state. I can't help wondering if this is the intended behavior?
>> a = [1,2,3];
>> size(a)
ans =
1 3
>> anew = jsondecode(jsonencode(a))
anew =
1
2
3
>> size(anew)
ans =
3 1

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by