Giter Site home page Giter Site logo

support non empty interface about easyjson HOT 10 CLOSED

mailru avatar mailru commented on May 22, 2024 5
support non empty interface

from easyjson.

Comments (10)

rvasily avatar rvasily commented on May 22, 2024

Can you provide sample code?

from easyjson.

sreekanth-cb avatar sreekanth-cb commented on May 22, 2024
type MyInterface interface {
        Hello() string
}

type MyString string

func (ms MyString) Hello() string {
        return string(ms)
}

type TestObject struct {
                Field MyInterface
 }
func main() {
	obj := TestObject{}
	obj.Field = MyString("AAA")
	b, _ := json.Marshal(&obj)
	fmt.Println("OP->", string(b))
	
}

easyjson code generation fails here with error
interface type MyInterface not supported: only interface{} is allowed.
Can you please let me know whether easyjson will fix this api/service break with std json?

from easyjson.

mkabischev avatar mkabischev commented on May 22, 2024

It is impossible to do it without reflection, so I think the only way is to generate code that will run default (reflection-based) marshaller for this field.

from easyjson.

rvasily avatar rvasily commented on May 22, 2024

I think it possible to have just a little bit of reflection overhead in stdlib marshaler if MyString will have generaded marshallers with easyjson ( implements json.Marshaler interface )

from easyjson.

sparques avatar sparques commented on May 22, 2024

My preference would be if easyjson doesn't support something (such as a non-empty interface) fallback on the standard json.Marshal. There could be a switch to enable this behavior and the default can be off (for backwards compatibility sake).

from easyjson.

dgoodine avatar dgoodine commented on May 22, 2024

@GoWebProd Why was this closed? I think @deusnefum's suggestion is not unreasonable.

from easyjson.

dgoodine avatar dgoodine commented on May 22, 2024

It should also be noted that the error generated by easyjson give absolutely no context as to where the error occurred in the source file.

from easyjson.

GoWebProd avatar GoWebProd commented on May 22, 2024

@dgoodine Hello, issue closed because easyjson not supports non-empty interfaces due to the lack of the ability to use reflect lib. If you want to use interface in struct, please set type to interface{} (empty interface)

from easyjson.

dgoodine avatar dgoodine commented on May 22, 2024

Why can't easyjson use the reflect lib? At least just for those fields whose type is an interface.

from easyjson.

GoWebProd avatar GoWebProd commented on May 22, 2024

The task of easyjson is parsing and serialization quickly. Reflect does not allow this.

from easyjson.

Related Issues (20)

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.