Comments (35)
Try
b, err := oj.JSON(data)
or if you wanted indented output
b, err := oj.JSON(data, 2)
from ojg.
b, err := oj.JSON(data)
is wrong because it doesn't pass any error that can happen :-(
And the b suggest a []byte , but it's a string :-(
So it is not really compatible, but "sort of".
Any other suggestions?
from ojg.
You are correct. I was rushing out the door this morning. My apologies. The string vs []bytes is simply fixed with a []byte(s)
. Oj attempts to alway generate JSON output so things that can not be converted to JSON such as a chan are encoded as a string representation of the value.
Is this more in line with what you are looking for?
b := []byte(oj.JSON(data))
from ojg.
Thanks for answering btw :-)
I came up with this myself, but I'm kind of: what happens if there's an error?
the normal marshaller would tell me this. and the oj.JSON just swallows it :-(
from ojg.
The only errors that could occur are encountering a type that can not be converted. Oj uses several approaches to assure everything can be converted even if it is just to a string. On the flip side, the gen
package enforces JSON generation using compile time type checking.
Do you see a need for some kind of more strict conversion to JSON that would fail when ever a non-convertible type is encountered? Basically fail instead of using a string representation of the value.
from ojg.
I never had a situation where it failed. But when it does, I will know ;-)
from ojg.
Under what condition would you expect a failure to occur?
from ojg.
I created a branch called 'marshal' that has an oj.Marshal()
function. If that is what you are looking for let me know and I'll release.
from ojg.
This is my code:
type testModel struct {
Field1 string
Field2 string
}
data := make(map[string]interface{}, 2)
data["success"] = true
model := testModel{}
model.Field1 = "F1"
model.Field2 = "F2"
data["record"] = model
b, err := oj.Marshal(data)
it gives me an error:
testModel can not be encoded as a JSON element
I think it has something todo with Opt.strict?
from ojg.
That's why I asked the question about what would be expected. I took the approach that unless the type implemented the Simplifier interface or was primitive it would fail unless an optional create key was provided. Based on your response I take it you think reflection should be used even without a create key. Is that true?
from ojg.
When I do the same via tje encoding/json it returns everything.
{"success":true,"record":{"Field1":"F1","Field2":"F2"}}
So I kind of expected to have thew same. If this is done via reflection in your library, that's fine by me.
Perhaps this could be optional, so other users don't have to use this.
But I was wondering what speed is left over after doing so.
I'm willing to test this, but have to eat soon :-) So don't hurry, there are more days in this year ;-)
from ojg.
I'll make a change to not require the create type option for reflection.
I would not expect a significant change is performance.
from ojg.
Ok, new version pushed.
from ojg.
The fields are there :-) but... lowercase in the json result :-(
(Which means it's still not compatible 100%)
I did 5 benchmark tests and here's the average of those 5.
Benchmark_PresentJSON-12. 991640 1148 ns/op 696 B/op 11 allocs/op
Benchmark_PresentJSONoj-12. 12648212 978 ns/op 989 B/op 8 allocs/op
So it is faster for sure :-)
from ojg.
The current behaviour for Oj is to convert the names to camelcase starting with a lower case letter. That tests to be the most common style convention for JSON from what I have seen. It you would like some other option such as no doing the conversion that would be an easy addition. I would prefer not to use the field decorations or tags a that forced lookup for each does take a toll and Oj provides alternatives to that with the Simplifier interface.
from ojg.
I would keep the same output as the standard JSON would give you.
So if the struct is like this:
type Todo struct {
ID string `json:"id"`
Label string `json:"label" validate:"required,min=3"`
Comment string `json:"comment" validate:"min=2"`
DueDate string `json:"duedate"`
}
it will use the name 'id' for the ID field, but if it is like this:
Type Todo struct {
ID string
Label string `json:"label" validate:"required,min=3"`
Comment string `json:"comment" validate:"min=2"`
DueDate string `json:"duedate"`
}
it will use ID for the ID field
from ojg.
Where Oj excels is on parsing and the use of JSON Path. Writing in either the golang json package or with Oj will not be that much difference. I can add a full compatible mode but it really isn't intended to be a drop in replace for the json package. Give me a day or two.
from ojg.
The marshal branch now includes an option to use the json annotation tags.
from ojg.
Ok, I somehow expected a library that was fully compatible and amazingly fast. In order to compare this again the standard way, is if it is compatible ;-)
from ojg.
Misspelled 'now' as 'not' above. Fixed. The marshal branch now includes an option to use the json annotation tags.
from ojg.
It is fuzzy to me how to get this to work. And perhaps unnecessary:
Can I still get a result where the fieldname of a struct is part of the json result?
Like: {"success":true,"record":{"Field1":"F1","Field2":"F2"}}
In the write test, I've tried to add 2 tests (but don't know if the options are meant for my purpose):
{value: &Dummy{Val: 3}, expect: `{"Val":3}`, options: &oj.Options{Sort: true, NoReflect: false}},
{value: &Dummy{Val: 3}, expect: `{"Val":3}`, options: &oj.Options{Sort: true, UseTags: true}},
from ojg.
So you would like to see exact field names without using the field tags, is that right?
from ojg.
I think that is true, unless there's a json annotation tag for a field, in that case it should be that tag's field.
This is how the standard encoding works.
from ojg.
I'll add another field to Options.
from ojg.
Okay, KeyExact option added to oj.Options.
from ojg.
My findings.
type Dummy struct {
Val int
}
// part of test set
{value: &Dummy{Val: 3}, expect: `{"Val":3}`, options: &oj.Options{KeyExact: true, UseTags: true}},
works as expected
type Dummy struct {
Val int `json:"value"`
}
// part of test set
{value: &Dummy{Val: 3}, expect: `{"value":3}`, options: &oj.Options{KeyExact: true, UseTags: true}},
However if I compare it to the standard library and run my benchmarks, I see that you library is now slower than the standard solution. I think this has to do with the .JSON returning a string instead of a []byte, which I have to correct afterwards to have it fully compatible.
from ojg.
oj.Marshal()
returns ([]byte, error)
just like json.Marshal()
. I would expect oj to be almost the same as the json package when using the field tags. For writing oj has a number of options no supported by the golang json package. Those options allow for faster writing when used. If you want the exact behaviour of the json package there really isn't a reason to use oj for writing. If you take advantage of the extra encoding and decoding features such as decoding interface fields then oj offers features not found in the json package.
from ojg.
Perhaps I misunderstood.
Can you show an example where I can have options doing the oj.Marshal()?
from ojg.
Sure. I added it to the example_test.go file as well.
func ExampleMarshal() {
type Valley struct {
Val int `json:"value"`
}
b, err := oj.Marshal(&Valley{Val: 3})
fmt.Printf("%v %s\n", err, b)
// Output: <nil> {"value":3}
}
from ojg.
type vally struct {
Val int
}
data := make(map[string]interface{}, 2)
data["success"] = true
model := vally{}
model.Val = 1
data["record"] = model
b, err := oj.Marshal(data)
output as string:
{"success":true,"record":{"val":1}}
Notice that val is still in small
from ojg.
I suspect you need to pull the latest. While you can provide options to the Marshal function, the defaults are now what you would expect from json.Marshal()
.
from ojg.
Benchmark_OutputJSONnormal-12 958072 1202 ns/op 702 B/op 11 allocs/op
Benchmark_OutputJSONoj-12 1000000 1022 ns/op 933 B/op 10 allocs/op
You succeeded :-)
from ojg.
I'll make a release tonight then.
from ojg.
released v1.3.0
from ojg.
okay to close?
from ojg.
Related Issues (20)
- 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
- Support Keyed and Indexed interfaces in `jp.Expr.Modify` HOT 1
- Script with regex fails HOT 3
- Negation operator is not working HOT 11
- The json hierarchy if too deep resulting in no matching HOT 12
- Consider implementing JSONPath HOT 9
- 0x24 ($) is not a valid escaped character HOT 9
- Inconsitent capitalization and loss of capitalization in pretty.JSON for structs HOT 4
- Cannot parse when properties start with "$" HOT 1
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.