Skip to content

Option to keep tuples on the same line #100

Closed

Description

@adrientetar

I'm using rapidjson (great package, thank you!) to dump contents to a json file, and I use indent=0 to get a readable/diffable file.

However I have many tuples in my files, such as transformation matrices, and (though JSON has no tuple type) it would be nice to have them all on the same line instead of broken down, which makes the file harder tor ead and larger.

So basically:

[{"matrix": (1, 0, 0, 1, 181, 0)}, {"matrix": (1, 0, 0, 1, 0, 0)}]

would become:

[
{
"matrix": [1, 0, 0, 1, 181, 0]
},
{
"matrix": [1, 0, 0, 1, 181, 0]
}
]

instead of:

[
{
"matrix": [
1,
0,
0,
1,
181,
0
]
},
{
"matrix": [
1,
0,
0,
1,
181,
0
]
}
]

because tuples wouldn't get flattened.

Does that sound reasonable / is it feasible?

Activity

  1. lelit commented on Apr 20, 2018

    @lelit
    Contributor
  2. adrientetar commented on Apr 20, 2018

    @adrientetar
    Author

    Thanks for the thoughts! It seems unlikely a JSON library would add support for "variable" indentation, so instead I chose to first make the string with all the newlines, and then find lists that contains simple elements (aka, no collections) and remove newlines within them.

  3. lelit commented on Apr 20, 2018

    @lelit
    Contributor

    One more idea, that may or may not be simpler than massaging the final output: if you have control on how the data in produced, you could preformat those tuples and wrap the result in RawJSON instances, and then call dumps() on the whole dataset...

  4. adrientetar commented on Apr 20, 2018

    @adrientetar
    Author

    Indeed that looks like it'd solve my problem efficiently, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions