mirror of
https://github.com/daeuniverse/dae.git
synced 2025-07-19 20:29:20 +07:00
fix(vmess): bool fuzzy parsing for sharing link
This commit is contained in:
51
common/json/fuzzy_decoder.go
Normal file
51
common/json/fuzzy_decoder.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
* Copyright (c) 2023, daeuniverse Organization <dae@v2raya.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
package json
|
||||||
|
|
||||||
|
import (
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type FuzzyBoolDecoder struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (decoder *FuzzyBoolDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) {
|
||||||
|
valueType := iter.WhatIsNext()
|
||||||
|
switch valueType {
|
||||||
|
case jsoniter.NumberValue:
|
||||||
|
*((*bool)(ptr)) = iter.ReadFloat64() != 0
|
||||||
|
case jsoniter.StringValue:
|
||||||
|
str := iter.ReadString()
|
||||||
|
switch str {
|
||||||
|
case "", "0":
|
||||||
|
*((*bool)(ptr)) = false
|
||||||
|
default:
|
||||||
|
*((*bool)(ptr)) = true
|
||||||
|
}
|
||||||
|
case jsoniter.BoolValue:
|
||||||
|
*((*bool)(ptr)) = iter.ReadBool()
|
||||||
|
// In order to stay consistent with the other decoders here, leaving arrays and objects out for now.
|
||||||
|
// case jsoniter.ObjectValue:
|
||||||
|
// iter.Skip()
|
||||||
|
// *((*bool)(ptr)) = true
|
||||||
|
// case jsoniter.ArrayValue:
|
||||||
|
// var nonEmptyArray bool
|
||||||
|
// iter.ReadArrayCB(
|
||||||
|
// func(*jsoniter.Iterator) bool {
|
||||||
|
// iter.Skip()
|
||||||
|
// nonEmptyArray = true
|
||||||
|
// return true
|
||||||
|
// },
|
||||||
|
// )
|
||||||
|
// *((*bool)(ptr)) = nonEmptyArray
|
||||||
|
case jsoniter.NilValue:
|
||||||
|
iter.Skip()
|
||||||
|
*((*bool)(ptr)) = false
|
||||||
|
default:
|
||||||
|
iter.ReportError("FuzzyBoolDecoder", "not number, string or bool")
|
||||||
|
}
|
||||||
|
}
|
3
main.go
3
main.go
@ -9,6 +9,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/daeuniverse/dae/cmd"
|
"github.com/daeuniverse/dae/cmd"
|
||||||
|
"github.com/daeuniverse/dae/common/json"
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
"github.com/json-iterator/go/extra"
|
"github.com/json-iterator/go/extra"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -16,6 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
jsoniter.RegisterTypeDecoder("bool", &json.FuzzyBoolDecoder{})
|
||||||
extra.RegisterFuzzyDecoders()
|
extra.RegisterFuzzyDecoders()
|
||||||
|
|
||||||
http.DefaultClient.Timeout = 30 * time.Second
|
http.DefaultClient.Timeout = 30 * time.Second
|
||||||
|
Reference in New Issue
Block a user