diff --git a/mutable_xobject.go b/mutable_xobject.go index 101ed7e..4fde087 100644 --- a/mutable_xobject.go +++ b/mutable_xobject.go @@ -6,11 +6,15 @@ type MutableXObject struct { func NewMxoArray() *MutableXObject { p := new(MutableXObject) + p.XObject._type = XOT_ARRAY + p.XObject._val = make([]*XObject, 10) return p } func NewMxoObject() *MutableXObject { p := new(MutableXObject) + p.XObject._type = XOT_OBJECT + p.XObject._val = make(map[string]*XObject) return p } @@ -23,8 +27,8 @@ func (this *MutableXObject) PushXObject(val *XObject) *MutableXObject{ if this.XObject.GetType() != XOT_ARRAY { panic("MutableXObject.PushXvalue type error") } - array := this.XObject._val.(*[]*XObject) - this.XObject._val = append(*array, val) + array := this.XObject._val.([]*XObject) + this.XObject._val = append(array, val) return this } @@ -37,8 +41,8 @@ func (this *MutableXObject) SetXObject(key string, val *XObject) *MutableXObject if this.XObject.GetType() != XOT_OBJECT { panic("MutableXObject.PushXvalue type error") } - kvObj := this.XObject._val.(*(map[string]*XObject)) - (*kvObj)[key] = val + kvObj := this.XObject._val.(map[string]*XObject) + kvObj[key] = val return this } diff --git a/xobject.go b/xobject.go index e8ec51f..f9ae64c 100644 --- a/xobject.go +++ b/xobject.go @@ -24,22 +24,22 @@ func NewXoFromJsonStr(jsonStr string) *XObject { } } -func (this *XObject) Size(key interface{}) int { +func (this *XObject) Size() int { if (this._type == XOT_ARRAY) { - array := this._val.(*[]*XObject) - return len(*array) + array := this._val.([]*XObject) + return len(array) } else { panic("XObject.Size type error") return 0 } } -func (this *XObject) Reset() { +func (this *XObject) reset() { this._type = XOT_SIMPLE - this._val = nil + this._val = NewXUndefined() } -func (this *XObject) GetType() int8{ +func (this *XObject) GetType() int8 { return this._type } @@ -73,8 +73,8 @@ func (this *XObject) At(key string) *XObject { func (this *XObject) Index(index int32) *XObject { if (this._type == XOT_ARRAY) { - array := this._val.(*[]*XObject) - return (*array)[index] + array := this._val.([]*XObject) + return array[index] } else { panic("XObject.At type error") return nil @@ -107,6 +107,7 @@ func (this *XObject) readFromJsonString(data string) bool { } this._type = XOT_OBJECT this._val = kvObj + return true } else if jsonType == JSON_ARRAY { var rawJson []interface{} err := json.Unmarshal([]byte(data), &rawJson) @@ -120,6 +121,7 @@ func (this *XObject) readFromJsonString(data string) bool { xobj := new(XObject).fromInterface(val) arrObj[index] = xobj } + return true } return false } @@ -135,9 +137,9 @@ func (this *XObject) ToJsonStr() string { func (this *XObject) ToInterface() interface{} { if this._type == XOT_ARRAY { - array := this._val.(*[]*XObject) - arrObj := make([]interface{}, len(*array)) - for index, val := range *array { + array := this._val.([]*XObject) + arrObj := make([]interface{}, len(array)) + for index, val := range array { arrObj[index] = val.ToInterface() } return arrObj @@ -152,12 +154,27 @@ func (this *XObject) ToInterface() interface{} { return this.AsXValue().ToJsonInterface() } -func (this *XObject) fromInterface(val interface{}) *XObject { - if this._type != XOT_SIMPLE { - panic("XObject.fromInterface type error1") - } - if !this.AsXValue().TryFromInterface(val) { - panic("XObject.fromInterface type error2") +func (this *XObject) fromInterface(iVal interface{}) *XObject { + if array, ok := iVal.([]interface{}); ok { + arrObj := make([]*XObject, len(array)) + for index, val := range array { + arrObj[index] = new(XObject).fromInterface(val) + } + this._type = XOT_ARRAY + this._val = arrObj + } else if object, ok := iVal.(map[string]interface{}); ok { + kvObj := make(map[string]*XObject) + for key, val := range object { + kvObj[key] = new(XObject).fromInterface(val) + } + this._type = XOT_OBJECT + this._val = kvObj + } else { + this._type = XOT_SIMPLE + this._val = NewXUndefined() + if !this.AsXValue().TryFromInterface(iVal) { + panic("XObject.fromInterface type error2") + } } return this }