@@ -14,18 +14,18 @@ package main
1414
1515import (
1616 "fmt"
17- "github.com/spatialcurrent/go-simple-serializer/gss"
18- )
19-
20- import (
2117 "github.com/gopherjs/gopherjs/js"
18+ "github.com/pkg/errors"
19+ "github.com/spatialcurrent/go-simple-serializer/gss"
2220 "honnef.co/go/js/console"
2321)
2422
2523func main () {
2624 js .Global .Set ("gss" , map [string ]interface {}{
27- "version" : gss .VERSION ,
28- "convert" : Convert ,
25+ "version" : gss .VERSION ,
26+ "convert" : Convert ,
27+ "deserialize" : Deserialize ,
28+ "serialize" : Serialize ,
2929 })
3030}
3131
@@ -60,8 +60,63 @@ func Convert(input_string string, input_format string, output_format string, opt
6060
6161 output_string , err := gss .Convert (input_string , input_format , input_header , input_comment , output_format , false )
6262 if err != nil {
63- console .Log (err .Error ())
63+ console .Error (err .Error ())
6464 return ""
6565 }
6666 return output_string
6767}
68+
69+ func Deserialize (input_string string , input_format string , options * js.Object ) interface {} {
70+
71+ m := map [string ]interface {}{}
72+ for _ , key := range js .Keys (options ) {
73+ m [key ] = options .Get (key ).Interface ()
74+ }
75+
76+ input_header := []string {}
77+ input_comment := ""
78+
79+ if v , ok := m ["header" ]; ok {
80+ switch v .(type ) {
81+ case []string :
82+ input_header = v .([]string )
83+ case []interface {}:
84+ input_header = make ([]string , 0 , len (v .([]interface {})))
85+ for _ , h := range v .([]interface {}) {
86+ input_header = append (input_header , fmt .Sprint (h ))
87+ }
88+ }
89+ }
90+
91+ if v , ok := m ["input_comment" ]; ok {
92+ switch v := v .(type ) {
93+ case string :
94+ input_comment = v
95+ }
96+ }
97+
98+ input_type , err := gss .GetType (input_string , input_format )
99+ if err != nil {
100+ console .Error (errors .Wrap (err , "error creating new object for format " + input_format ))
101+ return ""
102+ }
103+
104+ output_object , err := gss .Deserialize (input_string , input_format , input_header , input_comment , input_type , false )
105+ if err != nil {
106+ console .Error (err .Error ())
107+ return ""
108+ }
109+
110+ return output_object
111+ }
112+
113+ func Serialize (input_object interface {}, output_format string ) interface {} {
114+
115+ output_string , err := gss .Serialize (input_object , output_format )
116+ if err != nil {
117+ console .Error (err .Error ())
118+ return ""
119+ }
120+
121+ return output_string
122+ }
0 commit comments