Search in sources :

Example 21 with BeanInner

use of sqlite.kripton58.BeanInner in project kripton by xcesco.

the class BeanBeanBindMap method serializeOnJacksonAsString.

@Override
public int serializeOnJacksonAsString(BeanBean object, JsonGenerator jacksonSerializer) throws Exception {
    jacksonSerializer.writeStartObject();
    int fieldCount = 0;
    // Serialized Field:
    // field id (mapped with "id")
    jacksonSerializer.writeStringField("id", PrimitiveUtils.writeLong(object.getId()));
    // field value (mapped with "value")
    if (object.getValue() != null) {
        fieldCount++;
        int n = object.getValue().length;
        BeanInner item;
        // write wrapper tag
        jacksonSerializer.writeFieldName("value");
        if (n > 0) {
            jacksonSerializer.writeStartArray();
            for (int i = 0; i < n; i++) {
                item = object.getValue()[i];
                if (item == null) {
                    jacksonSerializer.writeString("null");
                } else {
                    if (beanInnerBindMap.serializeOnJacksonAsString(item, jacksonSerializer) == 0) {
                        jacksonSerializer.writeNullField("value");
                    }
                }
            }
            jacksonSerializer.writeEndArray();
        } else {
            jacksonSerializer.writeString("");
        }
    }
    // field value2 (mapped with "value2")
    if (object.getValue2() != null) {
        fieldCount++;
        int n = object.getValue2().length;
        BeanInner item;
        // write wrapper tag
        jacksonSerializer.writeFieldName("value2");
        if (n > 0) {
            jacksonSerializer.writeStartArray();
            for (int i = 0; i < n; i++) {
                item = object.getValue2()[i];
                if (item == null) {
                    jacksonSerializer.writeString("null");
                } else {
                    if (beanInnerBindMap.serializeOnJacksonAsString(item, jacksonSerializer) == 0) {
                        jacksonSerializer.writeNullField("value2");
                    }
                }
            }
            jacksonSerializer.writeEndArray();
        } else {
            jacksonSerializer.writeString("");
        }
    }
    jacksonSerializer.writeEndObject();
    return fieldCount;
}
Also used : BeanInner(sqlite.kripton58.BeanInner)

Example 22 with BeanInner

use of sqlite.kripton58.BeanInner in project kripton by xcesco.

the class BeanBeanBindMap method parseOnJacksonAsString.

/**
 * parse with jackson
 */
@Override
public BeanBean parseOnJacksonAsString(JsonParser jacksonParser) throws Exception {
    BeanBean instance = new BeanBean();
    String fieldName;
    if (jacksonParser.getCurrentToken() == null) {
        jacksonParser.nextToken();
    }
    if (jacksonParser.getCurrentToken() != JsonToken.START_OBJECT) {
        jacksonParser.skipChildren();
        return instance;
    }
    while (jacksonParser.nextToken() != JsonToken.END_OBJECT) {
        fieldName = jacksonParser.getCurrentName();
        jacksonParser.nextToken();
        // Parse fields:
        switch(fieldName) {
            case "id":
                // field id (mapped with "id")
                instance.setId(PrimitiveUtils.readLong(jacksonParser.getText(), 0L));
                break;
            case "value":
                // field value (mapped with "value")
                if (jacksonParser.currentToken() == JsonToken.START_ARRAY) {
                    ArrayList<BeanInner> collection = new ArrayList<>();
                    BeanInner item = null;
                    String tempValue = null;
                    while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
                        tempValue = jacksonParser.getValueAsString();
                        if (jacksonParser.currentToken() == JsonToken.VALUE_STRING && "null".equals(tempValue)) {
                            item = null;
                        } else {
                            item = beanInnerBindMap.parseOnJacksonAsString(jacksonParser);
                        }
                        collection.add(item);
                    }
                    instance.setValue(CollectionUtils.asArray(collection, new BeanInner[collection.size()]));
                } else if (jacksonParser.currentToken() == JsonToken.VALUE_STRING && !StringUtils.hasText(jacksonParser.getValueAsString())) {
                    ArrayList<BeanInner> collection = new ArrayList<>();
                    instance.setValue(CollectionUtils.asArray(collection, new BeanInner[collection.size()]));
                }
                break;
            case "value2":
                // field value2 (mapped with "value2")
                if (jacksonParser.currentToken() == JsonToken.START_ARRAY) {
                    ArrayList<BeanInner> collection = new ArrayList<>();
                    BeanInner item = null;
                    String tempValue = null;
                    while (jacksonParser.nextToken() != JsonToken.END_ARRAY) {
                        tempValue = jacksonParser.getValueAsString();
                        if (jacksonParser.currentToken() == JsonToken.VALUE_STRING && "null".equals(tempValue)) {
                            item = null;
                        } else {
                            item = beanInnerBindMap.parseOnJacksonAsString(jacksonParser);
                        }
                        collection.add(item);
                    }
                    instance.setValue2(CollectionUtils.asArray(collection, new BeanInner[collection.size()]));
                } else if (jacksonParser.currentToken() == JsonToken.VALUE_STRING && !StringUtils.hasText(jacksonParser.getValueAsString())) {
                    ArrayList<BeanInner> collection = new ArrayList<>();
                    instance.setValue2(CollectionUtils.asArray(collection, new BeanInner[collection.size()]));
                }
                break;
            default:
                jacksonParser.skipChildren();
                break;
        }
    }
    return instance;
}
Also used : BeanInner(sqlite.kripton58.BeanInner) ArrayList(java.util.ArrayList)

Example 23 with BeanInner

use of sqlite.kripton58.BeanInner in project kripton by xcesco.

the class BeanBeanBindMap method serializeOnXml.

/**
 * method for xml serialization
 */
@Override
public void serializeOnXml(BeanBean object, XMLSerializer xmlSerializer, int currentEventType) throws Exception {
    if (currentEventType == 0) {
        xmlSerializer.writeStartElement("beanBean");
    }
    // Persisted fields:
    // field id (mapped with "id")
    xmlSerializer.writeStartElement("id");
    xmlSerializer.writeLong(object.getId());
    xmlSerializer.writeEndElement();
    // field value (mapped with "value")
    if (object.getValue() != null) {
        int n = object.getValue().length;
        BeanInner item;
        for (int i = 0; i < n; i++) {
            item = object.getValue()[i];
            if (item == null) {
                xmlSerializer.writeEmptyElement("value");
            } else {
                xmlSerializer.writeStartElement("value");
                beanInnerBindMap.serializeOnXml(item, xmlSerializer, 2);
                xmlSerializer.writeEndElement();
            }
        }
        // to distinguish between first empty element and empty collection, we write an attribute emptyCollection
        if (n == 0) {
            xmlSerializer.writeStartElement("value");
            xmlSerializer.writeAttribute("emptyCollection", "true");
            xmlSerializer.writeEndElement();
        }
    }
    // field value2 (mapped with "value2")
    if (object.getValue2() != null) {
        int n = object.getValue2().length;
        BeanInner item;
        for (int i = 0; i < n; i++) {
            item = object.getValue2()[i];
            if (item == null) {
                xmlSerializer.writeEmptyElement("value2");
            } else {
                xmlSerializer.writeStartElement("value2");
                beanInnerBindMap.serializeOnXml(item, xmlSerializer, 2);
                xmlSerializer.writeEndElement();
            }
        }
        // to distinguish between first empty element and empty collection, we write an attribute emptyCollection
        if (n == 0) {
            xmlSerializer.writeStartElement("value2");
            xmlSerializer.writeAttribute("emptyCollection", "true");
            xmlSerializer.writeEndElement();
        }
    }
    if (currentEventType == 0) {
        xmlSerializer.writeEndElement();
    }
}
Also used : BeanInner(sqlite.kripton58.BeanInner)

Example 24 with BeanInner

use of sqlite.kripton58.BeanInner in project kripton by xcesco.

the class BeanBeanTable method serializeValue2.

/**
 * for attribute value2 serialization
 */
public static byte[] serializeValue2(BeanInner[] 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++;
            int n = value.length;
            BeanInner item;
            // write wrapper tag
            jacksonSerializer.writeFieldName("element");
            jacksonSerializer.writeStartArray();
            for (int i = 0; i < n; i++) {
                item = value[i];
                if (item == null) {
                    jacksonSerializer.writeNull();
                } else {
                    beanInnerBindMap.serializeOnJackson(item, jacksonSerializer);
                }
            }
            jacksonSerializer.writeEndArray();
        }
        jacksonSerializer.writeEndObject();
        jacksonSerializer.flush();
        return stream.toByteArray();
    } catch (Exception e) {
        throw (new KriptonRuntimeException(e.getMessage()));
    }
}
Also used : BeanInner(sqlite.kripton58.BeanInner) 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) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException)

Example 25 with BeanInner

use of sqlite.kripton58.BeanInner in project kripton by xcesco.

the class BeanBeanTable method serializeValue.

/**
 * for attribute value serialization
 */
public static byte[] serializeValue(BeanInner[] 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++;
            int n = value.length;
            BeanInner item;
            // write wrapper tag
            jacksonSerializer.writeFieldName("element");
            jacksonSerializer.writeStartArray();
            for (int i = 0; i < n; i++) {
                item = value[i];
                if (item == null) {
                    jacksonSerializer.writeNull();
                } else {
                    beanInnerBindMap.serializeOnJackson(item, jacksonSerializer);
                }
            }
            jacksonSerializer.writeEndArray();
        }
        jacksonSerializer.writeEndObject();
        jacksonSerializer.flush();
        return stream.toByteArray();
    } catch (Exception e) {
        throw (new KriptonRuntimeException(e.getMessage()));
    }
}
Also used : BeanInner(sqlite.kripton58.BeanInner) 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) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException)

Aggregations

BeanInner (sqlite.kripton58.BeanInner)33 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)18 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)18 ArrayList (java.util.ArrayList)14 KriptonByteArrayOutputStream (com.abubusoft.kripton.common.KriptonByteArrayOutputStream)9 JacksonWrapperParser (com.abubusoft.kripton.persistence.JacksonWrapperParser)9 JacksonWrapperSerializer (com.abubusoft.kripton.persistence.JacksonWrapperSerializer)9 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)9 JsonParser (com.fasterxml.jackson.core.JsonParser)9 LinkedList (java.util.LinkedList)3