Skip to content

Commit

Permalink
[feat] convert timeseries to json
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Jul 9, 2024
1 parent 1b26ce9 commit 27e7c0c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
14 changes: 13 additions & 1 deletion jdataencode.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
newitem = cell2jd(item, varargin{:});
elseif (isstruct(item))
newitem = struct2jd(item, varargin{:});
elseif (isnumeric(item) || islogical(item))
elseif (isnumeric(item) || islogical(item) || isa(item, 'timeseries'))
newitem = mat2jd(item, varargin{:});
elseif (ischar(item) || isa(item, 'string'))
newitem = mat2jd(item, varargin{:});
Expand Down Expand Up @@ -193,6 +193,18 @@
zipmethod = varargin{1}.compression;
minsize = varargin{1}.compressarraysize;

if (isa(item, 'timeseries'))
if (item.TimeInfo.isUniform && item.TimeInfo.Increment == 1)
if (ndims(item.Data) == 3 && size(item.Data, 1) == 1 && size(item.Data, 2) == 1)
item = permute(item.Data, [2 3 1]);
else
item = squeeze(item.Data);
end
else
item = [item.Time squeeze(item.Data)];
end
end

% 2d numerical (real/complex/sparse) arrays with _ArrayShape_ encoding enabled
if (varargin{1}.usearrayshape && ndims(item) == 2 && ~isvector(item))
encoded = 1;
Expand Down
6 changes: 3 additions & 3 deletions jsonpath.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function obj = jsonpath(root, jpath)
function obj = jsonpath(root, jpath, varargin)
%
% obj=jsonpath(root, jpath)
%
Expand Down Expand Up @@ -35,7 +35,7 @@
paths(1) = [];
end
for i = 1:length(paths)
[obj, isfound] = getonelevel(obj, paths, i);
[obj, isfound] = getonelevel(obj, paths, i, varargin{:});
if (~isfound)
return
end
Expand All @@ -44,7 +44,7 @@

%% scan function

function [obj, isfound] = getonelevel(input, paths, pathid)
function [obj, isfound] = getonelevel(input, paths, pathid, varargin)

pathname = paths{pathid};
if (iscell(pathname))
Expand Down
14 changes: 13 additions & 1 deletion savejson.m
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
txt = cell2json(name, item, level, varargin{:});
elseif (isstruct(item))
txt = struct2json(name, item, level, varargin{:});
elseif (isnumeric(item) || islogical(item))
elseif (isnumeric(item) || islogical(item) || isa(item, 'timeseries'))
txt = mat2json(name, item, level, varargin{:});
elseif (ischar(item))
if (~isempty(varargin{1}.compression) && numel(item) >= varargin{1}.compressstringsize)
Expand Down Expand Up @@ -594,6 +594,18 @@
format = opt.formatversion;
isnest = opt.nestarray;

if (isa(item, 'timeseries'))
if (item.TimeInfo.isUniform && item.TimeInfo.Increment == 1)
if (ndims(item.Data) == 3 && size(item.Data, 1) == 1 && size(item.Data, 2) == 1)
item = permute(item.Data, [2 3 1]);
else
item = squeeze(item.Data);
end
else
item = [item.Time squeeze(item.Data)];
end
end

if (~opt.nosubstruct_ && (((isnest == 0) && length(size(item)) > 2) || issparse(item) || ~isreal(item) || ...
(isempty(item) && any(size(item))) || opt.arraytostruct || (~isempty(dozip) && numel(item) > zipsize)))
if (isempty(name))
Expand Down

0 comments on commit 27e7c0c

Please sign in to comment.