Comments (4)
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.
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.
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.
I think this should be closed as well. Do you agree?
from ojg.
Related Issues (20)
- Working with a collection of JSONPaths simultaneously? HOT 2
- Does JsonPath supports escaping? HOT 13
- Option to keep order of keys HOT 16
- Does support ”-“ HOT 1
- Parse '1,2,3' should not succeed HOT 3
- Expr.String() doesn't escape strings HOT 6
- Question: Using jp.Set() to set non-existent slice indices HOT 2
- Maybe a regression on path filters on version 1.18.0 HOT 3
- support for "json" tag in struct elements HOT 4
- Single or Multiple HOT 6
- Provides the length functionality HOT 2
- asm.cond doesn't evaluate expressions HOT 4
- recursive descent on object, unexpected results HOT 7
- Add float write format option HOT 1
- Array type with windowing like functionality? HOT 2
- Panic on setting a nil value on JSONPath HOT 3
- '' is not a valid operation at 16 in $..book[[email protected]] HOT 9
- oj.Unmarshal fails on arrays HOT 4
- Can I get a Normalized Path in JP? (ietf) HOT 13
- Add jp.Expr.Locate() HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ojg.