Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
b76e512f
Unverified
Commit
b76e512f
authored
Jun 18, 2018
by
Mikhail Mazurskiy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Report parsing error in json serializer
parent
6d3f5b75
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
26 deletions
+55
-26
BUILD
...src/k8s.io/apimachinery/pkg/runtime/serializer/json/BUILD
+1
-0
json.go
...c/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go
+40
-22
json_test.go
....io/apimachinery/pkg/runtime/serializer/json/json_test.go
+14
-4
No files found.
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/BUILD
View file @
b76e512f
...
@@ -22,6 +22,7 @@ go_library(
...
@@ -22,6 +22,7 @@ go_library(
deps = [
deps = [
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/json-iterator/go:go_default_library",
"//vendor/github.com/json-iterator/go:go_default_library",
"//vendor/github.com/modern-go/reflect2:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer/recognizer:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer/recognizer:go_default_library",
...
...
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json.go
View file @
b76e512f
...
@@ -24,6 +24,7 @@ import (
...
@@ -24,6 +24,7 @@ import (
"github.com/ghodss/yaml"
"github.com/ghodss/yaml"
jsoniter
"github.com/json-iterator/go"
jsoniter
"github.com/json-iterator/go"
"github.com/modern-go/reflect2"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/schema"
...
@@ -68,43 +69,60 @@ type Serializer struct {
...
@@ -68,43 +69,60 @@ type Serializer struct {
var
_
runtime
.
Serializer
=
&
Serializer
{}
var
_
runtime
.
Serializer
=
&
Serializer
{}
var
_
recognizer
.
RecognizingDecoder
=
&
Serializer
{}
var
_
recognizer
.
RecognizingDecoder
=
&
Serializer
{}
func
init
()
{
type
customNumberExtension
struct
{
// Force jsoniter to decode number to interface{} via ints, if possible.
jsoniter
.
DummyExtension
decodeNumberAsInt64IfPossible
:=
func
(
ptr
unsafe
.
Pointer
,
iter
*
jsoniter
.
Iterator
)
{
}
switch
iter
.
WhatIsNext
()
{
case
jsoniter
.
NumberValue
:
func
(
cne
*
customNumberExtension
)
CreateDecoder
(
typ
reflect2
.
Type
)
jsoniter
.
ValDecoder
{
var
number
json
.
Number
if
typ
.
String
()
==
"interface {}"
{
iter
.
ReadVal
(
&
number
)
return
customNumberDecoder
{}
i64
,
err
:=
strconv
.
ParseInt
(
string
(
number
),
10
,
64
)
}
if
err
==
nil
{
return
nil
*
(
*
interface
{})(
ptr
)
=
i64
}
return
}
type
customNumberDecoder
struct
{
f64
,
err
:=
strconv
.
ParseFloat
(
string
(
number
),
64
)
}
if
err
==
nil
{
*
(
*
interface
{})(
ptr
)
=
f64
func
(
customNumberDecoder
)
Decode
(
ptr
unsafe
.
Pointer
,
iter
*
jsoniter
.
Iterator
)
{
return
switch
iter
.
WhatIsNext
()
{
}
case
jsoniter
.
NumberValue
:
// Not much we can do here.
var
number
jsoniter
.
Number
default
:
iter
.
ReadVal
(
&
number
)
*
(
*
interface
{})(
ptr
)
=
iter
.
Read
()
i64
,
err
:=
strconv
.
ParseInt
(
string
(
number
),
10
,
64
)
if
err
==
nil
{
*
(
*
interface
{})(
ptr
)
=
i64
return
}
f64
,
err
:=
strconv
.
ParseFloat
(
string
(
number
),
64
)
if
err
==
nil
{
*
(
*
interface
{})(
ptr
)
=
f64
return
}
}
iter
.
ReportError
(
"DecodeNumber"
,
err
.
Error
())
default
:
*
(
*
interface
{})(
ptr
)
=
iter
.
Read
()
}
}
jsoniter
.
RegisterTypeDecoderFunc
(
"interface {}"
,
decodeNumberAsInt64IfPossible
)
}
}
// CaseSensitiveJsonIterator returns a jsoniterator API that's configured to be
// CaseSensitiveJsonIterator returns a jsoniterator API that's configured to be
// case-sensitive when unmarshalling, and otherwise compatible with
// case-sensitive when unmarshalling, and otherwise compatible with
// the encoding/json standard library.
// the encoding/json standard library.
func
CaseSensitiveJsonIterator
()
jsoniter
.
API
{
func
CaseSensitiveJsonIterator
()
jsoniter
.
API
{
return
jsoniter
.
Config
{
config
:=
jsoniter
.
Config
{
EscapeHTML
:
true
,
EscapeHTML
:
true
,
SortMapKeys
:
true
,
SortMapKeys
:
true
,
ValidateJsonRawMessage
:
true
,
ValidateJsonRawMessage
:
true
,
CaseSensitive
:
true
,
CaseSensitive
:
true
,
}
.
Froze
()
}
.
Froze
()
// Force jsoniter to decode number to interface{} via int64/float64, if possible.
config
.
RegisterExtension
(
&
customNumberExtension
{})
return
config
}
}
// Private copy of jsoniter to try to shield against possible mutations
// from outside. Still does not protect from package level jsoniter.Register*() functions - someone calling them
// in some other library will mess with every usage of the jsoniter library in the whole program.
// See https://github.com/json-iterator/go/issues/265
var
caseSensitiveJsonIterator
=
CaseSensitiveJsonIterator
()
var
caseSensitiveJsonIterator
=
CaseSensitiveJsonIterator
()
// gvkWithDefaults returns group kind and version defaulting from provided default
// gvkWithDefaults returns group kind and version defaulting from provided default
...
...
staging/src/k8s.io/apimachinery/pkg/runtime/serializer/json/json_test.go
View file @
b76e512f
...
@@ -29,10 +29,11 @@ import (
...
@@ -29,10 +29,11 @@ import (
)
)
type
testDecodable
struct
{
type
testDecodable
struct
{
Other
string
Other
string
Value
int
`json:"value"`
Value
int
`json:"value"`
Spec
DecodableSpec
`json:"spec"`
Spec
DecodableSpec
`json:"spec"`
gvk
schema
.
GroupVersionKind
Interface
interface
{}
`json:"interface"`
gvk
schema
.
GroupVersionKind
}
}
// DecodableSpec has 15 fields. json-iterator treats struct with more than 10
// DecodableSpec has 15 fields. json-iterator treats struct with more than 10
...
@@ -242,6 +243,15 @@ func TestDecode(t *testing.T) {
...
@@ -242,6 +243,15 @@ func TestDecode(t *testing.T) {
},
},
},
},
},
},
// Error on invalid number
{
data
:
[]
byte
(
`{"kind":"Test","apiVersion":"other/blah","interface":1e1000}`
),
creater
:
&
mockCreater
{
obj
:
&
testDecodable
{}},
expectedGVK
:
&
schema
.
GroupVersionKind
{
Kind
:
"Test"
,
Group
:
"other"
,
Version
:
"blah"
},
errFn
:
func
(
err
error
)
bool
{
return
strings
.
Contains
(
err
.
Error
(),
`json_test.testDecodable.Interface: DecodeNumber: strconv.ParseFloat: parsing "1e1000": value out of range`
)
},
},
// Unmarshalling is case-sensitive
// Unmarshalling is case-sensitive
{
{
// "VaLue" should have been "value"
// "VaLue" should have been "value"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment