Safe json.RawMessage and json.Number #233
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are two types in
encoding/json
that are special:json.RawMessage
is defined as a[]byte
and it is used to store unparsed JSON. When encountered in a struct, gofakeit generates a random array of bytes but that doesn't mean that it is a valid JSON, and it will fail when trying tojson.Marshal
the struct.json.Number
is defined as astring
and it is used to represent numeric types in JSON. When encountered in a struct, gofakeit generates a random string that cannot be represented as a number and it will fail when trying tojson.Marshal
the struct.This PR adds support for those two types in the
encoding/json
package ensuring that valid JSON values are generated by gofakeit by default and that the resulting struct can be marshalled to JSON. More precise output control is still available via struct tags.There are two commits that add stability and ensure reusing the Faker object in CSV, JSON, and XML. If you prefer that those two commits are moved to their own PR, I'm happy to extract them.
Other than that improvement to existing functionality, all the existing tests pass and no behaviour changes are introduced to already released functionality.