aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/readers/StructReader.java
blob: 41b197463b656f7c54da3736717df1674fbb1b69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.json.readers;

import com.fasterxml.jackson.core.JsonToken;
import com.yahoo.document.Field;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.StructuredFieldValue;
import com.yahoo.document.json.JsonReaderException;
import com.yahoo.document.json.TokenBuffer;

import static com.yahoo.document.json.readers.SingleValueReader.readSingleValue;

public class StructReader {

    /**
     * Fills this struct.
     *
     * @return true if all this was applied and false if it was ignored because the field does not exist
     */
    public static boolean fillStruct(TokenBuffer buffer, StructuredFieldValue parent, boolean ignoreUndefinedFields) {
        // do note the order of initializing initNesting and token is relevant for empty docs
        int initialNesting = buffer.nesting();
        buffer.next();

        boolean fullyApplied = true;
        while (buffer.nesting() >= initialNesting) {
            Field field = parent.getField(buffer.currentName());
            if (field == null) {
                if (! ignoreUndefinedFields)
                    throw new IllegalArgumentException("No field '" + buffer.currentName() + "' in the structure of type '" +
                                                       parent.getDataType().getDataTypeName() +
                                                       "', which has the fields: " + parent.getDataType().getFields());
                buffer.skipToRelativeNesting(1);
                fullyApplied = false;
                continue;
            }

            try {
                if (buffer.current() != JsonToken.VALUE_NULL) {
                    FieldValue v = readSingleValue(buffer, field.getDataType(), ignoreUndefinedFields);
                    parent.setFieldValue(field, v);
                }
                buffer.next();
            } catch (IllegalArgumentException e) {
                throw new JsonReaderException(field, e);
            }
        }
        return fullyApplied;
    }

}