-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support non-strict mode to decode object to map when unregistered #309
Conversation
Codecov Report
@@ Coverage Diff @@
## master #309 +/- ##
==========================================
- Coverage 70.69% 70.44% -0.26%
==========================================
Files 27 27
Lines 3027 3116 +89
==========================================
+ Hits 2140 2195 +55
- Misses 661 687 +26
- Partials 226 234 +8
Continue to review full report at Codecov.
|
@wongoo // register pojo again, so that there are class definitions, which are used to encode map to object.
/**
Can these be omited.
**/
RegisterPOJO(name)
RegisterPOJO(person)
RegisterPOJO(worker1)
RegisterPOJO(&worker1.Jobs[0])
// encode the map to object again, note the worker2 is a map.
e = NewEncoder()
err = e.Encode(worker2)
if err != nil {
t.Error(err)
t.FailNow()
} |
@LaurenceLiZhixin yes, we can encode a map directly, I will try to implement it. |
@LaurenceLiZhixin I implement build class definition from map keys, and add a unit test: func TestEncodeMapToObject(t *testing.T) {
name := &UserName{
FirstName: "John",
LastName: "Doe",
}
RegisterPOJO(name)
// note: the first letter of the keys MUST lowercase.
m := map[string]interface{}{
"firstName": "John",
"lastName": "Doe",
}
e := NewEncoder()
encErr := e.EncodeMapAsClass(name.JavaClassName(), m)
if encErr != nil {
t.Error(encErr)
t.FailNow()
}
res := mustDecodeObject(t, e.Buffer())
assert.True(t, reflect.DeepEqual(name, res))
// note: the map contains the class key.
m = map[string]interface{}{
ClassKey: name.JavaClassName(),
"firstName": "John",
"lastName": "Doe",
}
// try to encode again
e = NewEncoder()
encErr = e.EncodeMapClass(m)
if encErr != nil {
t.Error(encErr)
t.FailNow()
}
res = mustDecodeObject(t, e.Buffer())
assert.True(t, reflect.DeepEqual(name, res))
} |
I have verified this feature locally, and it works fine! |
What this PR does:
Which issue(s) this PR fixes:
Fixes #308
Special notes for your reviewer:
Does this PR introduce a user-facing change?:
example: