Esempio n. 1
0
// encodeMap encodes a map as unsigned count followed by key:value pairs.
// Because map internals are not exposed, we must use reflection rather than
// addresses.
func (enc *Encoder) encodeMap(b *bytes.Buffer, mv *reflect.MapValue, keyOp, elemOp encOp, keyIndir, elemIndir int) {
	state := enc.newEncoderState(b)
	state.fieldnum = -1
	state.sendZero = true
	keys := mv.Keys()
	state.encodeUint(uint64(len(keys)))
	for _, key := range keys {
		encodeReflectValue(state, key, keyOp, keyIndir)
		encodeReflectValue(state, mv.Elem(key), elemOp, elemIndir)
	}
	enc.freeEncoderState(state)
}
Esempio n. 2
0
func writeMap(w io.Writer, val *reflect.MapValue) (err os.Error) {
	key := val.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return &MarshalError{val.Type()}
	}

	keys := val.Keys()
	if _, err = fmt.Fprint(w, "{"); err != nil {
		return
	}

	for i := 0; i < len(keys); i++ {
		if _, err = fmt.Fprintf(w, "%s:", Quote(keys[i].(*reflect.StringValue).Get())); err != nil {
			return
		}

		if err = writeValue(w, val.Elem(keys[i])); err != nil {
			return
		}

		if i < len(keys)-1 {
			if _, err = fmt.Fprint(w, ","); err != nil {
				return
			}
		}
	}

	_, err = fmt.Fprint(w, "}")
	return
}
Esempio n. 3
0
func writeMap(w io.Writer, val *reflect.MapValue) (err os.Error) {
	key := val.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return &MarshalError{val.Type()}
	}
	_, err = fmt.Fprint(w, "d")
	if err != nil {
		return
	}

	keys := val.Keys()

	// Sort keys

	svList := make(StringValueArray, len(keys))
	for i, key := range keys {
		svList[i].key = key.(*reflect.StringValue).Get()
		svList[i].value = val.Elem(key)
	}

	err = writeSVList(w, svList)
	if err != nil {
		return
	}

	_, err = fmt.Fprint(w, "e")
	if err != nil {
		return
	}
	return
}
Esempio n. 4
0
func (e *encodeState) writeMap(v *reflect.MapValue) {
	if v.IsNil() {
		return
	}
	if _, ok := v.Type().(*reflect.MapType).Key().(*reflect.StringType); !ok {
		e.abort(&EncodeTypeError{v.Type()})
	}
	offset := e.beginDoc()
	for _, k := range v.Keys() {
		e.encodeValue(k.(*reflect.StringValue).Get(), v.Elem(k))
	}
	e.WriteByte(0)
	e.endDoc(offset)
}
Esempio n. 5
0
func writeMap(w io.Writer, val *reflect.MapValue) os.Error {
  writeByte(w, SMALL_TUPLE);
  writeByte(w, 3);
  writeAtom(w, "bert");
  writeAtom(w, "dict");
  
  keys := val.Keys();
  writeByte(w, LIST);
  binary.Write(w, binary.BigEndian, uint32(len(keys)));
  for i := 0; i < len(keys); i++ {
    w.Write([]byte {SMALL_TUPLE, 2});
    writeValue(w, keys[i]);
    writeValue(w, val.Elem(keys[i]));
  }
  writeByte(w, NIL);

  return nil;
}
Esempio n. 6
0
func writeMap(w io.Writer, val *reflect.MapValue) os.Error {
	key := val.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return &MarshalError{val.Type()}
	}

	keys := val.Keys()
	fmt.Fprint(w, "{")
	for i := 0; i < len(keys); i++ {
		fmt.Fprintf(w, "%q:", keys[i].(*reflect.StringValue).Get())

		if err := writeValue(w, val.Elem(keys[i])); err != nil {
			return err
		}

		if i < len(keys)-1 {
			fmt.Fprint(w, ",")
		}
	}

	fmt.Fprint(w, "}")
	return nil
}
Esempio n. 7
0
// object consumes an object from d.data[d.off-1:], decoding into the value v.
// the first byte of the object ('{') has been read already.
func (d *decodeState) object(v reflect.Value) {
	// Check for unmarshaler.
	unmarshaler, pv := d.indirect(v, false)
	if unmarshaler != nil {
		d.off--
		err := unmarshaler.UnmarshalJSON(d.next())
		if err != nil {
			d.error(err)
		}
		return
	}
	v = pv

	// Decoding into nil interface?  Switch to non-reflect code.
	iv, ok := v.(*reflect.InterfaceValue)
	if ok {
		iv.Set(reflect.NewValue(d.objectInterface()))
		return
	}

	// Check type of target: struct or map[string]T
	var (
		mv *reflect.MapValue
		sv *reflect.StructValue
	)
	switch v := v.(type) {
	case *reflect.MapValue:
		// map must have string type
		t := v.Type().(*reflect.MapType)
		if t.Key() != reflect.Typeof("") {
			d.saveError(&UnmarshalTypeError{"object", v.Type()})
			break
		}
		mv = v
		if mv.IsNil() {
			mv.SetValue(reflect.MakeMap(t))
		}
	case *reflect.StructValue:
		sv = v
	default:
		d.saveError(&UnmarshalTypeError{"object", v.Type()})
	}

	if mv == nil && sv == nil {
		d.off--
		d.next() // skip over { } in input
		return
	}

	for {
		// Read opening " of string key or closing }.
		op := d.scanWhile(scanSkipSpace)
		if op == scanEndObject {
			// closing } - can only happen on first iteration.
			break
		}
		if op != scanBeginLiteral {
			d.error(errPhase)
		}

		// Read string key.
		start := d.off - 1
		op = d.scanWhile(scanContinue)
		item := d.data[start : d.off-1]
		key, ok := unquote(item)
		if !ok {
			d.error(errPhase)
		}

		// Figure out field corresponding to key.
		var subv reflect.Value
		if mv != nil {
			subv = reflect.MakeZero(mv.Type().(*reflect.MapType).Elem())
		} else {
			// First try for field with that tag.
			for i := 0; i < sv.NumField(); i++ {
				f := sv.Type().(*reflect.StructType).Field(i)
				if f.Tag == key {
					subv = sv.Field(i)
					break
				}
			}
			if subv == nil {
				// Second, exact match.
				subv = sv.FieldByName(key)
				if subv == nil {
					// Third, case-insensitive match.
					subv = sv.FieldByNameFunc(func(s string) bool { return matchName(key, s) })
				}
			}
		}

		// Read : before value.
		if op == scanSkipSpace {
			op = d.scanWhile(scanSkipSpace)
		}
		if op != scanObjectKey {
			d.error(errPhase)
		}

		// Read value.
		d.value(subv)

		// Write value back to map;
		// if using struct, subv points into struct already.
		if mv != nil {
			mv.SetElem(reflect.NewValue(key), subv)
		}

		// Next token must be , or }.
		op = d.scanWhile(scanSkipSpace)
		if op == scanEndObject {
			break
		}
		if op != scanObjectValue {
			d.error(errPhase)
		}
	}
}
Esempio n. 8
0
func fuzzyEqualMap(a *reflect.MapValue, b *reflect.MapValue) bool {
	key := a.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return false
	}
	key = b.Type().(*reflect.MapType).Key()
	if _, ok := key.(*reflect.StringType); !ok {
		return false
	}

	aKeys, bKeys := a.Keys(), b.Keys()

	if len(aKeys) != len(bKeys) {
		return false
	}

	for _, k := range aKeys {
		if !fuzzyEqualValue(a.Elem(k), b.Elem(k)) {
			return false
		}
	}
	return true
}
Esempio n. 9
0
func PackMap(writer io.Writer, value *reflect.MapValue) (n int, err os.Error) {
	keys := value.Keys()
	if value.Len() < 16 {
		n, err := writer.Write([]byte{0x80 | byte(len(keys))})
		if err != nil {
			return n, err
		}
		for _, k := range keys {
			_n, err := PackValue(writer, k)
			if err != nil {
				return n, err
			}
			n += _n
			_n, err = PackValue(writer, value.Elem(k))
			if err != nil {
				return n, err
			}
			n += _n
		}
	} else if value.Len() < 65536 {
		n, err := writer.Write([]byte{0xde, byte(len(keys) >> 8), byte(len(keys))})
		if err != nil {
			return n, err
		}
		for _, k := range keys {
			_n, err := PackValue(writer, k)
			if err != nil {
				return n, err
			}
			n += _n
			_n, err = PackValue(writer, value.Elem(k))
			if err != nil {
				return n, err
			}
			n += _n
		}
	} else {
		n, err := writer.Write([]byte{0xdf, byte(len(keys) >> 24), byte(len(keys) >> 16), byte(len(keys) >> 8), byte(len(keys))})
		if err != nil {
			return n, err
		}
		for _, k := range keys {
			_n, err := PackValue(writer, k)
			if err != nil {
				return n, err
			}
			n += _n
			_n, err = PackValue(writer, value.Elem(k))
			if err != nil {
				return n, err
			}
			n += _n
		}
	}
	return n, nil
}