@kjk1/

gobook xml custom marshal

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.6472843437477582","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImVuY29kaW5nL3htbCIKCSJmbXQiCgkibG9nIgoJInRpbWUiCikKCmZ1bmMgbm90Q3VzdG9tKCkgewoJLy8gOnNob3cgc3RhcnQKCXR5cGUgRXZlbnQgc3RydWN0IHsKCQlXaGF0IHN0cmluZwoJCVdoZW4gdGltZS5UaW1lCgl9CgllIDo9IEV2ZW50ewoJCVdoYXQ6ICJlYXJ0aHF1YWtlIiwKCQlXaGVuOiB0aW1lLk5vdygpLAoJfQoJZCwgZXJyIDo9IHhtbC5NYXJzaGFsKCZlKQoJaWYgZXJyICE9IG5pbCB7CgkJbG9nLkZhdGFsZigianNvbi5NYXJzaGFsSW5kZW50IGZhaWxlZCB3aXRoICclcydcbiIsIGVycikKCX0KCWZtdC5QcmludGYoIlN0YW5kYXJkIHRpbWUgSlNPTjogJXNcbiIsIHN0cmluZyhkKSkKCS8vIDpzaG93IGVuZAp9CgovLyA6c2hvdyBzdGFydAp0eXBlIGN1c3RvbVRpbWUgdGltZS5UaW1lCgpjb25zdCBjdXN0b21UaW1lRm9ybWF0ID0gYDIwMDYtMDItMDFgCgpmdW5jIChjdCBjdXN0b21UaW1lKSBNYXJzaGFsWE1MKGUgKnhtbC5FbmNvZGVyLCBzdGFydCB4bWwuU3RhcnRFbGVtZW50KSBlcnJvciB7Cgl0IDo9IHRpbWUuVGltZShjdCkKCXYgOj0gdC5Gb3JtYXQoY3VzdG9tVGltZUZvcm1hdCkKCXJldHVybiBlLkVuY29kZUVsZW1lbnQodiwgc3RhcnQpCn0KCmZ1bmMgKGN0ICpjdXN0b21UaW1lKSBVbm1hcnNoYWxYTUwoZCAqeG1sLkRlY29kZXIsIHN0YXJ0IHhtbC5TdGFydEVsZW1lb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dEN1c3RvbSgpCgljdXN0b20oKQoJLy8gOnNob3cgZW5kCn0="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main

import (
	"encoding/xml"
	"fmt"
	"log"
	"time"
)

func notCustom() {
	// :show start
	type Event struct {
		What string
		When time.Time
	}
	e := Event{
		What: "earthquake",
		When: time.Now(),
	}
	d, err := xml.Marshal(&e)
	if err != nil {
		log.Fatalf("json.MarshalIndent failed with '%s'\n", err)
	}
	fmt.Printf("Standard time JSON: %s\n", string(d))
	// :show end
}

// :show start
type customTime time.Time

const customTimeFormat = `2006-02-01`

func (ct customTime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
	t := time.Time(ct)
	v := t.Format(customTimeFormat)
	return e.EncodeElement(v, start)
}

func (ct *customTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var s string
	err := d.DecodeElement(&s, &start)
	if err != nil {
		return err
	}
	t, err := time.Parse(customTimeFormat, s)
	if err != nil {
		return err
	}
	*ct = customTime(t)
	return nil
}

type Event2 struct {
	What string
	When customTime
}

// :show end

func custom() {
	// :show start
	e := Event2{
		What: "earthquake",
		When: customTime(time.Now()),
	}
	d, err := xml.Marshal(&e)
	if err != nil {
		log.Fatalf("xml.Marshal failed with '%s'\n", err)
	}
	fmt.Printf("\nCustom time XML: %s\n", string(d))
	var decoded Event2
	err = xml.Unmarshal(d, &decoded)
	if err != nil {
		log.Fatalf("xml.Unmarshal failed with '%s'\n", err)
	}
	t := time.Time(decoded.When)
	fmt.Printf("Decoded custom time: %s\n", t.Format(customTimeFormat))
	// :show end
}

func main() {
	// :show start
	notCustom()
	custom()
	// :show end
}