jsonfileworker

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 8, 2025 License: MIT Imports: 5 Imported by: 0

README

About

This library makes it easier to work with local JSON files in Go. Has multiple handy abstractions to get, set & update JSON objects and arrays.
It is currently a Work In Progress, and I built it as I did not really find any library of the same exact working. If you just want to work with JSON objects in code, checkout:

Features currently built

Path Note: In all functions, the filePath works with both absolute as well as relative paths
GetAllObjects
  • Returns all the contents from the file
  • This function unmarshals the object directly into the variable passed (Does require passing the var by reference.)
  • If datatype is unknown, passing in an interface{} would still give results.
func main() {
  var users []models.Users // Replace with the datatype required.

  err := GetAllObjects(filePath, &users)
  // Handle errors

  b, _ := json.MarshalIndent(users, "", "  ")
  fmt.Print(string(b))
}
GetObjectFromIndex
  • Returns a single object from an array of objects
  • Uses generics to take care of typesafety & autocomplete
  • Would throw an error if the index passed does not exist
  • If datatype is unknown, can pass in any or discard it altogether
func main() {

  v, err := GetObjectFromIndex[models.Users](filePath, 5)
  // Handle errors

  fmt.Print(v.Name)
}
SetAllObjects
  • Clears out the entire contents of the file and replaces it with the given contents
// To be added
AppendObjectToArray
  • Appends an object to the end of the array.
  • Follows the RMW (Read-Modify-Write) approach. Reads the entire file into memory, modifies it and then writes the file contents back.
  • Currently only works with the entire object array. Working on appending objects to array fields.
func main() {
  
  obj := models.Users{
    ID:       1,
    Name:     "Leanne Graham",
    Username: "Bret",
    Email:    "Sincere@april.biz",
    Address: models.Address{
      // ...
    }
  }

  err = AppendObjectToArray(filePath, obj)

  // optional generic can also be added if you are sure that the other objects in the array are also of the same type
  // err = AppendObjectToArray[models.Users](filePath, obj)

  // Handle errors
}
AppendObjectToArrayDirect
  • Does the same thing as AppendObjectToArray
  • But instead of reading all the filecontents into memory, appends it directly using some manipulation.
  • Is recommended when the filesize is too large to be read into memory.
func main() {
  
  obj := models.Users{
    ID:       1,
    Name:     "Leanne Graham",
    Username: "Bret",
    Email:    "Sincere@april.biz",
    Address: models.Address{
      // ...
    }
  }

  err = AppendObjectToArrayDirect(filePath, obj)
  // Handle errors
}


Things to note:

  • If need more control over updates, it is better to GetAllObjects, run the updates and then SetAllObjects
  • The generics are used for type safety and JSON structure consistency. Can use any if the structure is unknown or doesnt matter

TODO:

  • Check wrongdatatypes for GetAllObjects
  • Test and add code example for SetAllObjects

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendObjectToArray

func AppendObjectToArray[T any](pathToFile string, obj T) error

Appends entire object to the array in file Does not yet work on arrays in individual fields This function follows the Read-Modify-Write approach

func AppendObjectToArrayDirect

func AppendObjectToArrayDirect[T any](pathToFile string, obj T) error

AppendObjectToArrayDirect - This will not read the entire file contents to memory, but instead append it directly using writeStrings ig ! Need to fix the indenting issues. Seeking -2 works in some cases, but not during empty file & only brackets situation.

func GetAllObjects

func GetAllObjects(pathToFile string, obj interface{}) error

func GetObjectFromIndex

func GetObjectFromIndex[T any](pathToFile string, index int) (T, error)

Returns the individual object at the given index. Takes in the type that should be returned (This would help in typesafety as well as autocomplete) Returns error when: 1. The file is not found 2. There is some error reading the file contents 3. The actual JSON content is not an array (need it to be an array to have an index for this one) 4. The content received is not of the type passed in

* Can pass in any as the type in the function if type is not known * The function works with optional field values as well

func SetAllObjects

func SetAllObjects(pathToFile string, obj interface{}) error

Will see if I can work on updating modularly if possible

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL