Showing revision 1.0

Demo of JSONlab and output

1. example/demo_jsonlab_basic.m
2. output to example/demo_jsonlab_basic.m

1. example/demo_jsonlab_basic.m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%         Demonstration of Basic Utilities of JSONlab
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rngstate = rand ('state');
randseed=hex2dec('623F9A9E');
clear data2json json2data

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  a simple scalar value \n')
fprintf(1,'%%=================================================\n\n')

data2json=pi
savejson('',data2json)
json2data=loadjson(ans)

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  a complex number\n')
fprintf(1,'%%=================================================\n\n')

data2json=1+2*i
savejson('',data2json)
json2data=loadjson(ans) 

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  a complex matrix\n')
fprintf(1,'%%=================================================\n\n')

data2json=magic(6);
data2json=data2json(:,1:3)+data2json(:,4:6)*i
savejson('',data2json)
json2data=loadjson(ans)

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  MATLAB special constants\n')
fprintf(1,'%%=================================================\n\n')

data2json=[NaN Inf -Inf]
savejson('specials',data2json)
json2data=loadjson(ans)

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  a real sparse matrix\n')
fprintf(1,'%%=================================================\n\n')

data2json=sprand(10,10,0.1)
savejson('sparse',data2json)
json2data=loadjson(ans)

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  a complex sparse matrix\n')
fprintf(1,'%%=================================================\n\n')

data2json=data2json-data2json*i
savejson('complex_sparse',data2json)
json2data=loadjson(ans)

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  a structure\n')
fprintf(1,'%%=================================================\n\n')

data2json=struct('name','Think Different','year',1997,'magic',magic(3),...
                 'misfits',[Inf,NaN],'embedded',struct('left',true,'right',false))
savejson('astruct',data2json,struct('ParseLogical',1))
json2data=loadjson(ans)

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  a structure array\n')
fprintf(1,'%%=================================================\n\n')

data2json=struct('name','Nexus Prime','rank',9);
data2json(2)=struct('name','Sentinel Prime','rank',9);
data2json(3)=struct('name','Optimus Prime','rank',9);
savejson('Supreme Commander',data2json)
json2data=loadjson(ans)

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  a cell array\n')
fprintf(1,'%%=================================================\n\n')

data2json=cell(3,1);
data2json{1}=struct('buzz',1.1,'rex',1.2,'bo',1.3,'hamm',2.0,'slink',2.1,'potato',2.2,...
              'woody',3.0,'sarge',3.1,'etch',4.0,'lenny',5.0,'squeeze',6.0,'wheezy',7.0);
data2json{2}=struct('Ubuntu',['Kubuntu';'Xubuntu';'Lubuntu']);
data2json{3}=[10.04,10.10,11.04,11.10]
savejson('debian',data2json,struct('FloatFormat','%.2f'))
json2data=loadjson(ans)

fprintf(1,'\n%%=================================================\n')
fprintf(1,'%%  invalid field-name handling\n')
fprintf(1,'%%=================================================\n\n')

json2data=loadjson('{"ValidName":1, "_InvalidName":2, ":Field:":3, "项目":"绝密"}')

rand ('state',rngstate);

2. output to example/demo_jsonlab_basic.m

%=================================================
%  a simple scalar value 
%=================================================


data2json =

    3.1416


ans =

[3.141592654]



json2data =

    3.1416


%=================================================
%  a complex number
%=================================================


data2json =

   1.0000 + 2.0000i


ans =

{
	"_ArrayType_": "double",
	"_ArraySize_": [1,1],
	"_ArrayIsComplex_": 1,
	"_ArrayData_": [1,2]
}



json2data =

   1.0000 + 2.0000i


%=================================================
%  a complex matrix
%=================================================


data2json =

  35.0000 +26.0000i   1.0000 +19.0000i   6.0000 +24.0000i
   3.0000 +21.0000i  32.0000 +23.0000i   7.0000 +25.0000i
  31.0000 +22.0000i   9.0000 +27.0000i   2.0000 +20.0000i
   8.0000 +17.0000i  28.0000 +10.0000i  33.0000 +15.0000i
  30.0000 +12.0000i   5.0000 +14.0000i  34.0000 +16.0000i
   4.0000 +13.0000i  36.0000 +18.0000i  29.0000 +11.0000i


ans =

{
	"_ArrayType_": "double",
	"_ArraySize_": [6,3],
	"_ArrayIsComplex_": 1,
	"_ArrayData_": [
		[35,26],
		[3,21],
		[31,22],
		[8,17],
		[30,12],
		[4,13],
		[1,19],
		[32,23],
		[9,27],
		[28,10],
		[5,14],
		[36,18],
		[6,24],
		[7,25],
		[2,20],
		[33,15],
		[34,16],
		[29,11]
	]
}



json2data =

  35.0000 +26.0000i   1.0000 +19.0000i   6.0000 +24.0000i
   3.0000 +21.0000i  32.0000 +23.0000i   7.0000 +25.0000i
  31.0000 +22.0000i   9.0000 +27.0000i   2.0000 +20.0000i
   8.0000 +17.0000i  28.0000 +10.0000i  33.0000 +15.0000i
  30.0000 +12.0000i   5.0000 +14.0000i  34.0000 +16.0000i
   4.0000 +13.0000i  36.0000 +18.0000i  29.0000 +11.0000i


%=================================================
%  MATLAB special constants
%=================================================


data2json =

   NaN   Inf  -Inf


ans =

{
	"specials": ["_NaN_","_Inf_","-_Inf_"]
}



json2data = 

    specials: [NaN Inf -Inf]


%=================================================
%  a real sparse matrix
%=================================================


data2json =

   (1,2)       0.6557
   (9,2)       0.7577
   (3,5)       0.8491
  (10,5)       0.7431
  (10,8)       0.3922
   (7,9)       0.6787
   (2,10)      0.0357
   (6,10)      0.9340
  (10,10)      0.6555


ans =

{
	"sparse": {
		"_ArrayType_": "double",
		"_ArraySize_": [10,10],
		"_ArrayIsSparse_": 1,
		"_ArrayData_": [
			[1,2,0.6557406992],
			[9,2,0.7577401306],
			[3,5,0.8491293059],
			[10,5,0.7431324681],
			[10,8,0.3922270195],
			[7,9,0.6787351549],
			[2,10,0.03571167857],
			[6,10,0.9339932478],
			[10,10,0.6554778902]
		]
	}
}



json2data = 

    sparse: [10x10 double]


%=================================================
%  a complex sparse matrix
%=================================================


data2json =

   (1,2)      0.6557 - 0.6557i
   (9,2)      0.7577 - 0.7577i
   (3,5)      0.8491 - 0.8491i
  (10,5)      0.7431 - 0.7431i
  (10,8)      0.3922 - 0.3922i
   (7,9)      0.6787 - 0.6787i
   (2,10)     0.0357 - 0.0357i
   (6,10)     0.9340 - 0.9340i
  (10,10)     0.6555 - 0.6555i


ans =

{
	"complex_sparse": {
		"_ArrayType_": "double",
		"_ArraySize_": [10,10],
		"_ArrayIsComplex_": 1,
		"_ArrayIsSparse_": 1,
		"_ArrayData_": [
			[1,2,0.6557406992,-0.6557406992],
			[9,2,0.7577401306,-0.7577401306],
			[3,5,0.8491293059,-0.8491293059],
			[10,5,0.7431324681,-0.7431324681],
			[10,8,0.3922270195,-0.3922270195],
			[7,9,0.6787351549,-0.6787351549],
			[2,10,0.03571167857,-0.03571167857],
			[6,10,0.9339932478,-0.9339932478],
			[10,10,0.6554778902,-0.6554778902]
		]
	}
}



json2data = 

    complex_sparse: [10x10 double]


%=================================================
%  a structure
%=================================================


data2json = 

        name: 'Think Different'
        year: 1997
       magic: [3x3 double]
     misfits: [Inf NaN]
    embedded: [1x1 struct]


ans =

{
	"astruct": {
		"name": "Think Different",
		"year": [1997],
		"magic": [
			[8,1,6],
			[3,5,7],
			[4,9,2]
		],
		"misfits": ["_Inf_","_NaN_"],
		"embedded": {
			"left": [true],
			"right": [false]
		}
	}
}



json2data = 

    astruct: [1x1 struct]


%=================================================
%  a structure array
%=================================================


ans =

{
	"Supreme Commander": [
		{
			"name": "Nexus Prime",
			"rank": [9]
		},
		{
			"name": "Sentinel Prime",
			"rank": [9]
		},
		{
			"name": "Optimus Prime",
			"rank": [9]
		}
	]
}



json2data = 

    Supreme_0x20_Commander: [1x3 struct]


%=================================================
%  a cell array
%=================================================


data2json = 

    [1x1 struct]
    [1x1 struct]
    [1x4 double]


ans =

{
	"debian": [
		{
			"buzz": [1.10],
			"rex": [1.20],
			"bo": [1.30],
			"hamm": [2.00],
			"slink": [2.10],
			"potato": [2.20],
			"woody": [3.00],
			"sarge": [3.10],
			"etch": [4.00],
			"lenny": [5.00],
			"squeeze": [6.00],
			"wheezy": [7.00]
		},
		{
			"Ubuntu": [
				"Kubuntu",
				"Xubuntu",
				"Lubuntu"
			]
		},
		[10.04,10.10,11.04,11.10]
	]
}



json2data = 

    debian: {[1x1 struct]  [1x1 struct]  [10.0400 10.1000 11.0400 11.1000]}


%=================================================
%  invalid field-name handling
%=================================================


json2data = 

                ValidName: 1
            x_InvalidName: 2
       x_0x3A_Field_0x3A_: 3
    x_0xE9A1B9__0xE79BAE_: '绝密'
Powered by Habitat