Code Monkey home page Code Monkey logo

Comments (4)

ohler55 avatar ohler55 commented on June 6, 2024

Both converting a struct to a []byte or string is done using reflection or by having the struct implement an interface. There are some examples in the write unit tests. If they are not enough I'd be glad to add some more complex ones.

from ojg.

marcelloh avatar marcelloh commented on June 6, 2024

hmmm, I only want to generate json and write it later. Is there another way not using write?
(Reason is that i want to store the generated json in cache so it can skip everything when it's in the cache)

I have this as a test:

	data := make(map[string]interface{}, 2)
	data["success"] = true
	model := testModel{}
	model.Field1 = "F1"
	model.Field2 = "F2"
	data["record"] = model
	result := presenters.OutputJSONoj(resp, data)
	log.Printf("JSON_test.go:66 result %s\n", string(result))

And this is the output:
2020/10/16 09:00:12 JSON_test.go:66 result {"success":true,"record":"{F1 F2}"}
Which is missing the 2 fieldnames, which I thing are necessary in the frontend.

and with doing this "trick"

// the OutputJSONoj part
	var bbuf strings.Builder
	err := oj.Write(&bbuf, data)
	if err != nil {
		libError.FatalError(err)
	}

	b := []byte(bbuf.String())

this is slower than the normal json variant :-(

from ojg.

ohler55 avatar ohler55 commented on June 6, 2024

I'm not sure what you are trying to achieve with that. It would be far simpler to just use oj.JSON(data). In your example, strings.Builder will never return an error on Write(). You can check the golang code. If you just want to feel comfortable by checking for errors you can try the marshal branch I suggested in your other issue #25 . The oj.Marshal() function does return both a []byte and an error. An error is returned if you try to encode un-encodeable values such as a function or chan. That way you have a reason to check for errors.

from ojg.

ohler55 avatar ohler55 commented on June 6, 2024

I think this should be closed as well. Do you agree?

from ojg.

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.