Skip to content

Commit

Permalink
Json
Browse files Browse the repository at this point in the history
  • Loading branch information
naeemaei committed Oct 31, 2022
1 parent 0e6f07a commit a9d31be
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 16-Json/examples/marshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package examples

import "encoding/json"

type Person struct {
Name string `json:"name"`
Family string `json:"family"`
Age int `json:"age,omitempty"`
}

func MarshalExample1() {
person1 := Person{Name: "Ali", Family: "Rezaee", Age: 17}
person2 := Person{Name: "Milad", Family: "Mohammadi", Age: 20}
person3 := Person{Name: "Peyman", Family: "Mohammadi", Age: 0}

person1Json, err := json.Marshal(person1)
if err != nil {
panic(err)
}
person2Json, err := json.Marshal(person2)
if err != nil {
panic(err)
}
person3Json, err := json.Marshal(person3)
if err != nil {
panic(err)
}

println(string(person1Json))
println(string(person2Json))
println(string(person3Json))
}

func MarshalExample2() {
person1 := Person{Name: "Ali", Family: "Rezaee", Age: 17}
person2 := Person{Name: "Milad", Family: "Mohammadi", Age: 20}
person3 := Person{Name: "Peyman", Family: "Mohammadi", Age: 0}

persons := []Person{person1, person2, person3}
personsJson, err := json.Marshal(persons)
if err != nil {
panic(err)
}

println(string(personsJson))
}
34 changes: 34 additions & 0 deletions 16-Json/examples/unMarshal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package examples

import (
"encoding/json"
"fmt"
)

func UnmarshalExample1() {
person1Json := []byte(`{"name":"Ali","family":"Rezaee","age":17}`)

var person1 = Person{}
err := json.Unmarshal(person1Json, &person1)

if err != nil {
panic(err)
}

println(person1.Name)
println(person1.Family)
println(person1.Age)
}

func UnmarshalExample2() {
personsJson := []byte(`[{"name":"Ali","family":"Rezaee","age":17},{"name":"Milad","family":"Mohammadi","age":20},{"name":"Peyman","family":"Mohammadi"}]`)

var persons = []Person{}
err := json.Unmarshal(personsJson, &persons)

if err != nil {
panic(err)
}

fmt.Printf("%+v", persons)
}
76 changes: 76 additions & 0 deletions 16-Json/examples/unMarshalFromApi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package examples

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)

type BookingLocation struct {
Country string `json:"country"`
State string `json:"state"`
StateName string `json:"stateName"`
ZipCode string `json:"zipcode"`
Timezone string `json:"timezone"`
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
City string `json:"city"`
Continent string `json:"continent"`
}

func UnmarshalExample3() {
response := make(chan []byte)
go GetResponse(response, "https://geolocation.onetrust.com/cookieconsentpub/v1/geo/location")

var location = BookingLocation{}
err := json.Unmarshal(<-response, &location)

if err != nil {
panic(err)
}

println(location.City)
println(location.Country)
println(location.StateName)
}

func GetResponse(content chan<- []byte, url string) {

client := http.Client{}

request, err := http.NewRequest("GET", url, nil)

if err != nil {
panic(err)
}

request.Header = http.Header{}

request.Header.Add("accept", "application/json")

response, err := client.Do(request)

if err != nil {
panic(err)
}

defer response.Body.Close()

responseBody, err := ioutil.ReadAll(response.Body)

if err != nil {
panic(err)
}

PrintlnWithTime(string(responseBody))
PrintlnWithTime("Before set content")
content <- responseBody
PrintlnWithTime("After set content")

}

func PrintlnWithTime(args ...any) {
fmt.Printf("Time: %s, %v\n", time.Now().Format(time.RFC3339Nano), args)
}
3 changes: 3 additions & 0 deletions 16-Json/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module jsonExamples

go 1.18
11 changes: 11 additions & 0 deletions 16-Json/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import "jsonExamples/examples"

func main() {
// examples.MarshalExample1()
// examples.MarshalExample2()
// examples.UnmarshalExample1()
// examples.UnmarshalExample2()
examples.UnmarshalExample3()
}

0 comments on commit a9d31be

Please sign in to comment.