Signed-off-by: Jessica Frazelle <acidburn@docker.com>
This commit is contained in:
Jessica Frazelle 2015-08-27 11:06:00 -07:00
Родитель d62108ab70
Коммит 6e461eb70c
3 изменённых файлов: 60 добавлений и 5 удалений

Просмотреть файл

@ -1,7 +1,7 @@
From 731ff3aa5d2bdd992972cc006ac4ba9d223eee1f Mon Sep 17 00:00:00 2001
From: Tibor Vass <teabee89@gmail.com>
From ee929ab88d09e61a93cd041730adfedb1aa2c510 Mon Sep 17 00:00:00 2001
From: Tibor Vass <teabee89@gmail.com>
Date: Fri, 7 Aug 2015 12:49:35 -0700
Subject: [PATCH 1/2] Expose Encoder.Canonical() and MarshalCanonical() Handles
Subject: [PATCH 1/3] Expose Encoder.Canonical() and MarshalCanonical() Handles
lexicographic order in struct fields, rejects floating numbers and handles
strings as defined in http://wiki.laptop.org/go/Canonical_JSON except for
unicode normalization. IOW, only escaping allowed is " and \.

Просмотреть файл

@ -1,7 +1,7 @@
From 775457d4430a6972f090ae6c1c8bba6ded34a9c4 Mon Sep 17 00:00:00 2001
From d818992b918abae8c6a61a870cf21280d3fd7a98 Mon Sep 17 00:00:00 2001
From: Jessica Frazelle <acidburn@docker.com>
Date: Fri, 7 Aug 2015 12:51:13 -0700
Subject: [PATCH 2/2] add test for canonical float error
Subject: [PATCH 2/3] add test for canonical float error
Signed-off-by: Jessica Frazelle <acidburn@docker.com>
---

Просмотреть файл

@ -0,0 +1,55 @@
From d62108ab70036d21c6f30804f9b5e77602066f1d Mon Sep 17 00:00:00 2001
From: Tonis Tiigi <tonistiigi@gmail.com>
Date: Thu, 27 Aug 2015 10:08:51 -0700
Subject: [PATCH 3/3] Encode non-fatal floats as integers in canonical mode
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
---
canonical/json/encode.go | 8 +++++++-
canonical/json/encode_test.go | 14 ++++++++++++++
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/canonical/json/encode.go b/canonical/json/encode.go
index aaa79c2..655f1a6 100644
--- a/canonical/json/encode.go
+++ b/canonical/json/encode.go
@@ -526,7 +526,13 @@ func (bits floatEncoder) encode(e *encodeState, v reflect.Value, quoted bool) {
if math.IsInf(f, 0) || math.IsNaN(f) || (e.canonical && math.Floor(f) != f) {
e.error(&UnsupportedValueError{v, strconv.FormatFloat(f, 'g', -1, int(bits))})
}
- b := strconv.AppendFloat(e.scratch[:0], f, 'g', -1, int(bits))
+
+ var b []byte
+ if e.canonical {
+ b = strconv.AppendInt(e.scratch[:0], int64(f), 10)
+ } else {
+ b = strconv.AppendFloat(e.scratch[:0], f, 'g', -1, int(bits))
+ }
if quoted {
e.WriteByte('"')
}
diff --git a/canonical/json/encode_test.go b/canonical/json/encode_test.go
index cd41aff..2e42b4e 100644
--- a/canonical/json/encode_test.go
+++ b/canonical/json/encode_test.go
@@ -589,3 +589,17 @@ func TestCanonicalFloatError(t *testing.T) {
t.Errorf("want float error, got nil")
}
}
+
+func TestCanonicalFloatAsInt(t *testing.T) {
+ in := struct{ A float64 }{1234567}
+
+ b, err := MarshalCanonical(in)
+ if err != nil {
+ t.Fatalf("Marshal(%q): %v", in, err)
+ }
+ out := string(b)
+ expected := `{"A":1234567}`
+ if out != expected {
+ t.Errorf("Marshal(%q) = %#q, want %#q", in, out, expected)
+ }
+}
--
2.5.0