Skip to content

Commit ffbac60

Browse files
authored
Merge pull request #40 from lacroixthomas/bugfixes/fix-lossy-in64
Fix: handle lossy max int64
2 parents 17d7994 + 4c723fc commit ffbac60

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

v2/jsonpatch.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ func CreatePatch(a, b []byte) ([]Operation, error) {
7070
}
7171
var aI interface{}
7272
var bI interface{}
73-
err := json.Unmarshal(a, &aI)
74-
if err != nil {
73+
aDec := json.NewDecoder(bytes.NewReader(a))
74+
aDec.UseNumber()
75+
if err := aDec.Decode(&aI); err != nil {
7576
return nil, errBadJSONDoc
7677
}
77-
err = json.Unmarshal(b, &bI)
78-
if err != nil {
78+
bDec := json.NewDecoder(bytes.NewReader(b))
79+
bDec.UseNumber()
80+
if err := bDec.Decode(&bI); err != nil {
7981
return nil, errBadJSONDoc
8082
}
8183
return handleValues(aI, bI, "", []Operation{})
@@ -94,6 +96,11 @@ func matchesValue(av, bv interface{}) bool {
9496
if ok && bt == at {
9597
return true
9698
}
99+
case json.Number:
100+
bt, ok := bv.(json.Number)
101+
if ok && bt == at {
102+
return true
103+
}
97104
case float64:
98105
bt, ok := bv.(float64)
99106
if ok && bt == at {
@@ -212,7 +219,7 @@ func handleValues(av, bv interface{}, p string, patch []Operation) ([]Operation,
212219
if err != nil {
213220
return nil, err
214221
}
215-
case string, float64, bool:
222+
case string, float64, bool, json.Number:
216223
if !matchesValue(av, bv) {
217224
patch = append(patch, NewOperation("replace", p, bv))
218225
}

v2/jsonpatch_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var simpleD = `{"a":100, "b":200, "c":"hello", "d":"foo"}`
1616
var simpleE = `{"a":100, "b":200}`
1717
var simplef = `{"a":100, "b":100, "d":"foo"}`
1818
var simpleG = `{"a":100, "b":null, "d":"foo"}`
19+
var simpleH = `{"a":100, "b":200, "c":"hello", "d": 9223372036854775500}`
1920
var empty = `{}`
2021

2122
var arraySrc = `
@@ -859,6 +860,7 @@ func TestCreatePatch(t *testing.T) {
859860
{"Simple:OneAdd", simpleA, simpleD},
860861
{"Simple:OneRemove", simpleA, simpleE},
861862
{"Simple:VsEmpty", simpleA, empty},
863+
{"Simple:AddBigInt", simpleA, simpleH},
862864
// array types
863865
{"Array:Same", arraySrc, arraySrc},
864866
{"Array:BoolReplace", arraySrc, arrayDst},

0 commit comments

Comments
 (0)