Repository: tdewolff/minify (language: python)

Issue:
JSON: Precision does not seem to work
Hi, I have been using minify/json, but it seems the additional `Precision` field does not seem to take any effects? I've tested this with code and with the online example. Is it just me or the same for everyone else?

![image](https://user-images.githubusercontent.com/9721723/115273576-a21cc600-a137-11eb-93e0-798b2da45923.png)

In the example, I'd like the 1000 to remain as 1000, however it's always `1e3` no matter what value I assign to the Precision.

Thank you very much. 


Discussion / hints:
Precision specifies the maximum amount of precision to retain. That is, it will cut `1.23456789` to `1.234` for example. The second step is minification, which has nothing to do with precision. The fact is that `1000` is equivalent to `1e3`, the second version is shorter which is why it is minified as such. Is there a specific reason why this is a problem for you?
Thank you very much for you quick reply, I misunderstood the usage of Precision.

So I'm building API clients and services. Where the request body from clients are minified using the minify/json. However, from the services side, it is unable to unmarshal the minified `1e3` json numbers in the request json into `int64` struct fields. So I'm looking for a way to minify json with out modifying the numbers.

Does that make sense?

Fix patch:
diff --git a/README.md b/README.md
index d909f8619f..10659a9370 100644
--- a/README.md
+++ b/README.md
@@ -303,6 +303,7 @@ The JSON minifier only removes whitespace, which is the only thing that can be l
 Options:
 
 - `Precision` number of significant digits to preserve for numbers, `0` means no trimming
+- `KeepNumbers` do not minify numbers if set to `true`, by default numbers will be minified
 
 ## SVG
 
diff --git a/json/json.go b/json/json.go
index 2fc5f0cf71..4ccbfde253 100644
--- a/json/json.go
+++ b/json/json.go
@@ -23,7 +23,8 @@ var DefaultMinifier = &Minifier{}
 
 // Minifier is a JSON minifier.
 type Minifier struct {
-	Precision int // number of significant digits
+	KeepNumbers bool // do not minify numbers if set to true
+	Precision   int  // number of significant digits
 }
 
 // Minify minifies JSON data, it reads from r and writes to w.
@@ -61,7 +62,7 @@ func (o *Minifier) Minify(_ *minify.M, w io.Writer, r io.Reader, _ map[string]st
 		}
 		skipComma = gt == json.StartObjectGrammar || gt == json.StartArrayGrammar
 
-		if 0 < len(text) && ('0' <= text[0] && text[0] <= '9' || text[0] == '-') {
+		if !o.KeepNumbers && 0 < len(text) && ('0' <= text[0] && text[0] <= '9' || text[0] == '-') {
 			text = minify.Number(text, o.Precision)
 			if text[0] == '.' {
 				w.Write(zeroBytes)


Test patch:
diff --git a/json/json_test.go b/json/json_test.go
index 3e35d1074b..42917a12d4 100644
--- a/json/json_test.go
+++ b/json/json_test.go
@@ -38,6 +38,35 @@ func TestJSON(t *testing.T) {
 	}
 }
 
+func TestJSON_IgnoreNumbers(t *testing.T) {
+	jsonTests := []struct {
+		json     string
+		expected string
+	}{
+		{"", ""},
+		{"{ \"a\": [1, 2] }", "{\"a\":[1,2]}"},
+		{"[{ \"a\": [{\"x\": null}, true] }]", "[{\"a\":[{\"x\":null},true]}]"},
+		{"{ \"a\": 1, \"b\": 2 }", "{\"a\":1,\"b\":2}"},
+		{"{ \"a\": 1           , \"b\": 2 }", "{\"a\":1,\"b\":2}"},
+		{"1.3e1", "1.3e1"},
+		{"1E+03", "1E+03"},
+		{"0.1", "0.1"},
+		{"-0.1", "-0.1"},
+		{"1.0", "1.0"},
+		{"10000", "10000"},
+	}
+	m := Minifier{KeepNumbers: true}
+	for _, tt := range jsonTests {
+		t.Run(tt.json, func(t *testing.T) {
+			r := bytes.NewBufferString(tt.json)
+			w := &bytes.Buffer{}
+			err := m.Minify(nil, w, r, nil)
+			test.Minify(t, tt.json, err, w.String(), tt.expected)
+		})
+	}
+
+}
+
 func TestReaderErrors(t *testing.T) {
 	r := test.NewErrorReader(0)
 	w := &bytes.Buffer{}