function out = callWithStringSerializedData(command, varargin)
%

%   Copyright 2016 The MathWorks, Inc.

try
    iCheckSerializationToolsOnPath();

    for ii = 1:numel(varargin)
        varargin{ii} = com.mathworks.toolbox.shared.bigdata.spark.SerializationUtils.decodeFromString(varargin{ii});
        varargin{ii} = com.mathworks.toolbox.shared.bigdata.spark.SerializationUtils.deserialize(varargin{ii});
        if isa(varargin{ii}, 'java.lang.String')
            varargin{ii} = char(varargin{ii});
        end
    end

    out = feval(command, varargin{:});

    out = com.mathworks.toolbox.shared.bigdata.spark.SerializationUtils.serialize(out);
    out = char(com.mathworks.toolbox.shared.bigdata.spark.SerializationUtils.encodeToString(out));
catch err
    error('%s\n', err.getReport());
end

function iCheckSerializationToolsOnPath()
persistent haveAdded
if isempty(haveAdded)
    javaaddpath(fullfile(matlabroot, 'toolbox', 'shared', 'bigdata', 'jar', 'sparkinteg.jar'));
    haveAdded = true;
end
