andrew-m-c / go.jsonvalue Goto Github PK
View Code? Open in Web Editor NEWQuick Solution with Unstructured JSON data
License: Other
Quick Solution with Unstructured JSON data
License: Other
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
type inner struct {
Name string
}
type outer struct {
*inner
Age int
}
person := &outer{}
person.inner = &inner{}
person.Name = "Andrew"
s := jsonvalue.New(person).MustMarshalString()
fmt.Println(s)
}
Got:
panic: reflect: call of reflect.Value.NumField on ptr Value
goroutine 1 [running]:
reflect.flag.mustBe(...)
/usr/local/go-faketime/src/reflect/value.go:221
reflect.Value.NumField({0x4c8b80?, 0xc000014300?, 0x7fb6d6408600?})
/usr/local/go-faketime/src/reflect/value.go:1963 +0x8d
github.com/Andrew-M-C/go%2ejsonvalue.parseStructFieldValue({0x4c8b80?, 0xc000014300?, 0x7fb6d6403700?}, {{0x4c2865, 0x5}, {0x4c22eb, 0x4}, {0x50bc60, 0x4c8b80}, {0x0, ...}, ...}, ...)
/tmp/gopath2056463688/pkg/mod/github.com/!andrew-!m-!c/[email protected]/import_export.go:345 +0x3da
github.com/Andrew-M-C/go%2ejsonvalue.parseStructValue({0x4d4560?, 0xc000014300?, 0x0?}, {0xe0?, 0x61?, 0x5d?})
/tmp/gopath2056463688/pkg/mod/github.com/!andrew-!m-!c/[email protected]/import_export.go:321 +0x1d5
github.com/Andrew-M-C/go%2ejsonvalue.parsePtrValue({0x4c8bc0?, 0xc000014300?, 0x0?}, {0x8?, 0x32?, 0xa8?})
/tmp/gopath2056463688/pkg/mod/github.com/!andrew-!m-!c/[email protected]/import_export.go:270 +0x12a
github.com/Andrew-M-C/go%2ejsonvalue.Import({0x4c8bc0, 0xc000014300}, {0x0?, 0x0?, 0x0?})
/tmp/gopath2056463688/pkg/mod/github.com/!andrew-!m-!c/[email protected]/import_export.go:34 +0xfd
github.com/Andrew-M-C/go%2ejsonvalue.New(...)
/tmp/gopath2056463688/pkg/mod/github.com/!andrew-!m-!c/[email protected]/new.go:19
main.main()
/tmp/sandbox2041220080/prog.go:24 +0x99
有这样一个包含正则的json在解析时报错:
{"regexp": "^[a-zA-Z\d-]{3,5}$"}
这种情况应该怎么处理呢?
==========
{"regexp": "^[a-zA-Z\\d-]{3,5}$"}
呃,多加一个反斜杠时可以解析的。
v1.3.2
type req struct {
IDs []uint64 `json:"ids,omitempty"`
}
type data struct {
Req *req `json:"req,omitempty"`
}
d := data{}
d.Req = &req{}
d.Req.IDs = []uint64{0}
j, err := Import(d)
so(err, isNil)
s := j.MustMarshalString()
so(s, eq, `{"req":{"ids":[0]}}`)
panic: runtime error: invalid memory address or nil pointer dereference
archived: false,
对于这种值好像Marshal的时候会丢失.变成:
"archived": ,
请问是哪个option可以控制的吗
https://go.dev/play/p/JvqKX_bNZ25
package main
import (
"encoding/json"
"fmt"
"time"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
type st struct {
Time time.Time
}
func main() {
s := st{
Time: time.UnixMilli(1678268209123),
}
b, _ := json.Marshal(s)
str, _ := jsonvalue.New(s).MarshalString()
fmt.Println(string(b))
fmt.Println(str)
}
expected:
{"Time":"2023-03-08T09:36:49.123Z"}
{"Time":"2023-03-08T09:36:49.123Z"}
got:
{"Time":"2023-03-08T09:36:49.123Z"}
{"Time":{}}
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
txt := `{"source":"1024"}`
v := jsonvalue.MustUnmarshalString(txt)
src, _ := v.GetInt("source")
fmt.Println(src)
}
自己给自己提 bug,下一版本迭代修复
jsonvalue有escapehtml选项么
I'm glad to find the tool I want after searching for a long time.
Many companies outside China will add Chinese char inspection to the project because some stubborn users think that Chinese products are not reliable enough.
如题
package main
import (
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
raw := `{"number":""}`
n := jsonvalue.MustUnmarshalString(raw)
fmt.Println(n.MustGet("number").Int())
}
执行会报错
panic: runtime error: index out of range [0] with length 0
这个是设计就是如此还是非预期错误?
I'm glad to find the tool I want after searching for a long time.
Many companies outside China will add Chinese char inspection to the project because some stubborn users think that Chinese products are not reliable enough.
Please let us avoid this risk before we change their minds.
package main
import (
"encoding/json"
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
m := map[string]interface{}{
"F32": float32(0.023),
"F64": float64(0.023),
}
b, _ := json.Marshal(m)
v, _ := jsonvalue.Import(m)
fmt.Println(string(b))
fmt.Println(v.MustMarshalString())
}
expected:
{"F32":0.023,"F64":0.023}
{"F32":0.023,"F64":0.023}
actual:
{"F32":0.023,"F64":0.023}
{"F32":0.023000000044703484,"F64":0.023}
version: v1.3.1
下一版 feature
首先这个库真的很好用,很顺手。
有一点在使用中显得比较繁琐的事情,就是对于默认 Option 无法满足需求的情况下,每次都要在 Marshal/Unmarshal 时打一堆选项。
有没有可能提供一个接口修改默认的 Option,这样只需要设置一次 Option。
package main
import (
"encoding/json"
"fmt"
jsonvalue "github.com/Andrew-M-C/go.jsonvalue"
)
type myStruct struct {
Ext *jsonvalue.V `json:"ext"`
}
func (s myStruct) MarshalJSON() ([]byte, error) {
w := myStructWrapper(s)
return jsonvalue.New(w).Marshal(jsonvalue.OptSetSequence())
}
type myStructWrapper myStruct
func main() {
st := myStruct{
Ext: jsonvalue.NewObject(),
}
st.Ext.SetString("1000").At("A")
st.Ext.SetString("2000").At("B")
const expected = `{"ext":{"A":"1000","B":"2000"}}`
for i := range 2000 {
b, _ := json.Marshal(st)
if string(b) != expected {
fmt.Println(string(b))
fmt.Println(expected)
panic(fmt.Sprintf("%d - diff!", i))
}
}
}
func Test_JsonVale(t *testing.T) { var m = map[string]interface{}{ "abc/123": "123", } v := jsonvalue.NewObject(m) s, _ := v.MarshalString() t.Log(s) }
expect {"abc/123":"123"}
get {"abc\/123":"123"}
标准 JSON 中不支持 NaN 和 Inf,但是有些场景可能需要,因此提一个 issue,下一版本支持
v.SetInt(9).At("result")
v.SetString("测试").At("result2")
v.SetString("http://abcd.com").At("result3")
ss := v.MustMarshalString(jsonvalue.OptEscapeHTML(false))
fmt.Println(ss)
实际结果:
{"result":9,"result2":"\u6D4B\u8BD5","result3":"http://abcd.com"
期待结果:
{"result":9,"result2":"测试","result3":"http://abcd.com"}
怎么处理?
在GIN里会使用以下代码对返回值进行赋值
c.JSON(http.StatusOK, gin.H{ "code": "A00000", "msg": "Operation Success", "data": gin.H{ "record": map[string]interface{}{}, }, })
这里的gin.H是一个map
type H map[string]interface{}
尝试了很久,都没有能够使用jsonvalue转换为这个格式,想请教下如何能做到呢?
顺便这库很好用,点个赞~
希望不要像fastjson那样出漏洞就好~
package main
import (
"fmt"
"log"
"strings"
"github.com/Andrew-M-C/go.jsonvalue"
)
func main() {
fmt.Println("Hello World!")
strJson := []byte(`{"tunnels":[{"name":"command_line","uri":"/api/tunnels/command_line","public_url":"https://11111.ngrok.io","proto":"https","config":{"addr":"http://localhost:11111","inspect":true},"metrics":{"conns":{"count":1,"gauge":0,"rate1":5.456067032277228e-19,"rate5":0.0000016821504265361616,"rate15":0.00008846097772300972,"p50":8287268034,"p90":8287268034,"p95":8287268034,"p99":8287268034},"http":{"count":5,"rate1":2.5535363027836646e-18,"rate5":0.000008299538128664852,"rate15":0.0004403445395661658,"p50":427625,"p90":600127,"p95":600127,"p99":600127}}},{"name":"command_line (http)","uri":"/api/tunnels/command_line%20%28http%29","public_url":"http://11111.ngrok.io","proto":"http","config":{"addr":"http://localhost:11111","inspect":true},"metrics":{"conns":{"count":0,"gauge":0,"rate1":0,"rate5":0,"rate15":0,"p50":0,"p90":0,"p95":0,"p99":0},"http":{"count":0,"rate1":0,"rate5":0,"rate15":0,"p50":0,"p90":0,"p95":0,"p99":0}}}],"uri":"/api/tunnels"}`)
j, err := jsonvalue.Unmarshal(strJson)
if err != nil {
log.Fatalf("Failed jsonvalue.Unmarshal : %s", err)
return
}
bb, _ := j.GetString("tunnels", 0, "proto")
cc, _ := j.GetString("tunnels", 0, "public_url")
if "https" == bb {
cc = strings.Replace(cc, "https://", "", -1)
} else if "http" == bb {
cc = strings.Replace(cc, "http://", "", -1)
}
fmt.Println(string(cc))
}
错误为: unrecognized raw text, invalid character \u0065 at Position 246
继续在 master 分支支持
注释、文档例程是否和实际代码有些出入。实际代码返回了2个参数,文档和注释看起来是只返回1个参数。
Lines 141 to 146 in a326e59
go.jsonvalue/docs/zh-cn/02_quick_start.md
Lines 33 to 37 in a326e59
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.