// Copyright (C) 2019-2021 Chrystian Huot // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see package main import ( "encoding/json" "fmt" ) type Unit struct { Id uint `json:"id"` Label string `json:"label"` } func (unit *Unit) FromMap(m map[string]interface{}) { switch v := m["id"].(type) { case float64: unit.Id = uint(v) } switch v := m["label"].(type) { case string: unit.Label = v } } type Units []Unit func (units *Units) FromJson(str string) error { var v interface{} *units = Units{} formatError := func(err error) error { return fmt.Errorf("units.fromjson") } if err := json.Unmarshal([]byte(str), &v); err != nil { return formatError(err) } switch v.(type) { case []interface{}: for _, r := range v.([]interface{}) { switch m := r.(type) { case map[string]interface{}: unit := Unit{} unit.FromMap(m) *units = append(*units, unit) } } } return nil } func (units *Units) ToJson() (string, error) { if b, err := json.Marshal(*units); err == nil { return string(b), nil } else { return "", fmt.Errorf("units.tojson: %v", err) } } func (units *Units) FromMap(f []interface{}) { *units = Units{} for _, r := range f { switch m := r.(type) { case map[string]interface{}: unit := Unit{} unit.FromMap(m) *units = append(*units, unit) } } }