How to make json.Marshal/Unmarshal complex Mapa?

I need to do replication of a map cache file. It's very simple I thought. All "simple" (string, int...) types it saves on normal but as soon as the cache hits the structure then the problems begin. Now I have with each added structure to the cache handles it to encode to json (throughout the code...). How to make the coding structures in json happens automatically in the function of replication?

How to use this reflection I did not.

The cache stores map
type DB struct {
 Storage map[string]interface{}
 RW *sync.RWMutex
}
var storage = map[string]*DB

Code replication
package cache

import (
"time"

"encoding/json"

"os"
"io/ioutil"
"log"
"sync"
)
Replication of type map[string]map[string]interface{}
ReplicationFileSystem func(file string, timeRepl time.Duration) {
SelectReplication(file)
 for {
time.Sleep(timeRepl)
replication(file)
}
}
SelectReplication func(file string){
 log.Println("Cache replication: Start")
 var replication Replication
 val, err := ioutil.ReadFile(file)
 if err != nil {
 log.Fatal("Cache replication err: ", err)
}
 err = json.Unmarshal(val, &replication)
 if err != nil {
 log.Fatal("Cache replication decode err: ", err)
}

rw.Lock()
 for key, dbs := range replication {
 database[key] = &DB{
 Storage: dbs,
 RW : &sync.RWMutex{},
}

}
rw.Unlock()
 log.Println("replication Cache: Ok")
}
replication func(file string) {
 encode := Replication{
"replication":map[string]interface{}{
 "replication": "",
},
}
rw.RLock()
 for k, v := range database {
 encode[k] = v.Storage
}
rw.RUnlock()

 json, _ := json.Marshal(encode)
 saveFile(file, string(json))
}

func saveFile(file, string raw) {
 f, err := os.Create(file)
 if err != nil {
return
}
 defer f.Close()

f.WriteString(raw)

}

July 8th 19 at 12:05
2 answers
July 8th 19 at 12:07
What prevents to declare the structure as a json structure?
type Config struct {
 Struct Database {
 DatabaseName string `json:"database_name"`
 DatabasePassword string `json:"database_password"`
 DatabaseUser string `json:"database_user"`
 DatabasePort int `json:"database_port"`
 } `json:"database"`
 Struct Accounts {
 AutoCreate bool `json:"auto_create"`
 } `json:"accounts"`
 Struct Client {
 BanTimeForBruteForcing int `json:"ban_time_for_brute_forcing"`
 Host string `json:"host"`
 LoginTryBeforeBan int `json:"login_try_before_ban"`
 Port int `json:"port"`
 } `json:"client"`
 Struct Server {
 Pingpong bool `json:"pingpong"`
 Host string `json:"host"`
 PingpongDelay bool `json:"pingpong_delay"`
 PingpongDelayTime int `json:"pingpong_delay_time"`
 Port int `json:"port"`
 } `json:"server"`
}


If I understand you correctly.
I do cache the map - Brody_Dickins commented on July 8th 19 at 12:10
and in the end, I can't even imagine what kind of data Mapa - Brody_Dickins commented on July 8th 19 at 12:13
And now the same but in Russian. - Frida15 commented on July 8th 19 at 12:16
: I have a code to complete the map Storage. It any data (from the structures to int) periodically I replicabuy this map to your hard drive. But all the structures it encodes is not true (instead of json I get "" ) - Brody_Dickins commented on July 8th 19 at 12:19
and then when you load the cache from file system he is listed there (there's also an empty string) and then I think it is clear that makes my kkod )) - Brody_Dickins commented on July 8th 19 at 12:22
what if there are bool, int, etc. then everything is fine - Brody_Dickins commented on July 8th 19 at 12:25
July 8th 19 at 12:09
If you need to convert the complex structure of any particular logic, then perhaps it makes sense to use your method of marshaling. Here is a rough example, but in General I think the idea will be able to convey true:
type Person struct {
 Id int
 Name string
}

func (p Person) MarshalJSON() ([]byte, error) {
 // Your custom logic
 return []byte(`{"name": "` + p.Name + `"}`), nil 
}

func main() {
 user := Person{Id: 1, Name: "Mr. Incognito"}
json.NewEncoder(os.Stdout).Encode(user)
 // Instead of the entire structure will only show {"name":"Mr. Incognito"}
}

To try in the sandbox: https://play.golang.org/p/SyVWaDtBR9

Find more questions by tags Go