help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Octave json creator


From: veryhappy
Subject: Octave json creator
Date: Sat, 4 Dec 2010 02:38:38 -0800 (PST)

I'd created a function to turn octave data into a json object.
I would like to share it with you with the hope it will be useful to someone
else. I'd not exhaustively tested it but it should be fairly complete. It
should be able to compute structs, cells and multidimensional array keeping
the dimensions right.
If you have any suggestion or if it serve good for you, please, tell me.
Here it go:


%% Copyright (C) 2010 Torre Herrera, Daniel de
%% 
%% This program is free software; you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published by
%% the Free Software Foundation; either version 2 of the License, or
%% (at your option) any later version.
%% 
%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%% GNU General Public License for more details.
%% 
%% You should have received a copy of the GNU General Public License
%% along with Octave; see the file COPYING.  If not, see
%% <http://www.gnu.org/licenses/>.
%% object2json.m
%% Ultima modificacion: 2010-12-03

function json=object2json(object)
  % function json=object2json(object)
  % This function returns a valid json string that will describe object
  
  s=size(object);
  if(all(s==1))
    % It's not a vector so we must check how to map it
    % Depending on the class of the object we must do one or another thing
    switch(class(object))
      case 'function_handle'
        % For a function handle we will only print the name
        fun=functions(object);
        json=['"',fun.function,'"'];
      case 'struct'
        fields=fieldnames(object);
       
results=cellfun(@object2json,struct2cell(object),"UniformOutput",false);
        json='{';
        if(numel(fields)>1)
          sep=',';
        else
          sep='';
        endif
        for(tmp=1:numel(fields))
          json=[json,'"',fields{tmp},'":',results{tmp},sep];
          if(tmp>=numel(fields)-1)
            sep='';
          endif
        endfor
        json(end+1)='}';
      case 'cell'
        % We dereference the cell and use it as a new value
        json=object2json(object{1});
      case 'double'
        if(isreal(object))
          json=num2str(object);
        else
          if(iscomplex(object))
           
json=['{"real":',num2str(real(object)),',"imag:"',num2str(imag(object)),'}'];
          endif
        endif
      case 'char'
        % Here we handle a single char
        json=['"',object,'"'];
      otherwise
        % We don't know what is it so we'll put the class name
        json=['"',class(object),'"'];
    endswitch
  else
    % It's a vector so it maps to an array
    sep='';
    if(numel(s)>2)
      json='[';
      for(tmp=1:s(1))
        json=[json,sep,object2json(reshape(object(tmp,:),s(2:end)))];
        sep=',';
      endfor
      json(end+1)=']';
    else
      % We can have three cases here:
      % Object is a row -> array with all the elements
      % Object is a column -> each element is an array in it's own
      % Object is a 2D matrix -> separate each row
      if(s(1)==1)
        % Object is a row
        if(ischar(object))
          % If it's a row of chars we will take it as a string
          json=['"',object,'"'];
        else
          json='[';
          for(tmp=1:s(2))
            json=[json,sep,object2json(object(1,tmp))];
            sep=',';
          endfor
          json(end+1)=']';
        endif
      elseif(s(2)==1)
        % Object is a column
        json='[';
        for(tmp=1:s(1))
          json=[json,'[',object2json(object(tmp,1)),']'];
        endfor
        json(end+1)=']';
      else
        % Object is a 2D matrix
        json='[';
        for(tmp=1:s(1))
          json=[json,sep,object2json(object(tmp,:))];
          sep=',';
        endfor
        json(end+1)=']';
      endif
    endif
  endif
endfunction
-- 
View this message in context: 
http://octave.1599824.n4.nabble.com/Octave-json-creator-tp3072273p3072273.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]