Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: Support jsonencode/jsondecode as regular functions #24

Merged
merged 1 commit into from
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions +bids/+util/jsondecode.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
% Copyright (C) 2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging
% Copyright (C) 2018--, BIDS-MATLAB developers

persistent has_jsondecode
if isempty(has_jsondecode)
has_jsondecode = ...
exist('jsondecode','builtin') == 5 || ... % MATLAB >= R2016b
ismember(exist('jsondecode','file'), [2 3]); % jsonstuff or other Matlab-compatible implementation
end

if exist('jsondecode','builtin') == 5 % MATLAB >= R2016b
value = builtin('jsondecode', fileread(file));
if has_jsondecode
value = jsondecode(fileread(file));
elseif exist('spm_jsonread','file') == 3 % SPM12
value = spm_jsonread(file, varargin{:});
elseif exist('jsonread','file') == 3 % JSONio
Expand Down
12 changes: 9 additions & 3 deletions +bids/+util/jsonencode.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
% Copyright (C) 2018, Guillaume Flandin, Wellcome Centre for Human Neuroimaging
% Copyright (C) 2018--, BIDS-MATLAB developers


if ~nargin
error('Not enough input arguments.');
end

persistent has_jsonencode
if isempty(has_jsonencode)
has_jsonencode = ...
exist('jsonencode','builtin') == 5 || ... % MATLAB >= R2016b
ismember(exist('jsonencode','file'), [2 3]); % jsonstuff or other Matlab-compatible implementation
end

if exist('spm_jsonwrite','file') == 2 % SPM12
[varargout{1:nargout}] = spm_jsonwrite(varargin{:});
elseif exist('jsonwrite','file') == 2 % JSONio
[varargout{1:nargout}] = jsonwrite(varargin{:});
elseif exist('jsonencode','builtin') == 5 % MATLAB >= R2016b
elseif has_jsonencode
file = '';
if ischar(varargin{1})
file = varargin{1};
Expand All @@ -44,7 +50,7 @@
end
end
end
txt = builtin('jsonencode', varargin{:});
txt = jsonencode(varargin{:});
if ~isempty(file)
fid = fopen(file,'wt');
if fid == -1
Expand Down