Search in sources :

Example 71 with KriptonByteArrayOutputStream

use of com.abubusoft.kripton.common.KriptonByteArrayOutputStream in project kripton by xcesco.

the class BindBean63SharedPreferences method serializeValueMapStringByte.

/**
 * for attribute valueMapStringByte serialization
 */
protected String serializeValueMapStringByte(Map<String, Byte> value) {
    if (value == null) {
        return null;
    }
    KriptonJsonContext context = KriptonBinder.jsonBind();
    try (KriptonByteArrayOutputStream stream = new KriptonByteArrayOutputStream();
        JacksonWrapperSerializer wrapper = context.createSerializer(stream)) {
        JsonGenerator jacksonSerializer = wrapper.jacksonGenerator;
        jacksonSerializer.writeStartObject();
        int fieldCount = 0;
        if (value != null) {
            fieldCount++;
            // write wrapper tag
            if (value.size() > 0) {
                jacksonSerializer.writeFieldName("valueMapStringByte");
                jacksonSerializer.writeStartArray();
                for (Map.Entry<String, Byte> item : value.entrySet()) {
                    jacksonSerializer.writeStartObject();
                    jacksonSerializer.writeStringField("key", item.getKey());
                    if (item.getValue() == null) {
                        jacksonSerializer.writeNullField("value");
                    } else {
                        jacksonSerializer.writeNumberField("value", item.getValue());
                    }
                    jacksonSerializer.writeEndObject();
                }
                jacksonSerializer.writeEndArray();
            } else {
                jacksonSerializer.writeNullField("valueMapStringByte");
            }
        }
        jacksonSerializer.writeEndObject();
        jacksonSerializer.flush();
        return stream.toString();
    } catch (Exception e) {
        throw (new KriptonRuntimeException(e.getMessage()));
    }
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException) KriptonByteArrayOutputStream(com.abubusoft.kripton.common.KriptonByteArrayOutputStream) KriptonJsonContext(com.abubusoft.kripton.KriptonJsonContext) JacksonWrapperSerializer(com.abubusoft.kripton.persistence.JacksonWrapperSerializer) HashMap(java.util.HashMap) Map(java.util.Map) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException)

Example 72 with KriptonByteArrayOutputStream

use of com.abubusoft.kripton.common.KriptonByteArrayOutputStream in project kripton by xcesco.

the class AbstractBindTypeProcessorTest method serializeAndParseBinary.

public int serializeAndParseBinary(Object bean, BinderType type) throws Exception {
    KriptonByteArrayOutputStream bar = new KriptonByteArrayOutputStream();
    KriptonBinder.bind(type).serialize(bean, bar);
    String value1 = toString(bar.getByteBuffer());
    System.out.println(value1);
    Object bean2 = KriptonBinder.bind(type).parse(new ByteArrayInputStream(bar.getByteBuffer()), bean.getClass());
    KriptonByteArrayOutputStream bar2 = new KriptonByteArrayOutputStream();
    KriptonBinder.bind(type).serialize(bean2, bar2);
    String value2 = toString(bar2.getByteBuffer());
    System.out.println(value2);
    Assert.assertTrue(value1.length() == value2.length());
    return bar.getCount();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) KriptonByteArrayOutputStream(com.abubusoft.kripton.common.KriptonByteArrayOutputStream)

Example 73 with KriptonByteArrayOutputStream

use of com.abubusoft.kripton.common.KriptonByteArrayOutputStream in project kripton by xcesco.

the class AbstractBindTypeProcessorTest method serializeAndParseCollectionBinary.

public <E> int serializeAndParseCollectionBinary(Collection<E> list, Class<E> clazz, BinderType type) throws Exception {
    KriptonByteArrayOutputStream bar = new KriptonByteArrayOutputStream();
    KriptonBinder.bind(type).serializeCollection(list, clazz, bar);
    String value1 = toString(bar.getByteBuffer());
    Collection<E> list2 = KriptonBinder.bind(type).parseCollection(bar.getByteBufferCopy(), new ArrayList<E>(), clazz);
    KriptonByteArrayOutputStream bar2 = new KriptonByteArrayOutputStream();
    KriptonBinder.bind(type).serializeCollection(list2, clazz, bar2);
    String value2 = toString(bar2.getByteBuffer());
    System.out.println(value1);
    System.out.println(value2);
    Assert.assertTrue(value1.length() == value2.length());
    ReflectionAssert.assertReflectionEquals(type.toString(), list, list2, ReflectionComparatorMode.LENIENT_ORDER);
    // 
    return bar.getCount();
}
Also used : KriptonByteArrayOutputStream(com.abubusoft.kripton.common.KriptonByteArrayOutputStream)

Example 74 with KriptonByteArrayOutputStream

use of com.abubusoft.kripton.common.KriptonByteArrayOutputStream in project kripton by xcesco.

the class AbstractBaseTest method serializeAndParseBinary.

public int serializeAndParseBinary(Object bean, BinderType type) throws Exception {
    KriptonByteArrayOutputStream bar = new KriptonByteArrayOutputStream();
    KriptonBinder.bind(type).serialize(bean, bar);
    String value1 = toString(bar.getByteBufferCopy());
    System.out.println("[[" + value1 + "]]");
    Object bean2 = KriptonBinder.bind(type).parse(new ByteArrayInputStream(bar.getByteBuffer()), bean.getClass());
    KriptonByteArrayOutputStream bar2 = new KriptonByteArrayOutputStream();
    KriptonBinder.bind(type).serialize(bean2, bar2);
    String value2 = toString(bar2.getByteBufferCopy());
    if (value1.equals(value2)) {
        System.out.println("[[-- same --]]");
    } else {
        System.out.println("[[" + value2 + "]]");
    }
    Assert.assertTrue(value1.length() == value2.length());
    return bar.getCount();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) KriptonByteArrayOutputStream(com.abubusoft.kripton.common.KriptonByteArrayOutputStream)

Example 75 with KriptonByteArrayOutputStream

use of com.abubusoft.kripton.common.KriptonByteArrayOutputStream in project kripton by xcesco.

the class AbstractBaseTest method serializeAndParseCollectionBinary.

public <E> int serializeAndParseCollectionBinary(Collection<E> list, Class<E> clazz, BinderType type) throws Exception {
    KriptonByteArrayOutputStream bar = new KriptonByteArrayOutputStream();
    KriptonBinder.bind(type).serializeCollection(list, clazz, bar);
    String value1 = toString(bar.getByteBuffer());
    System.out.println("[[" + value1 + "]]");
    Collection<E> list2 = KriptonBinder.bind(type).parseCollection(bar.getByteBufferCopy(), new ArrayList<E>(), clazz);
    KriptonByteArrayOutputStream bar2 = new KriptonByteArrayOutputStream();
    KriptonBinder.bind(type).serializeCollection(list2, clazz, bar2);
    String value2 = toString(bar2.getByteBuffer());
    if (value1.equals(value2)) {
        System.out.println("[[-- same --]]");
    } else {
        System.out.println("[[" + value2 + "]]");
    }
    Assert.assertTrue(value1.length() == value2.length());
    ReflectionAssert.assertReflectionEquals(type.toString(), list, list2, ReflectionComparatorMode.LENIENT_ORDER);
    // 
    return bar.getCount();
}
Also used : KriptonByteArrayOutputStream(com.abubusoft.kripton.common.KriptonByteArrayOutputStream)

Aggregations

KriptonByteArrayOutputStream (com.abubusoft.kripton.common.KriptonByteArrayOutputStream)264 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)257 JacksonWrapperSerializer (com.abubusoft.kripton.persistence.JacksonWrapperSerializer)257 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)257 HashMap (java.util.HashMap)23 Map (java.util.Map)23 BeanInner (sqlite.kripton58.BeanInner)9 LinkedHashMap (java.util.LinkedHashMap)8 BigDecimal (java.math.BigDecimal)6 Time (java.sql.Time)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 BinderContext (com.abubusoft.kripton.BinderContext)1 KriptonCborContext (com.abubusoft.kripton.KriptonCborContext)1 KriptonPropertiesContext (com.abubusoft.kripton.KriptonPropertiesContext)1 KriptonXmlContext (com.abubusoft.kripton.KriptonXmlContext)1 KriptonYamlContext (com.abubusoft.kripton.KriptonYamlContext)1