Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readable compact format #26

Closed
nyurik opened this issue Jun 2, 2017 · 9 comments
Closed

Readable compact format #26

nyurik opened this issue Jun 2, 2017 · 9 comments

Comments

@nyurik
Copy link

nyurik commented Jun 2, 2017

Is there a "compact but readable" format for HJSON? Something like https://github.com/lydell/json-stringify-pretty-compact for JSON?

Thanks!

@laktak
Copy link
Member

laktak commented Jun 22, 2017

You are talking about

{
  "bool": true,
  "short array": [1, 2, 3],
  "long array": [
    {"x": 1, "y": 2},
    {"x": 2, "y": 1},
    {"x": 1, "y": 1},
    {"x": 2, "y": 2}
  ]
}

That would only work for Hjson if none of the elements inside are quoteless strings (they require a new line each).

If it was only allowed in combination with the "quote all strings" option it would probably be easy to implement here, otherwise no.

@laktak laktak closed this as completed Jul 13, 2017
@nyurik
Copy link
Author

nyurik commented Jul 14, 2017

@laktak would it make sense to have a "mixed" approach - if multiple values can fit on the same line, encode them as regular JSON (just like compact json formatter does). Otherwise, follow HJSON rules

@laktak
Copy link
Member

laktak commented Jul 15, 2017

Like I said, that won't be easy to do. What is your use case? How would you define "fit on the same line"?

@nyurik
Copy link
Author

nyurik commented Jul 16, 2017

@laktak I was hoping to adapt HJSON to Vega (JSON) editing -- HJSON should make this code much more readable (BTW, is there a HJSON support for Monaco text editor?). A small set of values in a list should be on the same line -- e.g. key: [1, 2, "3", false]. Same with a small object: key: {"a":1, "b":2}.

laktak added a commit that referenced this issue Jul 20, 2017
@laktak
Copy link
Member

laktak commented Jul 20, 2017

After thinking about why this can't work I experimented a bit and found it to be working quite well :) Thanks for the idea!

You need to call

    result = Hjson.stringify(obj, { condense: 60 });

Let me know if this works for you.

@laktak laktak reopened this Jul 20, 2017
@laktak
Copy link
Member

laktak commented Jul 20, 2017

@nyurik
Copy link
Author

nyurik commented Jul 21, 2017

@laktak this is an awesome start! Here is an example of a compact( len=65 ) json and hjson representation of a Vega diagram with your new changes. The compact JSON is still more compact and readable, because the eyes do not stumble on each "{" and "[" that occupies a separate line: https://gist.github.com/nyurik/96ab0d0d19d4c3aee83b0250fbb9335c/revisions

Do you think the initial (not trailing) [ and { can be placed on the same line as : ?

Thanks for your hard work!!!

@laktak
Copy link
Member

laktak commented Jul 21, 2017

Happy to help with your project. Looks very cool btw!

There's actually a switch for controlling the braces:

    result = Hjson.stringify(obj, { condense: 60, bracesSameLine: true });

@nyurik
Copy link
Author

nyurik commented Jul 24, 2017

This is awesome, thanks!!!

@nyurik nyurik closed this as completed Jul 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants