Skip to content
/ extra Public

extra allows for flattening of unused JSON fields to be placed in a hash map

License

Notifications You must be signed in to change notification settings

gohort/extra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

extra

extra is used to flatten the extra fields in a JSON object that are not defined in a Golang structure.

This is a functionality that serde in Rust provides called flatten, but is not present in the default features of Golang JSON unmarshalling.

For example, if we have a JSON object for a person:

{
    "firstName": "John",
    "lastName": "Doe",
    "age": 25,
    "favoriteColor": "red",
    "job": "mail man"
}

Now let's say we only want to capture John Doe's first and last name to use in the code, and the rest we can ignore.

We want to ignore the other fields, but we don't want to omit them or delete them because it's vital information about John Doe. So we create this structure:

import "github.com/gohort/extra"

type Person struct {
    FirstName string `json:"firstName"`
    LastName string `json:"lastName"`
    // Any fields that are not included in the Go struct are placed in this map.
    extra.Any
}

So now when we marshal back using this structure it will be in the same format as the JSON object that we received.


To see more examples please look in the examples/ directory.

About

extra allows for flattening of unused JSON fields to be placed in a hash map

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages