bet365 / jingo Goto Github PK
View Code? Open in Web Editor NEWThis package provides the ability to encode golang structs to a buffer as JSON very quickly.
License: Apache License 2.0
This package provides the ability to encode golang structs to a buffer as JSON very quickly.
License: Apache License 2.0
causes nil pointer exception
type testStruct1 struct {
StrVal string `json:"str1"`
IntVal int `json:"int1"`
}
type testStruct0 struct {
StrVal string `json:"str0"`
IntVal int `json:"int0"`
StructVal testStruct1 `json:"struct0v"`
StructPtr *testStruct1 `json:"struct0p"`
}
var jingoEnc = jingo.NewStructEncoder(testStruct0{})
func TestJsoniterEnc(t *testing.T) {
buf := jingo.NewBufferFromPool()
v := testStruct0{
StrVal:"s0",
IntVal:10,
StructVal:testStruct1{
StrVal:"s11",
IntVal:11,
},
}
jingoEnc.Marshal(&v, buf)
buf.ReturnToPool()
}
Hello, thanx for the quick fix for nil struct member.
Got another issue
type testStruct2 struct {
Raw []byte `json:"raw"`
}
func TestJsoniterEnc2(t *testing.T) {
var jingoEnc2 = jingo.NewStructEncoder(testStruct2{})
buf := jingo.NewBufferFromPool()
v := testStruct2{
Raw:[]byte(`{"mapKey1":1,"mapKey2":2}`),
}
jingoEnc2.Marshal(&v, buf)
fmt.Print(buf)
buf.ReturnToPool()
}
out:
{"raw":[123,34,109,97,112,75,101,121,49,34,58,49,44,34,109,97,112,75,101,121,50,34,58,50,125]}
while obvious to get:
{"raw":{"mapKey1":1,"mapKey2":2}}
Hi,
I have a feature request. So far you provide encoder for individual struct and then use it for serialization. But what if my code contains hundred of structs (it is not uncommon when you work with large DBs), then your design forces me to create the same amount of encoders, while standard library I still use single json.Marshal
method for all my structs. Can code be generalized to use any struct passed to single encoder. May be this part can be added when generics can be in place?
this package cannot decode json ????????
jingo currently can't serialize time.Time. By default it will serialize to {}
, and using stringify it won't give a proper ISO8601 (?) representation.
I've attempted to work around this using a fallback to the json.Marshaller interface (see #13 ) but explicitly supporting time.Time is more efficient and probably as effective.
Expand the current set of tests to include more edge cases and some differently shaped datasets for benchmarks.
When a serialisation struct has a *time.Time
field with nil
value, it is generating JSON with "null"
instead of just null
struct:
type Data struct {
Datetime *time.Time `json:"Datetime"`
}
JSON:
{
"Datetime":"null"
}
as title......
An example of some test code:
type StructWithEscapes struct {
String string `json:"str"`
StringArray []string `json:"str-array"`
}
func Test_StructWithEscapes(t *testing.T) {
es := StructWithEscapes{
String: "one\\two\\,three\"",
StringArray: []string{"one\\two", "three\\,four"},
}
wantJSON := `{"str":"one\\two\\,three\"","str-array":["one\\two","three\\,four"]}`
var enc = NewStructEncoder(StructWithEscapes{})
buf := NewBufferFromPool()
enc.Marshal(&es, buf)
resultJSON := buf.String()
// Ensure JSON is valid.
if !json.Valid([]byte(resultJSON)) {
t.Errorf("Not valid JSON:" + resultJSON)
}
// Compare result
if resultJSON != wantJSON {
t.Errorf("Test_StructWithScapes Failed: want JSON:" + wantJSON + " got JSON:" + resultJSON)
}
}
Strings with \
or "
will produce incorrect JSON strings. Is there a way ptrStringToBuf
or any higher level function can escape these properly?
I feel rather dumb for not getting this to work, but since there are no relevant tests either, I'm about togive up.
package main
import (
"fmt"
"github.com/bet365/jingo"
)
type C struct {
X string `json:"x"`
}
var SEncoder = jingo.NewSliceEncoder([]C{C{}})
func TestSlice() {
buf2 := jingo.NewBufferFromPool()
defer buf2.ReturnToPool()
sdata := []C{C{"1"}}
SEncoder.Marshal(sdata, buf2)
fmt.Println(buf2.String())
}
func main() {
TestSlice()
}
No matter how I structire my slice: []*C{}
, []C{}
, []*C{&C{}}
, etc, I keep ending up with
panic: runtime error: growslice: cap out of range
Oddly enough, https://play.golang.org/p/Na5uQ-3l56d does seem to "not crash", but it doesn't produce the correct output.
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.