Package com.addthis.basis.kv

Examples of com.addthis.basis.kv.KVPairs


        this.format = new KVBundleFormat();
    }

    @Override
    public void send(Bundle row) {
        KVPairs kv = new KVPairs();
        for (BundleField field : row.getFormat()) {
            kv.putValue(field.getName(), row.getValue(field).toString());
        }
        try {
            if (binary) {
                byte data[] = kv.toBinArray();
                Bytes.writeInt(data.length, out);
                out.write(data);
            } else {
                out.write(Bytes.toBytes(kv.toString()));
                out.write('\n');
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
View Full Code Here


public class CodecKV extends Codec {

    @Override
    public Object decode(Object shell, byte[] data) throws Exception {
        return decodeString(getClassFieldMap(shell.getClass()), shell, new KVPairs(Bytes.toString(data)));
    }
View Full Code Here

    //    @SuppressWarnings("unchecked")
    public static String encodeString(Object object) throws Exception {
        if (object instanceof SuperCodable) {
            ((SuperCodable) object).preEncode();
        }
        KVPairs kv = new KVPairs();
        CodableClassInfo fieldMap = getClassFieldMap(object.getClass());
        if (fieldMap.size() == 0) {
            return object.toString();
        }
        String stype = fieldMap.getClassName(object);
        if (stype != null) {
            kv.putValue(fieldMap.getClassField(), stype);
        }
        for (Iterator<CodableFieldInfo> fields = fieldMap.values().iterator(); fields.hasNext();) {
            CodableFieldInfo field = fields.next();
            Object value = field.get(object);
            if (value == null) {
                continue;
            }
            String name = field.getName();
            if (field.isArray()) {
                int len = Array.getLength(value);
                if (field.getType() == byte.class) {
                    kv.putValue(name, new String(Base64.encode((byte[]) value)));
                } else {
                    for (int i = 0; i < len; i++) {
                        Object av = Array.get(value, i);
                        kv.putValue(name + i, encodeString(av));
                    }
                }
            } else if (field.isCollection()) {
                Collection<?> coll = (Collection<?>) value;
                int idx = 0;
                for (Iterator<?> iter = coll.iterator(); iter.hasNext(); idx++) {
                    kv.putValue(name + idx, encodeString(iter.next()));
                }
            } else if (field.isMap()) {
                Map<?, ?> map = (Map<?, ?>) value;
                KVPairs nv = new KVPairs();
                for (Entry<?, ?> entry : map.entrySet()) {
                    nv.putValue(entry.getKey().toString(), encodeString(entry.getValue()));
                }
                kv.putValue(field.getName(), nv.toString());
            } else if (field.isNative()) {
                kv.putValue(name, value.toString());
            } else if (field.isEnum()) {
                kv.putValue(name, value.toString());
            } else if (field.isCodable()) {
View Full Code Here

    public static Object decodeString(CodableClassInfo fieldMap, Class<?> type, String kv) throws Exception {
        if (fieldMap.size() == 0) {
            return decodeNative(type, kv);
        }
        return decodeKV(fieldMap, type, new KVPairs(kv));
    }
View Full Code Here

            } else if (field.isMap()) {
                Map<String, Object> map = (Map<String, Object>) field.getType().newInstance();
                // value type, assume key is String
                Class<?> kc = field.getMapKeyClass();
                Class<?> vc = field.getMapValueClass();
                for (KVPair p : new KVPairs(data.getValue(name))) {
                    map.put(p.getKey(), decodeString(vc, p.getValue()));
                }
                field.set(object, map);
            } else if (field.isCodable()) {
                field.set(object, decodeString(field.getType(), data.getValue(name)));
View Full Code Here

    public static final CodecKV INSTANCE = new CodecKV();

    @Override
    public Object decode(Object shell, byte[] data) throws Exception {
        return decodeString(Fields.getClassFieldMap(shell.getClass()), shell,
                            new KVPairs(Bytes.toString(data)));
    }
View Full Code Here

    //    @SuppressWarnings("unchecked")
    public static String encodeString(Object object) throws Exception {
        if (object instanceof SuperCodable) {
            ((SuperCodable) object).preEncode();
        }
        KVPairs kv = new KVPairs();
        CodableClassInfo fieldMap = Fields.getClassFieldMap(object.getClass());
        if (fieldMap.size() == 0) {
            return object.toString();
        }
        String stype = fieldMap.getClassName(object);
        if (stype != null) {
            kv.putValue(fieldMap.getClassField(), stype);
        }
        for (Iterator<CodableFieldInfo> fields = fieldMap.values().iterator(); fields.hasNext(); ) {
            CodableFieldInfo field = fields.next();
            Object value = field.get(object);
            if (value == null) {
                continue;
            }
            String name = field.getName();
            if (field.isArray()) {
                int len = Array.getLength(value);
                if (field.getType() == byte.class) {
                    kv.putValue(name, new String(Base64.encode((byte[]) value)));
                } else {
                    for (int i = 0; i < len; i++) {
                        Object av = Array.get(value, i);
                        kv.putValue(name + i, encodeString(av));
                    }
                }
            } else if (field.isCollection()) {
                Collection<?> coll = (Collection<?>) value;
                int idx = 0;
                for (Iterator<?> iter = coll.iterator(); iter.hasNext(); idx++) {
                    kv.putValue(name + idx, encodeString(iter.next()));
                }
            } else if (field.isMap()) {
                Map<?, ?> map = (Map<?, ?>) value;
                KVPairs nv = new KVPairs();
                for (Entry<?, ?> entry : map.entrySet()) {
                    nv.putValue(entry.getKey().toString(), encodeString(entry.getValue()));
                }
                kv.putValue(field.getName(), nv.toString());
            } else if (field.isNative()) {
                kv.putValue(name, value.toString());
            } else if (field.isEnum()) {
                kv.putValue(name, value.toString());
            } else if (field.isCodable()) {
View Full Code Here

    public static Object decodeString(CodableClassInfo fieldMap, Class<?> type, String kv) throws Exception {
        if (fieldMap.size() == 0) {
            return decodeNative(type, kv);
        }
        return decodeKV(fieldMap, type, new KVPairs(kv));
    }
View Full Code Here

            } else if (field.isMap()) {
                Map<String, Object> map = (Map<String, Object>) field.getType().newInstance();
                // value type, assume key is String
                Class<?> kc = field.getMapKeyClass();
                Class<?> vc = field.getMapValueClass();
                for (KVPair p : new KVPairs(data.getValue(name))) {
                    map.put(p.getKey(), decodeString(vc, p.getValue()));
                }
                field.set(object, map);
            } else if (field.isCodable()) {
                field.set(object, decodeString(field.getType(), data.getValue(name)));
View Full Code Here

    public static final CodecKV INSTANCE = new CodecKV();

    @Override
    public Object decode(Object shell, byte[] data) throws Exception {
        return decodeString(Fields.getClassFieldMap(shell.getClass()), shell,
                            new KVPairs(Bytes.toString(data)));
    }
View Full Code Here

TOP

Related Classes of com.addthis.basis.kv.KVPairs

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.