Code Monkey home page Code Monkey logo

jingo's People

Contributors

humbertzhang avatar ixuzhi avatar jimhickinbottom avatar kungfusheep avatar nicholas-wright-bjss avatar petemcmahon365 avatar variar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jingo's Issues

does not handle nil pointer to member struct

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()
}

expect raw []byte to be encoded as is

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}}

Use single encoder for different structs

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?

support time.Time serialization

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 testing suite.

Expand the current set of tests to include more edge cases and some differently shaped datasets for benchmarks.

Strings with `\` `"` are not escaped properly.

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?

How to properly create/use a SliceEncoder?

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.