Skip to content

Commit

Permalink
Implement support for v1 json.Number (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsnet authored Dec 17, 2024
1 parent 94d5fea commit e98bb04
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions arshal_any.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"
"strconv"

"github.com/go-json-experiment/json/internal"
"github.com/go-json-experiment/json/internal/jsonflags"
"github.com/go-json-experiment/json/internal/jsonopts"
"github.com/go-json-experiment/json/internal/jsonwire"
Expand Down Expand Up @@ -71,6 +72,9 @@ func unmarshalValueAny(dec *jsontext.Decoder, uo *jsonopts.Struct) (any, error)
}
return makeString(xd.StringCache, val), nil
case '0':
if uo.Flags.Get(jsonflags.UnmarshalAnyWithRawNumber) {
return internal.RawNumberOf(val), nil
}
fv, ok := jsonwire.ParseFloat(val, 64)
if !ok && uo.Flags.Get(jsonflags.RejectFloatOverflow) {
return nil, newUnmarshalErrorAfter(dec, float64Type, strconv.ErrRange)
Expand Down
7 changes: 6 additions & 1 deletion arshal_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strconv"
"sync"

"github.com/go-json-experiment/json/internal"
"github.com/go-json-experiment/json/internal/jsonflags"
"github.com/go-json-experiment/json/internal/jsonopts"
"github.com/go-json-experiment/json/internal/jsonwire"
Expand Down Expand Up @@ -1629,7 +1630,11 @@ func makeInterfaceArshaler(t reflect.Type) *arshaler {
case '"':
v = newAddressableValue(stringType)
case '0':
v = newAddressableValue(float64Type)
if uo.Flags.Get(jsonflags.UnmarshalAnyWithRawNumber) {
v = addressableValue{reflect.ValueOf(internal.NewRawNumber()).Elem()}
} else {
v = newAddressableValue(float64Type)
}
case '{':
v = newAddressableValue(mapStringAnyType)
case '[':
Expand Down
7 changes: 7 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ type NotForPublicUse struct{}
// AllowInternalUse is passed from "json" to "jsontext" to authenticate
// that the caller can have access to internal functionality.
var AllowInternalUse NotForPublicUse

var (
// NewRawNumber returns new(jsonv1.Number).
NewRawNumber func() any
// RawNumberOf returns jsonv1.Number(b).
RawNumberOf func(b []byte) any
)
3 changes: 0 additions & 3 deletions v1/failing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ TestMarshalInvalidUTF8/#04
TestMarshalInvalidUTF8/#05
TestMarshalEmbeds
TestUnmarshal
TestUnmarshal/#07
TestUnmarshal/#13
TestUnmarshal/#14
TestUnmarshal/#15
TestUnmarshal/#16
TestUnmarshal/#17
TestUnmarshal/#18
TestUnmarshal/#19
TestUnmarshal/#21
TestUnmarshal/#23
TestUnmarshal/#30
TestUnmarshal/#32
TestUnmarshal/#35
Expand Down
13 changes: 13 additions & 0 deletions v1/inject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package json

import "github.com/go-json-experiment/json/internal"

// Inject functionality into v2 to properly handle v1 types.
func init() {
internal.NewRawNumber = func() any { return new(Number) }
internal.RawNumberOf = func(b []byte) any { return Number(b) }
}

0 comments on commit e98bb04

Please sign in to comment.