From 4f29ebf13b3e249fabb8d6be6ad34ca965f2d0c6 Mon Sep 17 00:00:00 2001 From: baijinping Date: Wed, 28 Jun 2023 19:46:43 +0800 Subject: [PATCH 1/3] feat: add Valid() api --- api.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api.go b/api.go index 809d5ec7b..fa738f21d 100644 --- a/api.go +++ b/api.go @@ -184,3 +184,8 @@ func Get(src []byte, path ...interface{}) (ast.Node, error) { func GetFromString(src string, path ...interface{}) (ast.Node, error) { return ast.NewSearcher(src).GetByPath(path...) } + +// Valid reports whether data is a valid JSON encoding. +func Valid(data []byte) bool { + return ConfigDefault.Valid(data) +} From d777404e608abb0115bde736682cbea726723f51 Mon Sep 17 00:00:00 2001 From: baijinping Date: Wed, 28 Jun 2023 19:48:14 +0800 Subject: [PATCH 2/3] test: add api Valid() test --- api_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 api_test.go diff --git a/api_test.go b/api_test.go new file mode 100644 index 000000000..a5f22deb1 --- /dev/null +++ b/api_test.go @@ -0,0 +1,35 @@ +package sonic + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestValid(t *testing.T) { + require.False(t, Valid(nil)) + + testCase := []struct { + data string + expected bool + }{ + {``, false}, + {`s`, false}, + {`{`, false}, + {`[`, false}, + {`[1,2`, false}, + {`{"so":nic"}`, false}, + + {`null`, true}, + {`""`, true}, + {`1`, true}, + {`"sonic"`, true}, + {`{}`, true}, + {`[]`, true}, + {`[1,2]`, true}, + {`{"so":"nic"}`, true}, + } + for _, tc := range testCase { + require.Equal(t, tc.expected, Valid([]byte(tc.data)), tc.data) + } +} From d3fc49d695f4c141241a797e801a521e76a118f7 Mon Sep 17 00:00:00 2001 From: Yi Duan Date: Wed, 28 Jun 2023 21:39:19 +0800 Subject: [PATCH 3/3] reformat --- api_test.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/api_test.go b/api_test.go index a5f22deb1..04ec72799 100644 --- a/api_test.go +++ b/api_test.go @@ -1,9 +1,25 @@ +/* + * Copyright 2021 ByteDance Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package sonic import ( - "testing" + `testing` - "github.com/stretchr/testify/require" + `github.com/stretchr/testify/require` ) func TestValid(t *testing.T) {