Skip to content
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

Always skip unexported fields when encoding #175

Merged
merged 1 commit into from
Sep 19, 2017

Conversation

olegshaldybin
Copy link
Contributor

Skip creating encoders for unexported fields. They are not participating
in JSON marshaling anyway. This allows using unexported fields of
non-marshalable types in structs.

As a side-effect of this change it's no longer possible to marshal
unexported JSON fields by adding a custom type extenstion. It seems this
is desired behavior since it matches standard library and jsoniter
already disallows json:"-" fields from participating in custom
extensions.

Fixes #174.

Skip creating encoders for unexported fields. They are not participating
in JSON marshaling anyway. This allows using unexported fields of
non-marshalable types in structs.

As a side-effect of this change it's no longer possible to marshal
unexported JSON fields by adding a custom type extenstion. It seems this
is desired behavior since it matches standard library and jsoniter
already disallows `json:"-"` fields from participating in custom
extensions.

Fixes json-iterator#174.
@olegshaldybin
Copy link
Contributor Author

@taowen I see there are some tests that use custom extension to marshal/unmarshal private fields... not sure this is a good pattern but if you want to keep that behavior then we need a slightly more advanced fix for #174, maybe adding an ignored flag for the fields of unsupported types in the struct descriptor.

@taowen taowen merged commit 1f58120 into json-iterator:master Sep 19, 2017
@taowen
Copy link
Contributor

taowen commented Sep 19, 2017

if field is unexported, the field names will be empty slice. field names are used to decode or encode one go field from multiple json field. if is empty, meaning it is not present in json.

@ghost
Copy link

ghost commented Dec 22, 2021

Feature request: we can custom define the unsupport value field,
json.setUnsupportFieldValue("null");

or
json.setUnspportFieldValue("")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

如果结构体中带有time.Timer类型的字段,无论是否可导出都无法Marshal
2 participants