ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - abusomani/jsonhandlers: JSON library to expose simple handlers that lets you easily read and write json from various sources.
JSON library to expose simple handlers that lets you easily read and write json from various sources. - abusomani/jsonhandlers
Visit Site

GitHub - abusomani/jsonhandlers: JSON library to expose simple handlers that lets you easily read and write json from various sources.

GitHub - abusomani/jsonhandlers: JSON library to expose simple handlers that lets you easily read and write json from various sources.

Build Status Github top language Github language count License Go Report Card Go Reference Repo size Coverage Status Mentioned in Awesome Go

Prerequisites

A go module where you want to integrate jsonhandlers. To create one, follow this guide.

Installation

go get github.com/abusomani/jsonhandlers

Usage

A very useful feature of Go’s import statement are aliases. A common use case for import aliases is to provide a shorter alternative to a library’s package name.

In this example, we save ourselves having to type jsonhandlers everytime we want to call one of the library’s functions, we just use jh instead.

import (
    jh "github.com/abusomani/jsonhandlers"
)

Options

Jsonhandlers package exposes multiple options while creating a new jsonhandler to be able to read/write json from sources like Files, Http Requests or Http responses.

WithFileHandler

You can use the WithFileHandler option to read/write Json from/to a file. For this, you need to create a new jsonhandler with the file handler option.

Example to understand WithFileHandler in more detail.

Sample Code

package operations

import (
	"fmt"

	"github.com/abusomani/jsonhandlers"
)


func handleFile() {
	jh := jsonhandlers.New(jsonhandlers.WithFileHandler(testFilePath))

	var sch school
	err := jh.Unmarshal(&sch)
}

WithHTTPRequestHandler

You can use the WithHTTPRequestHandler option to read Json from a Http Request and to write Json to a Http ResponseWriter. For this, you need to create a new jsonhandler with the Http request handler option.

Example to understand WithHTTPRequestHandler in more detail.

Sample Code

package operations

import (
	"net/http"

	"github.com/abusomani/jsonhandlers"
)

type studentSearchRequest struct {
	Name string
}

type studentSearchResponse struct {
	Info student
}

func HandleHTTPRequest(students []student) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		
    	jh := jsonhandlers.New(jsonhandlers.WithHTTPRequestHandler(w, r))

		var reqBody studentSearchRequest
		_ := jh.Unmarshal(&reqBody)
		

		for _, student := range students {
			// student found
			if student.Name == reqBody.Name {
				
				// write the response using jh.Marshal
				jh.Marshal(studentSearchResponse{
					Info: student,
				})
				return
			}
		}
	})
}

/*
  Sample request to be hit on the localhost server to test WithHTTPRequestHandler functionality.
  curl http://localhost:8080/search -d '{"Name": "Abhishek Somani"}'
*/

WithHTTPResponseHandler

You can use the WithHTTPResponseHandler option to read/write Json from/to a Http Response. For this, you need to create a new jsonhandler with the Http response handler option.

Example to understand WithHTTPResponseHandler in more detail.

Sample Code

package operations

import (
	"fmt"
	"log"
	"net/http"

	"github.com/abusomani/jsonhandlers"
)

type user struct {
	Id        int
	FirstName string
	LastName  string
}

type getUsersResponse struct {
	Users []user
}

func HandleHTTPResponse() {
	resp, _ := http.Get("https://dummyjson.com/users")
	jh := jsonhandlers.New(jsonhandlers.WithHTTPResponseHandler(resp))

	var userResp getUsersResponse
	jh.Unmarshal(&userResp)
}

Run examples

To run the examples present in the example folder you need to first checkout this package by doing a git clone. Once you have checked out this package, then you can run the main.go using the following command to see all the examples in action:

go run example/main.go

License

Licensed under MIT

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory