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
When marshal with map[string]string, it gave me ">UNKNOWN/>" #48
Comments
I assume you want I'll look into it. There's not a quick fix, since the encoder doesn't use reflection. (It was originally written to re-encode values that were the result of calling mxj.NewMapXml().) |
@clbanning |
Addressed with release v1.8. (Note: also handles map[int]string values, etc.) |
Well done, I pulled the latest codes and this issue was gone, thanks a lot. But hey I noticed that you solved this using reflection, compulsively convert any if reflect.ValueOf(value).Kind() == reflect.Map {
if _, ok := value.(map[string]interface{}); !ok {
val := make(map[string]interface{})
keys := reflect.ValueOf(value).MapKeys()
vv := reflect.ValueOf(value)
for _, k := range keys {
val[fmt.Sprint(k)] = vv.MapIndex(k).Interface()
}
value = val
}
} Is it better have a original type check, and using type assertion instead of reflection? |
First, note that only map values that are NOT map[string]interface{} are processed -
Using type check logic:
requires at least similar work to be performed (under the covers) for map values and, I suspect, more processing of non-map values. In general, users of this package are encoding Map values that were decoded from it or are dealing with simple non-nested Map values they've created. So lots, if not most, values - the 'value' argument passed to But I'm not sure the overhead for type assertion - I think it's minimal, since it must first check that the assertion can be made, then just creates a new 'value' but doesn't create a new hash map itself. (Note: map values are like slices and pointers in that regard.) But just in case the compiler isn't smart enough to just perform the type check and not create a new value, I've changed that logic from performing a type assertion to a type check. |
I looked into the codes in detail, yes you're right. Thanks for your detailed explanation, and also your great project. |
Hi there, mxj is a great project and I've been using it in my gf project in XML marshal/unmarshal. It worked quite well with
map[string]interface{}
type, but when the parameter contains other map type (eg:map[string]string
/map[string]int
etc...), it failed working.Here's the simple codes to reproduce this issue:
It's supposed to give me
{"m":{"k":"v"}}
, but actually I got>UNKNOWN/>
.Would you please take a look, and kindly give me a nice feedback, thanks!
The text was updated successfully, but these errors were encountered: