Jackson - general getValue method

All subclasses ValueNodein Jackson (JSON library) have different methods for getting the base value object, for example. IntNodehas getIntValue, BooleanNodehas getBooleanValue, etc.

Why is there no universal / polymorphic method, called simply getValue, which simply returns Object, and Objectsomewhere Integer, or Booleanso on, depending on the type of node Is the method called?

Or ... is there really such a method? I need such a method for my purposes, but it seems that the developers of the library did not find that adding such a method would be useful. Or ... for some reason, is this method missing?

My goal: in the code below, I look through the tree and create a structure composed only of HashMap, Object[]and basic Java types (eg Integer, Booleanetc.). If I had such a method, instead of all these if-else if-else if blocks , I would have only one method call (in the case when it JsonNodeis a leaf node, i.e. a subtype ValueNode). But it seems that I do not have such a method in Jackson. So I had to encode all these ugly if-else if-else if blocks .

CODE:

    @SuppressWarnings({ "rawtypes", "unchecked" })
        private static Object traverse(JsonNode nd) {
            if (nd instanceof ObjectNode) {
                ObjectNode ndd = (ObjectNode) nd;
                HashMap mp = new HashMap();
                Iterator<String> it = ndd.getFieldNames();
                while (it.hasNext()) {
                    String s = it.next();
                    mp.put(s, traverse(ndd.get(s)));
                }
                return mp;
            } else if (nd instanceof ArrayNode) {
                ArrayNode ndd = (ArrayNode) nd;
                Object[] arr = new Object[ndd.size()];
                for (int i = 0; i < ndd.size(); i++) {
                    arr[i] = traverse(ndd.get(i));
                }
                return arr;
            } else if (nd instanceof NullNode) {
                // NullNode ndd = (NullNode)nd;
                return null;
            } else if (nd instanceof BooleanNode) {
                BooleanNode ndd = (BooleanNode) nd;
                return ndd.getBooleanValue();
            } else if (nd instanceof IntNode) {
                IntNode ndd = (IntNode) nd;
                return ndd.getIntValue();
            } else if (nd instanceof LongNode) {
                LongNode ndd = (LongNode) nd;
                return ndd.getLongValue();
            } else if (nd instanceof DoubleNode) {
                DoubleNode ndd = (DoubleNode) nd;
                return ndd.getDoubleValue();
            } else if (nd instanceof DecimalNode) {
                DecimalNode ndd = (DecimalNode) nd;
                return ndd.getDecimalValue();
            } else if (nd instanceof BigIntegerNode) {
                BigIntegerNode ndd = (BigIntegerNode) nd;
                return ndd.getBigIntegerValue();
            } else if (nd instanceof TextNode) {
                TextNode ndd = (TextNode) nd;
                return ndd.getTextValue();
            }

            throw new IllegalStateException("Failed while traversing the JSON tree at node: ||| " + nd.asText() + " |||");
        }
+4
source share
2 answers

Since you are returning Object, you can use something like this

ObjectMapper mapper = new ObjectMapper();
return mapper.convertValue(nd,Object.class);

        JsonNode nodeBool = BooleanNode.TRUE;
        Object objectBool = mapper.convertValue(nodeBool,Object.class);
        Boolean returnValBool = (Boolean) objectBool;
        System.err.println(returnValBool);

        JsonNode nodeDouble = new DoubleNode(3.4);
        Object objectDouble =    mapper.convertValue(nodeDouble,Object.class);
        Double returnValDouble = (Double) objectDouble;
        System.err.println(returnValDouble);

:

true
3.4
+4

:)

, . , , ? , , , .

, (), (..,..)

:

. , . .

.

:

public static class MyGen extends GeneratorBase {

        protected MyGen(int features, ObjectCodec codec) {
            super(features, codec);
        }

        private Object currentObject = null;

        @Override
        public void flush() throws IOException {
            // do nothing
        }

        @Override
        protected void _releaseBuffers() {
            // do nothing           
        }

        @Override
        protected void _verifyValueWrite(String typeMsg) throws IOException {
            // do nothing
        }

        @Override
        public void writeStartArray() throws IOException {
            // do nothing
        }

        @Override
        public void writeEndArray() throws IOException {
        }           // do nothing

        @Override
        public void writeStartObject() throws IOException {
            // do nothing
        }

        @Override
        public void writeEndObject() throws IOException {
            // do nothing
        }

        @Override
        public void writeFieldName(String name) throws IOException {
            // do nothing
        }

        @Override
        public void writeString(String text) throws IOException {
            currentObject = text;
        }

        @Override
        public void writeString(char[] text, int offset, int len) throws IOException {
            currentObject = new String(text);
        }

        @Override
        public void writeRawUTF8String(byte[] text, int offset, int length) throws IOException {
            currentObject = new String(text);
        }

        @Override
        public void writeUTF8String(byte[] text, int offset, int length) throws IOException {
            currentObject = new String(text);
        }

        @Override
        public void writeRaw(String text) throws IOException {
            currentObject = new String(text);
        }

        @Override
        public void writeRaw(String text, int offset, int len) throws IOException {
            currentObject = new String(text);           
        }

        @Override
        public void writeRaw(char[] text, int offset, int len) throws IOException {
            currentObject = new String(text);           
        }

        @Override
        public void writeRaw(char c) throws IOException {
            currentObject = new Character(c);
        }

        @Override
        public void writeBinary(Base64Variant bv, byte[] data, int offset, int len) throws IOException {
            currentObject = bv;
        }

        @Override
        public void writeNumber(int v) throws IOException {
            currentObject = new Integer(v);
        }

        @Override
        public void writeNumber(long v) throws IOException {
            currentObject = new Long(v);
        }

        @Override
        public void writeNumber(BigInteger v) throws IOException {
            currentObject = v;
        }

        @Override
        public void writeNumber(double v) throws IOException {
            currentObject = new Double(v);
        }

        @Override
        public void writeNumber(float v) throws IOException {
            currentObject = new Float(v);
        }

        @Override
        public void writeNumber(BigDecimal v) throws IOException {
            currentObject = v;
        }

        @Override
        public void writeNumber(String encodedValue) throws IOException {
            currentObject = encodedValue;
        }

        @Override
        public void writeBoolean(boolean state) throws IOException {
            currentObject = new Boolean(state);
        }

        @Override
        public void writeNull() throws IOException {
            currentObject = null;
        }

    }

, " ", , , .

:

public static void main(String[] args) throws JsonProcessingException, IOException {
        MyGen gen = new MyGen(0, new ObjectMapper());


        String test = "{ \"a\" : \"test\", \"b\" : 1, \"c\" : true, \"d\" : 2.5 }";

        JsonNode tree = new ObjectMapper().readTree(test);

        Impl instance = new DefaultSerializerProvider.Impl();

        ObjectMapper m = new ObjectMapper();

        for(JsonNode node : tree) {
            node.serialize(gen, instance);
            Object currentObject = gen.currentObject;
            System.out.println(currentObject);
        }

    }

:

test
1
true
2.5

, :)

Ps: - , ,

Edit:

. , , . .

+2

Source: https://habr.com/ru/post/1654710/


All Articles