Coder Social home page Coder Social logo

json-reformat's Introduction

JSON Reformat

Build Status Test Coverage Status melpa badge melpa stable badge

json-reformat.el is reformat tool for JSON.

Important

From emacs 24.4 , json-pretty-print and json-pretty-print-buffer (similar specifications as json-reformat-region) was bundled.

Requirements

  • Emacs 23 or higher

Installation

You can install from MELPA with package.el

M-x package-install RET json-reformat

Usage

M-x json-reformat-region

Sample 1

Sample 2

Configuration

json-reformat:indent-width (integer)

    Change indentation level (default 4)

json-reformat:pretty-string? (boolean)

    Specify whether to decode the string (default nil)

    Example:

    ;; {"name":"foo\"bar","nick":"foo \u00e4 bar","description":"<pre>\nbaz\n</pre>","home":"/home/foobar"}

    If nil:

    {
        "name": "foo\"bar",
        "nick": "foo \u00e4 bar",
        "description": "<pre>\nbaz\n<\/pre>",
        "home": "\/home\/foobar"
    }

    Else t:

    {
        "name": "foo\"bar",
        "nick": "foo ä bar",
        "description": "<pre>
    baz
    </pre>",
        "home": "/home/foobar"
    }

LICENSE

MIT License. see json-reformat.el

json-reformat's People

Contributors

ahwatts avatar gongo avatar kosh04 avatar psequeirag avatar purcell avatar sviridov avatar swsnr avatar syohex avatar twmr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

json-reformat's Issues

Slashes are escaped

Hi, I'm using version 20141210.1656 installed from MELPA.

When reformatting some JSON that has forward slashes (like URLs), the reformat escapes them with backward slashes, but I don't think that's strictly necessary and sometimes is in fact undesirable (at least for me).

For example:

{"URL":"https://example.com"}

Is reformated as:

{
    "URL": "https:\/\/example.com"
}

What do you think of reformatting rather like this?

{
    "URL": "https://example.com"
}

Reverses field order of objects

{
  "a": 1,
  "b": 2,
  "c": 3,
  "d": 4,
  "e": 5
}

becomes

{
  "e": 5,
  "d": 4,
  "c": 3,
  "b": 2,
  "a": 1
}

becomes

{
  "a": 1,
  "b": 2,
  "c": 3,
  "d": 4,
  "e": 5
}

(Return of #10?)

Substitution Problems

I noticed with recent versions the following 2 undesirable behaviors:
"foo": {},

is changed to:
"foo": null,

and
"conf": "^(amq\\.gen.*|amq\\.default)$",

is changed to:
"conf": "^(amq\.gen.*|amq\.default)$",

Note, I have (setq json-reformat:pretty-string? t) in my config to prevent forward slashes from being escaped.

json-encode-string usage for reformating

Hi, @gongo.
I'm trying to understand why json-encode-string is called in json-reformat:string-to-string function.
I would expect a reformating tool to be just reformatting json, but not encoding it in process.
The example of my confusion is following.

I'm using relative URLs intensively.
When my HTTP server gives me back:

{"href": "/v1/subscriptions"}

after json-reformat-region I get:

{
    "href": "\/v1\/subscriptions"
}

which is not what I expect.
Can you, please, suggest how can we mitigate this?
Thank you.

Why would you use?

I see that emacs comes with json-pretty-print and json-pretty-print-buffer.

Marks buffer dirty even if nothing changed

  1. Mark whole buffer.
  2. json-reformat-region.
  3. Buffer is marked dirty. Save it.
  4. Mark whole buffer.
  5. json-reformat-region.
  6. Buffer is marked dirty again, even though nothing changed.

Status of this library?

I see there are a few unresolved issues and open pull requests, but no commits have been made for 5 years. Is this package maintained, or should we at MELPA encourage a new potential maintainer to step up, e.g. @thisch?

If you're not planning to do any further maintenance yourself, you could either grant access to your repo to one or more co-maintainers, or transfer the repo to someone so that the issues/PRs are kept intact.

Can not download

When trying to download from MELPA I get:
http://melpa.milkbox.net/packages/json-reformat-20151007.1728.el: Not found

Display the error line number

The json-reformat will display Bad string format: "doesn't start with '\"'!" if there are some wrong formats in the file. However, it is difficult to trace where the error occurred.

How about adding more information to the error message, like json.tool module in Python: Expecting property name: line 21 column 5 (char 514).

Feature request: add a minify command (opposite of beautify)

Sometimes it would be handy to be able to minify the json back to a "single row". Just doing naive replacement of whitespace might break the content of strings so I think this would require some minimal parsing. Or maybe just adjusting the output of the beautifier with some settings.

Pretty-print setting convert "{}" to null

When I run json-pretty-print {} is converted to null, e.g.

{
"type": {}
}
becomes
{
"type": null
}

This breaks some of the parsing tools I am using. Is there a way of inhibitiing this?

Bind json-reformat-region to a key

If you can bind json-reformat-region to a key like C-c C-c r(not sure whether this key is a good idea) or something like that, would be much appreciated.

Fail test on Emacs 25 json-reformat-test:json-reformat-region-occur-error and json-reformat-test:string-to-string

Hello, Some tests fail on Emacs 25 (unlike on version 24).

Running 10 tests (2017-12-03 17:16:26+0300)
   passed   1/10  json-reformat-test:indent
   passed   2/10  json-reformat-test:json-reformat-region
Test json-reformat-test:json-reformat-region-occur-error backtrace:
  (if (unwind-protect (setq value-152 (apply fn-150 args-151)) (setq f
  (let (form-description-154) (if (unwind-protect (setq value-152 (app
  (let ((value-152 (quote ert-form-evaluation-aborted-153))) (let (for
  (let ((fn-150 (function string=)) (args-151 (list "JSON parse error 
  (progn (fset (quote message) vnew) (let ((temp-buffer (generate-new-
  (unwind-protect (progn (fset (quote message) vnew) (let ((temp-buffe
  (let* ((vnew (function (lambda (&rest args) (setq message-string (ap
  (let (message-string) (let* ((vnew (function (lambda (&rest args) (s
  (lambda nil (let (message-string) (let* ((vnew (function (lambda (&r
  ert--run-test-internal([cl-struct-ert--test-execution-info [cl-struc
  ert-run-test([cl-struct-ert-test json-reformat-test:json-reformat-re
  ert-run-or-rerun-test([cl-struct-ert--stats t [[cl-struct-ert-test j
  ert-run-tests(t #[385 "\306�\307\"\203G�\211\211G\310U\203��\211@\20
  ert-run-tests-batch(nil)
  ert-run-tests-batch-and-exit()
  command-line-1(("-L" "." "-l" "json-reformat.el" "-l" "test/json-ref
  command-line()
  normal-top-level()
Test json-reformat-test:json-reformat-region-occur-error condition:
    (ert-test-failed
     ((should
       (string= "JSON parse error [Reason] Bad string format: \"doesn't start with '\\\"'!\" [Position] In buffer, line 3 (char 6)" message-string))
      :form
      (string= "JSON parse error [Reason] Bad string format: \"doesn't start with '\\\"'!\" [Position] In buffer, line 3 (char 6)" "JSON parse error [Reason] Bad string format: \"doesn't start with `\\\"'!\" [Position] In buffer, line 3 (char 6)")
      :value nil))
   FAILED   3/10  json-reformat-test:json-reformat-region-occur-error
   passed   4/10  json-reformat-test:number-to-string
   passed   5/10  json-reformat-test:print-node
Test json-reformat-test:string-to-string backtrace:
  (if (unwind-protect (setq value-92 (apply fn-90 args-91)) (setq form
  (let (form-description-94) (if (unwind-protect (setq value-92 (apply
  (let ((value-92 (quote ert-form-evaluation-aborted-93))) (let (form-
  (let ((fn-90 (function string=)) (args-91 (list "\"\\u2661\"" (json-
  (lambda nil (let ((fn-80 (function string=)) (args-81 (list "\"fooba
  ert--run-test-internal([cl-struct-ert--test-execution-info [cl-struc
  ert-run-test([cl-struct-ert-test json-reformat-test:string-to-string
  ert-run-or-rerun-test([cl-struct-ert--stats t [[cl-struct-ert-test j
  ert-run-tests(t #[385 "\306�\307\"\203G�\211\211G\310U\203��\211@\20
  ert-run-tests-batch(nil)
  ert-run-tests-batch-and-exit()
  command-line-1(("-L" "." "-l" "json-reformat.el" "-l" "test/json-ref
  command-line()
  normal-top-level()
Test json-reformat-test:string-to-string condition:
    (ert-test-failed
     ((should
       (string= "\"\\u2661\""
		(json-reformat:string-to-string "♡")))
      :form
      (string= "\"\\u2661\"" "\"♡\"")
      :value nil))
   FAILED   6/10  json-reformat-test:string-to-string
   passed   7/10  json-reformat-test:string-to-string-when-pretty
   passed   8/10  json-reformat-test:symbol-to-string
   passed   9/10  json-reformat-test:tree-to-string
   passed  10/10  json-reformat-test:vector-to-string

Ran 10 tests, 8 results as expected, 2 unexpected (2017-12-03 17:16:26+0300)

2 unexpected results:
   FAILED  json-reformat-test:json-reformat-region-occur-error
   FAILED  json-reformat-test:string-to-string

The slash symbol was escaped after formatting

There is an unexpected and unreliable behavior when formatting a string such as file path:

Before:

{
  "js": ["to/some/path"]
}

After:

{
  "js": [
    "to\/some\/path"
  ]
}

The slash symbols were escaped.

Reformat dict messes with field order

OK, technically a JSON "dictionary" object is unordered, but in practice when we're editing stuff the order sometimes has meaning.

json-reformat-region {"name":"Joe Smith", "address":"123 Mill Lane", "city":"Evanston", "state":"illinois", "zip":"12345"}
=>
{
"zip": "12345",
"state": "illinois",
"city": "Evanston",
"address": "123 Mill Lane",
"name": "Joe Smith"
}

It seems it basically reverses it. One would like to see:
{
"name": "Joe Smith",
"address": "123 Mill Lane",
"city": "Evanston",
"state": "illinois",
"zip": "12345"
}

'loop' is an obsolete alias

When loading json-reformat 0.0.6 in Emacs 28.1, it complains:

json-reformat.el: Warning: 'loop' is an obsolete alias (as of 27.1); use 'cl-loop' instead.

Format a single line?

Hello @gongo!

Wonderful package, very lean and tidy :)

I was wondering if it is possible to indent a single line only so that I can assign:

(setq-local indent-line-function #'json-reformat-line) ;; wishful thinking
(setq-local indent-region-function #'json-reformat-region)

Maybe something to add? 😺

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.