| 1 |
// This file is part of go-properties-orderedmap library. |
| 2 |
// |
| 3 |
// SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies |
| 4 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
|
| 6 |
package properties |
| 7 |
|
| 8 |
import ( |
| 9 |
"encoding/json" |
| 10 |
) |
| 11 |
|
| 12 |
// XXX: no simple way to preserve ordering in JSON. |
| 13 |
|
| 14 |
// MarshalJSON implements json.Marshaler interface |
| 15 |
func (m *Map) MarshalJSON() ([]byte, error) { |
| 16 |
return json.Marshal(m.kv) |
| 17 |
} |
| 18 |
|
| 19 |
// UnmarshalJSON implements json.Unmarshaler interface |
| 20 |
func (m *Map) UnmarshalJSON(d []byte) error { |
| 21 |
var obj map[string]string |
| 22 |
if err := json.Unmarshal(d, &obj); err != nil { |
| 23 |
return err |
| 24 |
} |
| 25 |
|
| 26 |
m.kv = map[string]string{} |
| 27 |
m.o = []string{} |
| 28 |
for k, v := range obj { |
| 29 |
m.Set(k, v) |
| 30 |
} |
| 31 |
return nil |
| 32 |
} |