Skip to content

Commit

Permalink
Check for R2017a or older when using jsonencode
Browse files Browse the repository at this point in the history
jsonencode doesn't support the keyword arguments used to convert inf or NaN values to null in
json, making conversion loops impossible for these versions.

The fixes now will throw a warning on write.
  • Loading branch information
lawrence-mbf committed Sep 7, 2022
1 parent cd2baec commit 69a6498
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 69a6498

Please sign in to comment.