|
32 | 32 | % tables, maps, ... as JData annotations) |
33 | 33 | % UnpackHex [1|0]: field names decoded with decodevarname (x0x20_ is a space) |
34 | 34 | % ForceRootName [0|1]: a struct or cell with no root name is written as {"root": ...} |
| 35 | +% Backend ['auto'|'mex'|'m']: 'auto' uses the compiled jdai backend (jdai.savemsgpack) |
| 36 | +% when it is on the path, else this file's code; 'mex' requires jdai; |
| 37 | +% 'm' never uses it |
35 | 38 | % output: |
36 | 39 | % msgpk: the MessagePack bytes (a char row, as savebj gives its bytes) |
37 | 40 | % |
|
63 | 66 | opt = varargin2struct(varargin{:}); |
64 | 67 | end |
65 | 68 |
|
| 69 | +% the compiled backend (jdai) when it is on the path: the same bytes, much faster; options and values |
| 70 | +% it does not handle, and any error, fall back to the code below |
| 71 | +backend = lower(jsonopt('Backend', 'auto', opt)); |
| 72 | +if (~strcmp(backend, 'm') && exist('jdai_mex', 'file') == 3) |
| 73 | + try |
| 74 | + if (nargout > 0 || isempty(jsonopt('FileName', '', opt))) |
| 75 | + msgpk = jdai.savemsgpack(rootname, obj, opt); |
| 76 | + else |
| 77 | + jdai.savemsgpack(rootname, obj, opt); |
| 78 | + end |
| 79 | + return |
| 80 | + catch err |
| 81 | + if (strcmp(backend, 'mex')) |
| 82 | + rethrow(err); |
| 83 | + end |
| 84 | + end |
| 85 | +end |
| 86 | + |
66 | 87 | o.packed = jsonopt('PackedArray', 0, opt) == 1; |
67 | 88 | o.keeptype = jsonopt('KeepType', 0, opt) == 1; |
68 | 89 | o.nestarray = jsonopt('NestArray', 0, opt) == 1; |
|
86 | 107 | % classes): on those values only, as they are met (it gives each value what it gives it within |
87 | 108 | % the whole), or on the whole value first when an option changes ordinary arrays too |
88 | 109 | o.preencode = jsonopt('PreEncode', 1, opt) == 1; |
89 | | -o.jdopt = opt; |
90 | | -o.jdopt.Compression = ''; % (arrays are compressed below, as savebj does) |
| 110 | +% (jdataencode compresses the data of what it encodes, complex and sparse arrays, as savebj does: |
| 111 | +% with the compression in effect, zlib by default) |
| 112 | +fn = fieldnames(opt); |
| 113 | +o.jdopt = rmfield(opt, fn(strcmpi(fn, 'Compression') | strcmpi(fn, 'CompressArraySize'))); |
| 114 | +o.jdopt.Compression = o.zip; |
| 115 | +o.jdopt.CompressArraySize = o.zipsize; |
91 | 116 | if (o.preencode && (jsonopt('UseArrayShape', 0, opt) || jsonopt('ArrayToStruct', 0, opt) || jsonopt('AnnotateArray', 0, opt) || ... |
92 | 117 | jsonopt('NestArray', 0, opt) || ~isempty(jsonopt('ArrayChunks', [], opt)) || jsonopt('FormatVersion', 4, opt) < 4)) |
93 | 118 | obj = jdataencode(obj, 'Base64', 0, 'UseArrayZipSize', 0, o.jdopt); |
|
0 commit comments