Code Monkey home page Code Monkey logo

Comments (9)

fangq avatar fangq commented on May 18, 2024

This has been requested more than one time, let's add that support. will work on it in the next few days.

from jsonlab.

scottjm-user avatar scottjm-user commented on May 18, 2024

I would also like to request this feature if you have not implemented it already. It would be very valuable.

from jsonlab.

fangq avatar fangq commented on May 18, 2024

Here is my fix to this issue

7dd018b

Basically, I added an option called "SingletCell". By default, it is 1, which force printing [] even a cell has only one element.

I also retired NoRowBracket, and replaced it with SingletArray. SingletArray is set as not(NoRowBracket). By default, SingletArray is 0, which removes [] for a single scalar.

Now, the remaining question is how to deal with struct arrays. Currently, I am using SingletArray flag to control the behavior, as if a struct array is a numerical array - i.e. a 1x1 struct will be printed as

"astruct": { "field1": value1, "field2":value2,...}

instead of

"astruct": {[ "field1": value1, "field2":value2,...]}

What do you think?

from jsonlab.

jpaasen avatar jpaasen commented on May 18, 2024

Thank you for your effort Qianqian.

Will this preserve the first example file in #15 if you load and save it? In that example you have both a single struct and a single struct within an array. In that issue there is also an example of a file which contains both a key with a scalar value and a key with a scalar value inside an array.

My issue is that the json files saved by savejson is going to be used as input to another system (C++/python) where there is a difference between a single value and and single value in an array, in addition to a single map/struct and a single map/struct in an array. Therefore it can be some situations where I do not want brackets on all single elements, hence a global flag will not do the job. If for instance a json file is generated by some python or C++ software, and I want to load it in Matlab, change something and write it back, I need load-/save-json to not alter the layout.

It might be to tedious, but currently loadjson is differentiating between a single struct and a single struct in an array by placing the latter 1x1 struct-array inside a cell-array. Could one do the same with a single value in an array to?

So,

{
    "a":[0],
    "b":1,
    "c":[2, 3]
}

will in Matlab look like

a : {[1x1 array]}
b : [1x1 array]
c : [2x1 array]

I'm not sure if this is a good solution, but somehow I need the information about which elements who was originally scalar values (or struct/map) or a value (or struct/map) inside an array to be preserved. So that savejson can store it in the correct way.

My suggestion is that the save json functions automatically adds square brackets to single elements matlab arrays which are contained within a cell-array. It should probably not be the default behaviour, but rater a compliant-mode if the load/save functions are used in combination with other programming languages.

If you have another solution that accomplish the same I'll be fine with that. ;-)

from jsonlab.

fangq avatar fangq commented on May 18, 2024

@jpaasen

Will this preserve the first example file in #15 if you load and save it?

try it, it works on my end

My issue is that the json files saved by savejson is going to be used as input to another system (C++/python) where there is a difference between a single value and and single value in an array,

if you have access to the C/C++/python source code, you can make your code more flexible to deal with situations like this. For example, I did the same for a C code I wrote to handle single element with or without bracket

fangq/mmc@08da9f9

I used cJSON library to access the JSON structure. If you can implement something like mine, I am sure you can deal with situations like this

{
    "a":[0],
    "b":1,
    "c":[2, 3]
}

however, at this point, JSONLab can not preserve the above form with loadjson/savejson because SingletArray flag will force "a" and "b" to use a consistent format, either with bracket, or not, but not mixed.

from jsonlab.

jpaasen avatar jpaasen commented on May 18, 2024

@fangq

try it, it works on my end

Good. I'll test it.

if you have access to the C/C++/python source code, you can make your code more flexible to deal with situations like this. For example, I did the same for a C code I wrote to handle single element with or without bracket

Sure, but in my opinion it makes much more sense to handle, what is basically a Matlab issue, within the Matlab json load/save functions instead of everywhere else. Sometimes you have access to the other source code, but sometimes you don't.

I still wish for a mixed bracket mode for single arrays which can be turned on if needed ;-)

from jsonlab.

fangq avatar fangq commented on May 18, 2024

here is a workaround. first download the latest version with my above patch. then, try below commands

>> ! cat test.json
{
    "a":[0],
    "b":1,
    "c":[2, 3]
}

>> tt=loadjson('test.json','FastArrayParser',0)

tt = 

    a: {[0]}
    b: 1
    c: {[2]  [3]}

>> savejson('',tt)

ans =

{
    "a": [
        0
    ],
    "b": 1,
    "c": [
        2,
        3
    ]
}

>> loadjson(savejson('',tt),'FastArrayParser',0)

ans = 

    a: {[0]}
    b: 1
    c: {[2]  [3]}

The original version of loadjson, derived from below upstream works

http://www.mathworks.com/matlabcentral/fileexchange/20565-json-parser
http://www.mathworks.com/matlabcentral/fileexchange/23393--another--json-parser
http://www.mathworks.com/matlabcentral/fileexchange/25713-highly-portable-json-input-parser

parses any array into a cell array with each array element into a cell element. I added something I called "fast array parser" (5b82753), which groups array elements as much as possible into an array instead of a cell. This alone accelerated the parsing process by 50-80 fold.

Later on, I added an option "FastArrayParser" to set the min dimension beyond which the grouping happens (9b4c3ba). Setting it to 0 disables the fast parser entirely.

The difference between [0] and 0 is only distinguishable when you disable the fast array parser.

If you deal only with small JSON objects and wants to keep the depth level of all elements, you may consider setting FastArrayParser to 0 as in the above example. Just be aware that you will have a performance penalty when dealing with large JSON inputs.

from jsonlab.

fangq avatar fangq commented on May 18, 2024

I still wish for a mixed bracket mode for single arrays which can be turned on if needed ;-)

of course I open to ideas for generating consistent output. currently, the nature of the difficulty comes from the ambiguity of the data mapping between JSON data structure (numbers, arrays and objects) to MATLAB data structures (scalars, arrays, structs, struct arrays, cells, cell arrays, classes, and objects). Unless we define a mapping that uniquely label the input data, to reproduce any JSON input is going to be difficult.

As I suggested in this reply (#1 (comment)), the current goal is to do round-trip convergence after one or two iterations into the loadjson/savejson loop.

from jsonlab.

fangq avatar fangq commented on May 18, 2024

Closing this bug for now. please reopen if something comes up.

from jsonlab.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.