json-common-utilities-go

module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2022 License: MIT

README

json-common-utilities-go

Yet another go library for common json operations. The purpose of this library is to assist Go developers in fast track json manipulation and save them from hassle of writing repeating code.

json-common-utilities-go

Handler interface is the core of this library, which exposes a number of different methods to handle json.

type Handler interface {
 Display(msg interface{})
 StringToStruct(s string, i interface{}) error
 StructToString(i interface{}) (string, error)
 StringToMap(s string) (map[string]interface{}, error)
 MapToString(m map[string]interface{}) (string, error)
 BytesToString(jsonBytes []byte) string
 StringToBytes(s string) []byte
 StructToBytes(i interface{}) (jsonBytes []byte, err error)
 BytesToStruct(b []byte, d interface{}) error
 ModifyInputJson(s string) (map[string]interface{}, error)
 DisplayJsonFieldTypes(jsonBytes []byte) error
 TriggerAllJsonMethods(str string)
}

Vet whole project by,

go vet ./...

run all tests in json_handler_test.go by,

go test ./...
Getting started

import json-common-utilities-go into the user module by including following lines in go.mod file

require (
    github.com/azam-akram/json-common-utilities-go v0.0.6
)

Have a look at TriggerAllJsonMethods(str string) to know how to use other functions exposed by JsonHandler interface. If you want to call all of these functions, simply call TriggerAllJsonMethods(str string)

Example: A user main function calling TriggerAllJsonMethods(str string) which shows how to call all the functions exposed by Handler interface.

package main

import (
 "fmt"

 json_handler "github.com/azam-akram/json-common-utilities-go/json-handler"
 "github.com/azam-akram/json-common-utilities-go/logger"
 "github.com/azam-akram/json-common-utilities-go/model"
)

var employeeStr = string(`{
    "id": "The ID",
    "name": "The User",
    "designation": "CEO",
    "address":
    [
        {
            "doorNumber": 1,
            "street": "The office street",
            "city": "The office city",
            "country": "The office country"
        },
        {
            "doorNumber": 1,
            "street": "The home street",
            "city": "The home city",
            "country": "The home country"
        }
    ]
}`)

func main() {
 fmt.Println("Starting main..")

 TriggerAllJsonMethods(employeeStr)
}

func TriggerAllJsonMethods(employeeStr string) {
 jh := json_handler.NewJsonHandler()

 var emp *model.Employee
 _ = jh.StringToStruct(employeeStr, &emp)
 logger.GetLogger().PrintKeyValue("StringToStruct: ", "emp", emp)

 outputStr, _ := jh.StructToString(&emp)
 logger.GetLogger().PrintKeyValue("StructToString: ", "str", outputStr)

 jMap, _ := jh.StringToMap(outputStr)
 id := jMap["id"].(string)
 user := jMap["name"].(string)
 logger.GetLogger().PrintKeyValue("StringToMap: ", "id", id, "user", user)

 mapData := map[string]interface{}{
  "id":   "The ID",
  "name": "The User",
 }

 outputStr, _ = jh.MapToString(mapData)
 logger.GetLogger().PrintKeyValue("MapToString: ", "str", outputStr)

 inputBytes := []byte(`{"id": "The ID", "name": "The User"}`)
 outputString := jh.BytesToString(inputBytes)
 logger.GetLogger().PrintKeyValue("BytesToString: ", "outputString", outputString)

 outputBytes := jh.StringToBytes(outputString)
 logger.GetLogger().PrintKeyValue("StringToBytes: ", "outputBytes", outputBytes)

 outputBytes, _ = jh.StructToBytes(emp)
 logger.GetLogger().PrintKeyValue("StructToBytes: ", "outputBytes", outputBytes)

 var newEmp *model.Employee
 _ = jh.BytesToStruct(outputBytes, &newEmp)
 logger.GetLogger().PrintKeyValue("BytesToStruct: ", "newEmp", newEmp)

 logger.GetLogger().PrintKeyValue("Before calling ModifyInputJson: ", "employeeStr", employeeStr)
 _ = jh.StringToStruct(outputStr, &emp)
 logger.GetLogger().PrintKeyValue("Before calling ModifyInputJson: ", "emp", emp)
 modifiedEmp, _ := jh.ModifyInputJson(employeeStr)
 logger.GetLogger().PrintKeyValue("After calling ModifyInputJson: ", "modifiedEmp", modifiedEmp)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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