Skip to content

Commit

Permalink
Merge pull request #464 from NeurodataWithoutBorders/fix-jsonencode-c…
Browse files Browse the repository at this point in the history
…ompatibility

Check for R2017a or older when using jsonencode
  • Loading branch information
lawrence-mbf authored Sep 8, 2022
2 parents cd2baec + 69a6498 commit af00d7d
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions +schemes/exportJson.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,47 +23,61 @@
keepRelevantNamespace(Cache);
removeEmptySchemaComponents(Cache)
stripNamespaceFileExt(Cache.namespace);
JsonMap = containers.Map({'namespace'}, {jsonencode(Cache.namespace, 'ConvertInfAndNaN', true)});
JsonMap = containers.Map({'namespace'}, {encodeJsonWithConversion(Cache.namespace)});
for iScheme = 1:length(Cache.filenames)
filename = Cache.filenames{iScheme};
jsonencode(Cache.schema(filename));
JsonMap(filename) = jsonencode(Cache.schema(filename), 'ConvertInfAndNaN', true);
JsonMap(filename) = encodeJsonWithConversion(Cache.schema(filename));
end

JsonData(iCache).version = Cache.version;
JsonData(iCache).json = JsonMap;
end
end

function encoded = encodeJsonWithConversion(structs)
if verLessThan('matlab', '9.3')
% R2017a and older don't support keyword arguments for jsonencode
warning('NWB:ExportJson:InvalidConversion', ...
['MATLAB R2017a and earlier do not support automatic conversion ' ...
'of NaN, inf, and -inf to json null. If you encounter unreadable ' ...
'schemas, consider using a newer version of MATLAB.']);
encoded = jsonencode(structs);
else
encoded = jsonencode(structs, 'ConvertInfAndNaN', true);
end
end

function removeEmptySchemaComponents(Cache)
Schema = Cache.schema;
SchemaKeys = keys(Schema);
for iScheme = 1:length(SchemaKeys)
Scheme = Schema(SchemaKeys{iScheme});
SchemeKeys = keys(Scheme);
for iSub = 1:length(SchemeKeys)
SubScheme = Scheme(SchemeKeys{iSub});
if isempty(SubScheme{1})
remove(Scheme,SchemeKeys{iSub});
end
Schema = Cache.schema;
SchemaKeys = keys(Schema);
for iScheme = 1:length(SchemaKeys)
Scheme = Schema(SchemaKeys{iScheme});
SchemeKeys = keys(Scheme);
for iSub = 1:length(SchemeKeys)
SubScheme = Scheme(SchemeKeys{iSub});
if isempty(SubScheme{1})
remove(Scheme,SchemeKeys{iSub});
end
Schema(SchemaKeys{iScheme}) = Scheme;
end
Cache.schema = Schema;
Schema(SchemaKeys{iScheme}) = Scheme;
end
Cache.schema = Schema;
end


function keepRelevantNamespace(Cache)
Namespaces = Cache.namespace('namespaces');
ns = 1;
while ns <= length(Namespaces)
Namespace = Namespaces{ns};
if ~strcmp(Cache.name, Namespace('name'))
Namespaces(ns) = [];
end
ns = ns+1;
Namespaces = Cache.namespace('namespaces');
ns = 1;
while ns <= length(Namespaces)
Namespace = Namespaces{ns};
if ~strcmp(Cache.name, Namespace('name'))
Namespaces(ns) = [];
end
Cache.namespace('namespaces') = Namespaces;
ns = ns+1;
end
Cache.namespace('namespaces') = Namespaces;
end

function NamespaceRoot = stripNamespaceFileExt(NamespaceRoot)
Namespaces = NamespaceRoot('namespaces');
for ns = 1:length(Namespaces)
Expand Down

0 comments on commit af00d7d

Please sign in to comment.