Package org.msgpack.value.holder

Examples of org.msgpack.value.holder.ValueHolder


            // Here is a list of message pack data format: https://github.com/msgpack/msgpack/blob/master/spec.md#overview
            MessageFormat format = unpacker.getNextFormat();

            // Alternatively you can use ValueHolder to extract a value of any type
            // NOTE: Value interface is in a preliminary state, so the following code might change in future releases
            ValueHolder v = new ValueHolder();
            format = unpacker.unpackValue(v);
            switch(format.getValueType()) {
                case NIL:
                    Value nil = v.get();
                    nil.isNil(); // true
                    System.out.println("read nil");
                    break;
                case BOOLEAN:
                    boolean b = v.get().asBoolean().toBoolean();
                    System.out.println("read boolean: " + b);
                    break;
                case INTEGER:
                    IntegerHolder ih = v.getIntegerHolder();
                    if(ih.isValidInt()) { // int range check [-2^31-1, 2^31-1]
                        int i = ih.asInt();
                        System.out.println("read int: " + i);
                    }
                    else {
                        long l = ih.asLong();
                        System.out.println("read long: " + l);
                    }
                    break;
                case FLOAT:
                    FloatHolder fh = v.getFloatHolder();
                    float f = fh.toFloat();   // read as float
                    double d = fh.toDouble(); // read as double
                    System.out.println("read float: " + d);
                    break;
                case STRING:
                    String s = v.get().asString().toString();
                    System.out.println("read string: " + s);
                    break;
                case BINARY:
                    // Message buffer is an efficient byte buffer
                    MessageBuffer mb = v.get().asBinary().toMessageBuffer();
                    System.out.println("read binary: " + mb.toHexString(0, mb.size()));
                    break;
                case ARRAY:
                    ArrayValue arr = v.get().asArrayValue();
                    for(ValueRef a : arr) {
                        System.out.println("read array element: " + a);
                    }
                    break;
                case EXTENDED:
                    ExtendedValue ev = v.get().asExtended();
                    int extType = ev.getExtType();
                    byte[] extValue = ev.toByteArray();
                    break;
            }
        }
View Full Code Here


    private ValueHolder valueHolder;

    public CursorImpl(MessageUnpacker unpacker) {
        this.unpacker = unpacker;
        this.currentFormat = MessageFormat.NIL;
        this.valueHolder = new ValueHolder();
    }
View Full Code Here

TOP

Related Classes of org.msgpack.value.holder.ValueHolder

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.