Skip to content

Commit

Permalink
complete saveubjson debug mode, add compression example
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jun 12, 2019
1 parent f28f010 commit e5f2ffb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 8 deletions.
16 changes: 16 additions & 0 deletions examples/demo_jsonlab_basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,21 @@
json2data=loadjson(ans)
end

try
val=zlibencode('test');
fprintf(1,'\n%%=================================================\n')
fprintf(1,'%% a 2-D array in compressed array format\n')
fprintf(1,'%%=================================================\n\n')

data2json=eye(10);
data2json(20,1)=1;
savejson('',data2json,'Compression','zlib','CompressionSize',0) % nestarray for 4-D or above is not working
json2data=loadjson(ans)
if(any(json2data(:)~=data2json(:)) || any(size(json2data)~=size(data2json)))
warning('conversion does not preserve original data');
end
catch
end

rand ('state',rngstate);

16 changes: 16 additions & 0 deletions examples/demo_ubjson_basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,21 @@
json2data=loadubjson(ans)
end

try
val=zlibencode('test');
fprintf(1,'\n%%=================================================\n')
fprintf(1,'%% a 2-D array in compressed array format\n')
fprintf(1,'%%=================================================\n\n')

data2json=eye(10);
data2json(20,1)=1;
saveubjson('',data2json,'Compression','zlib','CompressionSize',0) % nestarray for 4-D or above is not working
json2data=loadubjson(ans)
if(any(json2data(:)~=data2json(:)) || any(size(json2data)~=size(data2json)))
warning('conversion does not preserve original data');
end
catch
end

rand ('state',rngstate);

42 changes: 42 additions & 0 deletions examples/jsonlab_basictest.matlab
Original file line number Diff line number Diff line change
Expand Up @@ -1265,4 +1265,46 @@ json2data =

table: {[1x1 struct] [1x1 struct] [1x1 struct]}

>> >>
%=================================================
% a 2-D array in compressed array format
%=================================================


ans =

{
"_ArrayType_": "double",
"_ArraySize_": [20,10],
"_ArrayCompressionSize_": [1,200],
"_ArrayCompressionMethod_": "zlib",
"_ArrayCompressedData_": "eJxjYACBD/YMNAGj5o6aO2ruKBgFgwtQL10DAMHODQY=
"
}



json2data =

1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0

>> >> >> >>
35 changes: 35 additions & 0 deletions examples/jsonlab_ubjson_basictest.matlab
Original file line number Diff line number Diff line change
Expand Up @@ -783,4 +783,39 @@ json2data =

table: {[1x1 struct] [1x1 struct] [1x1 struct]}

>> >>
%=================================================
% a 2-D array in compressed array format
%=================================================


ans =

{U _ArrayType_SUdoubleU _ArraySize_[$U#U
U_ArrayCompressionSize_[$U#UÈU_ArrayCompressionMethod_SUzlibU_ArrayCompressedData_[$U#U xœc` ö 4£æŽš;jî(ƒ P/] ÁÎ}


json2data =

1 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0

>> >> >> >>
Expand Down
16 changes: 8 additions & 8 deletions saveubjson.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@
end
opt.Compression=dozip;
end
ismsgpack=jsonopt('MessagePack',0,opt);
if(~ismsgpack)
ismsgpack=jsonopt('MessagePack',0,opt) + bitshift(jsonopt('Debug',0,opt),1);
if(~bitget(ismsgpack, 1))
opt.IM_='UiIlL';
opt.FM_='dD';
opt.FTM_='FT';
Expand Down Expand Up @@ -187,7 +187,7 @@
end
json=obj2ubjson(rootname,obj,rootlevel,opt);
if(~rootisarray)
if(ismsgpack)
if(bitget(ismsgpack, 1))
json=[char(129) json opt.OM_{2}];
else
json=[opt.OM_{1} json opt.OM_{2}];
Expand Down Expand Up @@ -725,15 +725,15 @@
%%-------------------------------------------------------------------------
function val=N_(str)
global ismsgpack
if(~ismsgpack)
val=[I_(int32(length(str))) str];
if(~bitget(ismsgpack, 1))
val=[I_(int32(length(str)),'UiIlL',struct('Debug',bitget(ismsgpack,2))) str];
else
val=S_(str);
end
%%-------------------------------------------------------------------------
function val=S_(str)
global ismsgpack
if(ismsgpack)
if(bitget(ismsgpack, 1))
Smarker=char([161,219]);
Imarker=char([204,208:211]);
else
Expand All @@ -743,10 +743,10 @@
if(length(str)==1)
val=[Smarker(1) str];
else
if(ismsgpack)
if(bitget(ismsgpack, 1))
val=[Imsgpk_(length(str),Imarker,218,160) str];
else
val=['S' I_(int32(length(str))) str];
val=['S' I_(int32(length(str)),Imarker,struct('Debug',bitget(ismsgpack,2))) str];
end
end

Expand Down

0 comments on commit e5f2ffb

Please sign in to comment.